0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
503
|
10 #include <ngx_md5.h>
|
0
|
11
|
|
12
|
558
|
13 static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r,
|
|
14 ngx_http_cache_t *c);
|
|
15 #if (NGX_HAVE_FILE_AIO)
|
|
16 static void ngx_http_cache_aio_event_handler(ngx_event_t *ev);
|
|
17 #endif
|
507
|
18 static ngx_int_t ngx_http_file_cache_exists(ngx_http_file_cache_t *cache,
|
|
19 ngx_http_cache_t *c);
|
503
|
20 static ngx_http_file_cache_node_t *
|
|
21 ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key);
|
|
22 static void ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
23 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
|
|
24 static void ngx_http_file_cache_cleanup(void *data);
|
507
|
25 static time_t ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache);
|
|
26 static time_t ngx_http_file_cache_expire(ngx_http_file_cache_t *cache);
|
|
27 static void ngx_http_file_cache_delete(ngx_http_file_cache_t *cache,
|
|
28 ngx_queue_t *q, u_char *name);
|
|
29 static ngx_int_t
|
|
30 ngx_http_file_cache_manager_sleep(ngx_http_file_cache_t *cache);
|
|
31 static ngx_int_t ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx,
|
503
|
32 ngx_str_t *path);
|
507
|
33 static ngx_int_t ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx,
|
|
34 ngx_str_t *path);
|
|
35 static ngx_int_t ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx,
|
|
36 ngx_str_t *path);
|
|
37 static ngx_int_t ngx_http_file_cache_add(ngx_http_file_cache_t *cache,
|
|
38 ngx_http_cache_t *c);
|
|
39 static ngx_int_t ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx,
|
503
|
40 ngx_str_t *path);
|
0
|
41
|
|
42
|
542
|
43 ngx_str_t ngx_http_cache_status[] = {
|
|
44 ngx_string("MISS"),
|
|
45 ngx_string("EXPIRED"),
|
|
46 ngx_string("STALE"),
|
|
47 ngx_string("UPDATING"),
|
|
48 ngx_string("HIT")
|
|
49 };
|
|
50
|
|
51
|
503
|
52 static u_char ngx_http_file_cache_key[] = { LF, 'K', 'E', 'Y', ':', ' ' };
|
0
|
53
|
|
54
|
503
|
55 static ngx_int_t
|
|
56 ngx_http_file_cache_init(ngx_shm_zone_t *shm_zone, void *data)
|
0
|
57 {
|
503
|
58 ngx_http_file_cache_t *ocache = data;
|
0
|
59
|
507
|
60 size_t len;
|
554
|
61 ngx_uint_t n;
|
503
|
62 ngx_http_file_cache_t *cache;
|
0
|
63
|
503
|
64 cache = shm_zone->data;
|
0
|
65
|
503
|
66 if (ocache) {
|
|
67 if (ngx_strcmp(cache->path->name.data, ocache->path->name.data) != 0) {
|
|
68 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
|
69 "cache \"%V\" uses the \"%V\" cache path "
|
|
70 "while previously it used the \"%V\" cache path",
|
520
|
71 &shm_zone->shm.name, &cache->path->name,
|
503
|
72 &ocache->path->name);
|
0
|
73
|
|
74 return NGX_ERROR;
|
|
75 }
|
|
76
|
554
|
77 for (n = 0; n < 3; n++) {
|
|
78 if (cache->path->level[n] != ocache->path->level[n]) {
|
|
79 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
|
80 "cache \"%V\" had previously different levels",
|
|
81 &shm_zone->shm.name);
|
|
82 return NGX_ERROR;
|
|
83 }
|
|
84 }
|
|
85
|
520
|
86 cache->sh = ocache->sh;
|
|
87
|
503
|
88 cache->shpool = ocache->shpool;
|
507
|
89 cache->bsize = ocache->bsize;
|
|
90
|
|
91 cache->max_size /= cache->bsize;
|
503
|
92
|
554
|
93 if (!cache->sh->cold || cache->sh->loading) {
|
|
94 cache->path->loader = NULL;
|
|
95 }
|
|
96
|
503
|
97 return NGX_OK;
|
|
98 }
|
|
99
|
|
100 cache->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
101
|
520
|
102 if (shm_zone->shm.exists) {
|
|
103 cache->sh = cache->shpool->data;
|
|
104 cache->bsize = ngx_fs_bsize(cache->path->name.data);
|
|
105
|
|
106 return NGX_OK;
|
503
|
107 }
|
|
108
|
520
|
109 cache->sh = ngx_slab_alloc(cache->shpool, sizeof(ngx_http_file_cache_sh_t));
|
|
110 if (cache->sh == NULL) {
|
503
|
111 return NGX_ERROR;
|
|
112 }
|
|
113
|
520
|
114 cache->shpool->data = cache->sh;
|
|
115
|
|
116 ngx_rbtree_init(&cache->sh->rbtree, &cache->sh->sentinel,
|
503
|
117 ngx_http_file_cache_rbtree_insert_value);
|
|
118
|
520
|
119 ngx_queue_init(&cache->sh->queue);
|
503
|
120
|
520
|
121 cache->sh->cold = 1;
|
554
|
122 cache->sh->loading = 0;
|
520
|
123 cache->sh->size = 0;
|
507
|
124
|
|
125 cache->bsize = ngx_fs_bsize(cache->path->name.data);
|
|
126
|
|
127 cache->max_size /= cache->bsize;
|
|
128
|
520
|
129 len = sizeof(" in cache keys zone \"\"") + shm_zone->shm.name.len;
|
507
|
130
|
|
131 cache->shpool->log_ctx = ngx_slab_alloc(cache->shpool, len);
|
|
132 if (cache->shpool->log_ctx == NULL) {
|
|
133 return NGX_ERROR;
|
|
134 }
|
|
135
|
|
136 ngx_sprintf(cache->shpool->log_ctx, " in cache keys zone \"%V\"%Z",
|
520
|
137 &shm_zone->shm.name);
|
503
|
138
|
|
139 return NGX_OK;
|
|
140 }
|
|
141
|
|
142
|
|
143 void
|
|
144 ngx_http_file_cache_create_key(ngx_http_request_t *r)
|
|
145 {
|
|
146 size_t len;
|
|
147 ngx_str_t *key;
|
|
148 ngx_uint_t i;
|
|
149 ngx_md5_t md5;
|
|
150 ngx_http_cache_t *c;
|
|
151
|
|
152 c = r->cache;
|
|
153
|
|
154 len = 0;
|
|
155
|
|
156 ngx_crc32_init(c->crc32);
|
|
157 ngx_md5_init(&md5);
|
|
158
|
|
159 key = c->keys.elts;
|
|
160 for (i = 0; i < c->keys.nelts; i++) {
|
|
161 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
162 "http cache key: \"%V\"", &key[i]);
|
|
163
|
|
164 len += key[i].len;
|
|
165
|
|
166 ngx_crc32_update(&c->crc32, key[i].data, key[i].len);
|
|
167 ngx_md5_update(&md5, key[i].data, key[i].len);
|
|
168 }
|
|
169
|
|
170 c->header_start = sizeof(ngx_http_file_cache_header_t)
|
|
171 + sizeof(ngx_http_file_cache_key) + len + 1;
|
|
172
|
|
173 ngx_crc32_final(c->crc32);
|
|
174 ngx_md5_final(c->key, &md5);
|
|
175 }
|
|
176
|
|
177
|
|
178 ngx_int_t
|
|
179 ngx_http_file_cache_open(ngx_http_request_t *r)
|
|
180 {
|
558
|
181 u_char *p;
|
|
182 ngx_int_t rc, rv;
|
|
183 ngx_uint_t cold, test;
|
|
184 ngx_path_t *path;
|
|
185 ngx_http_cache_t *c;
|
|
186 ngx_pool_cleanup_t *cln;
|
|
187 ngx_open_file_info_t of;
|
|
188 ngx_http_file_cache_t *cache;
|
|
189 ngx_http_core_loc_conf_t *clcf;
|
503
|
190
|
|
191 c = r->cache;
|
558
|
192
|
|
193 if (c->buf) {
|
|
194 return ngx_http_file_cache_read(r, c);
|
|
195 }
|
|
196
|
503
|
197 cache = c->file_cache;
|
|
198
|
|
199 cln = ngx_pool_cleanup_add(r->pool, 0);
|
|
200 if (cln == NULL) {
|
|
201 return NGX_ERROR;
|
|
202 }
|
|
203
|
507
|
204 rc = ngx_http_file_cache_exists(cache, c);
|
503
|
205
|
538
|
206 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
207 "http file cache exists: %i e:%d", rc, c->exists);
|
503
|
208
|
|
209 if (rc == NGX_ERROR) {
|
|
210 return rc;
|
|
211 }
|
|
212
|
|
213 cln->handler = ngx_http_file_cache_cleanup;
|
|
214 cln->data = c;
|
|
215
|
|
216 if (rc == NGX_AGAIN) {
|
558
|
217 return NGX_HTTP_CACHE_SCARCE;
|
503
|
218 }
|
|
219
|
520
|
220 cold = cache->sh->cold;
|
503
|
221
|
|
222 if (rc == NGX_OK) {
|
|
223
|
|
224 if (c->error) {
|
|
225 return c->error;
|
|
226 }
|
|
227
|
|
228 c->temp_file = 1;
|
|
229 test = c->exists ? 1 : 0;
|
|
230 rv = NGX_DECLINED;
|
|
231
|
|
232 } else { /* rc == NGX_DECLINED */
|
|
233
|
|
234 if (c->min_uses > 1) {
|
|
235
|
|
236 if (!cold) {
|
558
|
237 return NGX_HTTP_CACHE_SCARCE;
|
0
|
238 }
|
|
239
|
503
|
240 test = 1;
|
558
|
241 rv = NGX_HTTP_CACHE_SCARCE;
|
503
|
242
|
|
243 } else {
|
|
244 c->temp_file = 1;
|
|
245 test = cold ? 1 : 0;
|
|
246 rv = NGX_DECLINED;
|
0
|
247 }
|
|
248 }
|
|
249
|
503
|
250 path = cache->path;
|
|
251
|
|
252 c->file.name.len = path->name.len + 1 + path->len
|
|
253 + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
254
|
|
255 c->file.name.data = ngx_pnalloc(r->pool, c->file.name.len + 1);
|
|
256 if (c->file.name.data == NULL) {
|
|
257 return NGX_ERROR;
|
|
258 }
|
|
259
|
|
260 ngx_memcpy(c->file.name.data, path->name.data, path->name.len);
|
|
261
|
|
262 p = c->file.name.data + path->name.len + 1 + path->len;
|
|
263 p = ngx_hex_dump(p, c->key, NGX_HTTP_CACHE_KEY_LEN);
|
|
264 *p = '\0';
|
|
265
|
|
266 ngx_create_hashed_filename(path, c->file.name.data, c->file.name.len);
|
|
267
|
|
268 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
269 "cache file: \"%s\"", c->file.name.data);
|
|
270
|
|
271 if (!test) {
|
|
272 return NGX_DECLINED;
|
|
273 }
|
|
274
|
|
275 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
276
|
|
277 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
0
|
278
|
503
|
279 of.uniq = c->uniq;
|
|
280 of.valid = clcf->open_file_cache_valid;
|
|
281 of.min_uses = clcf->open_file_cache_min_uses;
|
|
282 of.events = clcf->open_file_cache_events;
|
|
283 of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
|
|
284
|
|
285 if (ngx_open_cached_file(clcf->open_file_cache, &c->file.name, &of, r->pool)
|
|
286 != NGX_OK)
|
|
287 {
|
|
288 switch (of.err) {
|
|
289
|
|
290 case 0:
|
|
291 return NGX_ERROR;
|
|
292
|
|
293 case NGX_ENOENT:
|
|
294 case NGX_ENOTDIR:
|
|
295 return rv;
|
|
296
|
|
297 default:
|
|
298 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
|
|
299 ngx_open_file_n " \"%s\" failed", c->file.name.data);
|
|
300 return NGX_ERROR;
|
|
301 }
|
|
302 }
|
|
303
|
|
304 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
305 "http file cache fd: %d", of.fd);
|
|
306
|
|
307 c->file.fd = of.fd;
|
505
|
308 c->file.log = r->connection->log;
|
558
|
309 c->uniq = of.uniq;
|
|
310 c->length = of.size;
|
503
|
311
|
|
312 c->buf = ngx_create_temp_buf(r->pool, c->body_start);
|
|
313 if (c->buf == NULL) {
|
|
314 return NGX_ERROR;
|
|
315 }
|
|
316
|
558
|
317 return ngx_http_file_cache_read(r, c);
|
|
318 }
|
|
319
|
|
320
|
|
321 static ngx_int_t
|
|
322 ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c)
|
|
323 {
|
|
324 time_t now;
|
|
325 ssize_t n;
|
|
326 ngx_int_t rc;
|
|
327 ngx_http_file_cache_t *cache;
|
|
328 ngx_http_file_cache_header_t *h;
|
|
329
|
|
330 c = r->cache;
|
|
331
|
|
332 #if (NGX_HAVE_FILE_AIO)
|
|
333 {
|
|
334 ngx_http_core_loc_conf_t *clcf;
|
|
335
|
|
336 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
337
|
|
338 if (clcf->aio) {
|
|
339 n = ngx_file_aio_read(&c->file, c->buf->pos, c->body_start, 0, r->pool);
|
|
340
|
|
341 if (n == NGX_AGAIN) {
|
|
342 c->file.aio->data = r;
|
|
343 c->file.aio->handler = ngx_http_cache_aio_event_handler;
|
|
344
|
|
345 r->main->blocked++;
|
|
346 r->aio = 1;
|
|
347
|
|
348 return NGX_AGAIN;
|
|
349 }
|
|
350
|
|
351 } else {
|
|
352 n = ngx_read_file(&c->file, c->buf->pos, c->body_start, 0);
|
|
353 }
|
|
354 }
|
|
355 #else
|
|
356
|
503
|
357 n = ngx_read_file(&c->file, c->buf->pos, c->body_start, 0);
|
|
358
|
558
|
359 #endif
|
|
360
|
503
|
361 if (n == NGX_ERROR) {
|
0
|
362 return n;
|
|
363 }
|
|
364
|
503
|
365 if ((size_t) n <= c->header_start) {
|
|
366 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
|
26
|
367 "cache file \"%s\" is too small", c->file.name.data);
|
0
|
368 return NGX_ERROR;
|
|
369 }
|
|
370
|
503
|
371 h = (ngx_http_file_cache_header_t *) c->buf->pos;
|
0
|
372
|
503
|
373 if (h->crc32 != c->crc32 || (size_t) h->header_start != c->header_start) {
|
|
374 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
|
|
375 "cache file \"%s\" has md5 collision", c->file.name.data);
|
0
|
376 return NGX_DECLINED;
|
|
377 }
|
|
378
|
503
|
379 c->buf->last += n;
|
26
|
380
|
503
|
381 c->valid_sec = h->valid_sec;
|
|
382 c->last_modified = h->last_modified;
|
|
383 c->date = h->date;
|
|
384 c->valid_msec = h->valid_msec;
|
|
385 c->body_start = h->body_start;
|
26
|
386
|
503
|
387 r->cached = 1;
|
|
388
|
558
|
389 cache = c->file_cache;
|
|
390
|
|
391 if (cache->sh->cold) {
|
503
|
392
|
|
393 ngx_shmtx_lock(&cache->shpool->mutex);
|
26
|
394
|
507
|
395 if (!c->node->exists) {
|
|
396 c->node->uses = 1;
|
|
397 c->node->body_start = c->body_start;
|
|
398 c->node->exists = 1;
|
558
|
399 c->node->uniq = c->uniq;
|
507
|
400
|
520
|
401 cache->sh->size += (c->length + cache->bsize - 1) / cache->bsize;
|
507
|
402 }
|
503
|
403
|
|
404 ngx_shmtx_unlock(&cache->shpool->mutex);
|
0
|
405 }
|
|
406
|
507
|
407 now = ngx_time();
|
|
408
|
503
|
409 if (c->valid_sec < now) {
|
0
|
410
|
538
|
411 ngx_shmtx_lock(&cache->shpool->mutex);
|
0
|
412
|
538
|
413 if (c->node->updating) {
|
|
414 rc = NGX_HTTP_CACHE_UPDATING;
|
503
|
415
|
538
|
416 } else {
|
|
417 c->node->updating = 1;
|
|
418 rc = NGX_HTTP_CACHE_STALE;
|
|
419 }
|
|
420
|
|
421 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
422
|
|
423 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
424 "http file cache expired: %i %T %T",
|
|
425 rc, c->valid_sec, now);
|
|
426
|
|
427 return rc;
|
0
|
428 }
|
|
429
|
503
|
430 return NGX_OK;
|
|
431 }
|
|
432
|
|
433
|
558
|
434 #if (NGX_HAVE_FILE_AIO)
|
|
435
|
|
436
|
|
437 static void
|
|
438 ngx_http_cache_aio_event_handler(ngx_event_t *ev)
|
|
439 {
|
|
440 ngx_event_aio_t *aio;
|
|
441 ngx_http_request_t *r;
|
|
442
|
|
443 aio = ev->data;
|
|
444 r = aio->data;
|
|
445
|
|
446 r->main->blocked--;
|
|
447 r->aio = 0;
|
|
448
|
|
449 r->connection->write->handler(r->connection->write);
|
|
450 }
|
|
451
|
|
452 #endif
|
|
453
|
|
454
|
503
|
455 static ngx_int_t
|
507
|
456 ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
|
503
|
457 {
|
|
458 ngx_int_t rc;
|
|
459 ngx_http_file_cache_node_t *fcn;
|
|
460
|
|
461 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
462
|
507
|
463 fcn = ngx_http_file_cache_lookup(cache, c->key);
|
503
|
464
|
|
465 if (fcn) {
|
|
466 ngx_queue_remove(&fcn->queue);
|
|
467
|
|
468 if (fcn->error) {
|
|
469
|
|
470 if (fcn->valid_sec < ngx_time()) {
|
|
471 goto renew;
|
|
472 }
|
|
473
|
|
474 rc = NGX_OK;
|
|
475
|
|
476 goto done;
|
|
477 }
|
|
478
|
|
479 fcn->uses++;
|
|
480 fcn->count++;
|
|
481
|
|
482 if (fcn->exists) {
|
|
483
|
507
|
484 c->exists = fcn->exists;
|
|
485 c->body_start = fcn->body_start;
|
503
|
486
|
|
487 rc = NGX_OK;
|
|
488
|
|
489 goto done;
|
|
490 }
|
|
491
|
507
|
492 if (fcn->uses >= c->min_uses) {
|
503
|
493
|
507
|
494 c->exists = fcn->exists;
|
|
495 c->body_start = fcn->body_start;
|
503
|
496
|
|
497 rc = NGX_OK;
|
|
498
|
|
499 } else {
|
|
500 rc = NGX_AGAIN;
|
|
501 }
|
|
502
|
|
503 goto done;
|
|
504 }
|
|
505
|
|
506 fcn = ngx_slab_alloc_locked(cache->shpool,
|
|
507 sizeof(ngx_http_file_cache_node_t));
|
|
508 if (fcn == NULL) {
|
|
509 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
510
|
518
|
511 (void) ngx_http_file_cache_forced_expire(cache);
|
503
|
512
|
|
513 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
514
|
|
515 fcn = ngx_slab_alloc_locked(cache->shpool,
|
|
516 sizeof(ngx_http_file_cache_node_t));
|
|
517 if (fcn == NULL) {
|
|
518 rc = NGX_ERROR;
|
|
519 goto failed;
|
|
520 }
|
|
521 }
|
|
522
|
507
|
523 ngx_memcpy((u_char *) &fcn->node.key, c->key, sizeof(ngx_rbtree_key_t));
|
503
|
524
|
507
|
525 ngx_memcpy(fcn->key, &c->key[sizeof(ngx_rbtree_key_t)],
|
503
|
526 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
|
|
527
|
520
|
528 ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
|
503
|
529
|
|
530 renew:
|
|
531
|
|
532 rc = NGX_DECLINED;
|
|
533
|
|
534 fcn->uses = 1;
|
|
535 fcn->count = 1;
|
|
536 fcn->valid_msec = 0;
|
|
537 fcn->error = 0;
|
|
538 fcn->exists = 0;
|
|
539 fcn->valid_sec = 0;
|
|
540 fcn->uniq = 0;
|
|
541 fcn->body_start = 0;
|
509
|
542 fcn->length = 0;
|
503
|
543
|
|
544 done:
|
|
545
|
|
546 fcn->expire = ngx_time() + cache->inactive;
|
|
547
|
520
|
548 ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
|
503
|
549
|
507
|
550 c->uniq = fcn->uniq;
|
|
551 c->error = fcn->error;
|
|
552 c->node = fcn;
|
503
|
553
|
|
554 failed:
|
|
555
|
|
556 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
557
|
|
558 return rc;
|
|
559 }
|
|
560
|
|
561
|
|
562 static ngx_http_file_cache_node_t *
|
|
563 ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key)
|
|
564 {
|
|
565 ngx_int_t rc;
|
|
566 ngx_rbtree_key_t node_key;
|
|
567 ngx_rbtree_node_t *node, *sentinel;
|
|
568 ngx_http_file_cache_node_t *fcn;
|
|
569
|
|
570 ngx_memcpy((u_char *) &node_key, key, sizeof(ngx_rbtree_key_t));
|
|
571
|
520
|
572 node = cache->sh->rbtree.root;
|
|
573 sentinel = cache->sh->rbtree.sentinel;
|
503
|
574
|
|
575 while (node != sentinel) {
|
|
576
|
|
577 if (node_key < node->key) {
|
|
578 node = node->left;
|
|
579 continue;
|
|
580 }
|
|
581
|
|
582 if (node_key > node->key) {
|
|
583 node = node->right;
|
|
584 continue;
|
|
585 }
|
|
586
|
|
587 /* node_key == node->key */
|
|
588
|
|
589 do {
|
|
590 fcn = (ngx_http_file_cache_node_t *) node;
|
|
591
|
|
592 rc = ngx_memcmp(&key[sizeof(ngx_rbtree_key_t)], fcn->key,
|
|
593 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
|
|
594
|
|
595 if (rc == 0) {
|
|
596 return fcn;
|
|
597 }
|
|
598
|
|
599 node = (rc < 0) ? node->left : node->right;
|
|
600
|
|
601 } while (node != sentinel && node_key == node->key);
|
|
602
|
|
603 break;
|
|
604 }
|
|
605
|
|
606 /* not found */
|
|
607
|
|
608 return NULL;
|
|
609 }
|
|
610
|
|
611
|
|
612 static void
|
|
613 ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
614 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
615 {
|
|
616 ngx_rbtree_node_t **p;
|
|
617 ngx_http_file_cache_node_t *cn, *cnt;
|
|
618
|
|
619 for ( ;; ) {
|
|
620
|
|
621 if (node->key < temp->key) {
|
|
622
|
|
623 p = &temp->left;
|
|
624
|
|
625 } else if (node->key > temp->key) {
|
|
626
|
|
627 p = &temp->right;
|
|
628
|
|
629 } else { /* node->key == temp->key */
|
|
630
|
|
631 cn = (ngx_http_file_cache_node_t *) node;
|
|
632 cnt = (ngx_http_file_cache_node_t *) temp;
|
|
633
|
|
634 p = (ngx_memcmp(cn->key, cnt->key,
|
|
635 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t))
|
|
636 < 0)
|
|
637 ? &temp->left : &temp->right;
|
|
638 }
|
|
639
|
|
640 if (*p == sentinel) {
|
|
641 break;
|
|
642 }
|
|
643
|
|
644 temp = *p;
|
|
645 }
|
|
646
|
|
647 *p = node;
|
|
648 node->parent = temp;
|
|
649 node->left = sentinel;
|
|
650 node->right = sentinel;
|
|
651 ngx_rbt_red(node);
|
|
652 }
|
|
653
|
|
654
|
|
655 void
|
|
656 ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf)
|
|
657 {
|
|
658 ngx_http_file_cache_header_t *h = (ngx_http_file_cache_header_t *) buf;
|
|
659
|
|
660 u_char *p;
|
|
661 ngx_str_t *key;
|
|
662 ngx_uint_t i;
|
|
663 ngx_http_cache_t *c;
|
|
664
|
|
665 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
666 "http file cache set header");
|
|
667
|
|
668 c = r->cache;
|
|
669
|
|
670 h->valid_sec = c->valid_sec;
|
|
671 h->last_modified = c->last_modified;
|
|
672 h->date = c->date;
|
|
673 h->crc32 = c->crc32;
|
|
674 h->valid_msec = (u_short) c->valid_msec;
|
|
675 h->header_start = (u_short) c->header_start;
|
|
676 h->body_start = (u_short) c->body_start;
|
|
677
|
|
678 p = buf + sizeof(ngx_http_file_cache_header_t);
|
|
679
|
|
680 p = ngx_cpymem(p, ngx_http_file_cache_key, sizeof(ngx_http_file_cache_key));
|
|
681
|
|
682 key = c->keys.elts;
|
|
683 for (i = 0; i < c->keys.nelts; i++) {
|
|
684 p = ngx_copy(p, key[i].data, key[i].len);
|
|
685 }
|
|
686
|
|
687 *p = LF;
|
|
688 }
|
|
689
|
|
690
|
|
691 void
|
|
692 ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf)
|
|
693 {
|
507
|
694 off_t size;
|
503
|
695 ngx_int_t rc;
|
|
696 ngx_file_uniq_t uniq;
|
|
697 ngx_file_info_t fi;
|
|
698 ngx_http_cache_t *c;
|
|
699 ngx_ext_rename_file_t ext;
|
|
700 ngx_http_file_cache_t *cache;
|
|
701
|
|
702 c = r->cache;
|
|
703
|
|
704 if (c->updated) {
|
|
705 return;
|
|
706 }
|
|
707
|
|
708 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
709 "http file cache update");
|
|
710
|
|
711 c->updated = 1;
|
|
712
|
|
713 cache = c->file_cache;
|
|
714
|
|
715 uniq = 0;
|
|
716
|
|
717 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
718 "http file cache rename: \"%s\" to \"%s\"",
|
|
719 tf->file.name.data, c->file.name.data);
|
|
720
|
|
721 ext.access = NGX_FILE_OWNER_ACCESS;
|
|
722 ext.path_access = NGX_FILE_OWNER_ACCESS;
|
|
723 ext.time = -1;
|
|
724 ext.create_path = 1;
|
|
725 ext.delete_file = 1;
|
|
726 ext.log = r->connection->log;
|
|
727
|
|
728 rc = ngx_ext_rename_file(&tf->file.name, &c->file.name, &ext);
|
|
729
|
|
730 if (rc == NGX_OK) {
|
|
731
|
|
732 if (ngx_fd_info(tf->file.fd, &fi) == NGX_FILE_ERROR) {
|
|
733 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
734 ngx_fd_info_n " \"%s\" failed", tf->file.name.data);
|
|
735
|
|
736 rc = NGX_ERROR;
|
|
737
|
|
738 } else {
|
|
739 uniq = ngx_file_uniq(&fi);
|
|
740 }
|
|
741 }
|
|
742
|
507
|
743 size = (c->length + cache->bsize - 1) / cache->bsize;
|
|
744
|
503
|
745 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
746
|
|
747 c->node->count--;
|
|
748 c->node->uniq = uniq;
|
|
749 c->node->body_start = c->body_start;
|
|
750
|
507
|
751 size = size - (c->node->length + cache->bsize - 1) / cache->bsize;
|
|
752
|
|
753 c->node->length = c->length;
|
|
754
|
520
|
755 cache->sh->size += size;
|
507
|
756
|
503
|
757 if (rc == NGX_OK) {
|
|
758 c->node->exists = 1;
|
|
759 }
|
|
760
|
538
|
761 c->node->updating = 0;
|
|
762
|
503
|
763 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
764 }
|
|
765
|
|
766
|
|
767 ngx_int_t
|
|
768 ngx_http_cache_send(ngx_http_request_t *r)
|
|
769 {
|
|
770 ngx_int_t rc;
|
|
771 ngx_buf_t *b;
|
|
772 ngx_chain_t out;
|
|
773 ngx_http_cache_t *c;
|
|
774
|
|
775 c = r->cache;
|
|
776
|
|
777 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
778 "http file cache send: %s", c->file.name.data);
|
|
779
|
|
780 /* we need to allocate all before the header would be sent */
|
|
781
|
|
782 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
783 if (b == NULL) {
|
|
784 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
785 }
|
|
786
|
|
787 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
|
|
788 if (b->file == NULL) {
|
|
789 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
790 }
|
|
791
|
|
792 rc = ngx_http_send_header(r);
|
|
793
|
|
794 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
795 return rc;
|
|
796 }
|
|
797
|
|
798 b->file_pos = c->body_start;
|
|
799 b->file_last = c->length;
|
|
800
|
|
801 b->in_file = (c->length - c->body_start) ? 1: 0;
|
|
802 b->last_buf = (r == r->main) ? 1: 0;
|
|
803 b->last_in_chain = 1;
|
|
804
|
|
805 b->file->fd = c->file.fd;
|
|
806 b->file->name = c->file.name;
|
|
807 b->file->log = r->connection->log;
|
|
808
|
|
809 out.buf = b;
|
|
810 out.next = NULL;
|
|
811
|
|
812 return ngx_http_output_filter(r, &out);
|
|
813 }
|
|
814
|
|
815
|
|
816 void
|
|
817 ngx_http_file_cache_free(ngx_http_request_t *r, ngx_temp_file_t *tf)
|
|
818 {
|
|
819 ngx_http_cache_t *c;
|
|
820 ngx_http_file_cache_t *cache;
|
|
821
|
|
822 c = r->cache;
|
|
823
|
|
824 if (c->updated) {
|
|
825 return;
|
|
826 }
|
|
827
|
|
828 c->updated = 1;
|
|
829
|
|
830 cache = c->file_cache;
|
|
831
|
|
832 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
833 "http file cache free");
|
|
834
|
|
835 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
836
|
|
837 c->node->count--;
|
|
838
|
|
839 if (c->error) {
|
|
840 c->node->valid_sec = c->valid_sec;
|
|
841 c->node->valid_msec = c->valid_msec;
|
|
842 c->node->error = c->error;
|
|
843 }
|
|
844
|
538
|
845 c->node->updating = 0;
|
|
846
|
503
|
847 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
848
|
|
849 if (c->temp_file) {
|
|
850 if (tf && tf->file.fd != NGX_INVALID_FILE) {
|
|
851 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
852 "http file cache incomplete: \"%s\"",
|
|
853 tf->file.name.data);
|
|
854
|
|
855 if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {
|
|
856 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
857 ngx_delete_file_n " \"%s\" failed",
|
|
858 tf->file.name.data);
|
|
859 }
|
|
860 }
|
|
861 }
|
|
862 }
|
|
863
|
|
864
|
|
865 static void
|
|
866 ngx_http_file_cache_cleanup(void *data)
|
|
867 {
|
|
868 ngx_http_cache_t *c = data;
|
|
869
|
|
870 ngx_http_file_cache_t *cache;
|
|
871
|
|
872 if (c->updated) {
|
|
873 return;
|
|
874 }
|
|
875
|
|
876 c->updated = 1;
|
|
877
|
|
878 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
|
|
879 "http file cache cleanup");
|
|
880
|
|
881 if (c->error) {
|
|
882 return;
|
|
883 }
|
|
884
|
|
885 cache = c->file_cache;
|
|
886
|
|
887 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
888
|
|
889 c->node->count--;
|
|
890
|
|
891 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
892 }
|
|
893
|
|
894
|
|
895 static time_t
|
507
|
896 ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache)
|
|
897 {
|
|
898 u_char *name;
|
|
899 size_t len;
|
|
900 time_t wait;
|
|
901 ngx_uint_t tries;
|
|
902 ngx_path_t *path;
|
|
903 ngx_queue_t *q;
|
|
904 ngx_http_file_cache_node_t *fcn;
|
|
905
|
|
906 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
907 "http file cache forced expire");
|
|
908
|
|
909 path = cache->path;
|
|
910 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
911
|
|
912 name = ngx_alloc(len + 1, ngx_cycle->log);
|
|
913 if (name == NULL) {
|
518
|
914 return 10;
|
507
|
915 }
|
|
916
|
|
917 ngx_memcpy(name, path->name.data, path->name.len);
|
|
918
|
518
|
919 wait = 10;
|
507
|
920 tries = 0;
|
|
921
|
|
922 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
923
|
520
|
924 for (q = ngx_queue_last(&cache->sh->queue);
|
|
925 q != ngx_queue_sentinel(&cache->sh->queue);
|
507
|
926 q = ngx_queue_prev(q))
|
|
927 {
|
|
928 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
|
|
929
|
|
930 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
931 "http file cache forced expire: #%d %d %02xd%02xd%02xd%02xd",
|
|
932 fcn->count, fcn->exists,
|
|
933 fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);
|
|
934
|
|
935 if (fcn->count) {
|
|
936
|
|
937 if (tries++ < 20) {
|
|
938 continue;
|
|
939 }
|
|
940
|
|
941 wait = 1;
|
|
942
|
|
943 break;
|
|
944 }
|
|
945
|
|
946 if (!fcn->exists) {
|
|
947
|
|
948 ngx_queue_remove(q);
|
520
|
949 ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
|
507
|
950 ngx_slab_free_locked(cache->shpool, fcn);
|
|
951
|
|
952 break;
|
|
953 }
|
|
954
|
|
955 ngx_http_file_cache_delete(cache, q, name);
|
|
956
|
|
957 break;
|
|
958 }
|
|
959
|
|
960 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
961
|
|
962 ngx_free(name);
|
|
963
|
|
964 return wait;
|
|
965 }
|
|
966
|
|
967
|
|
968 static time_t
|
|
969 ngx_http_file_cache_expire(ngx_http_file_cache_t *cache)
|
503
|
970 {
|
|
971 u_char *name, *p;
|
|
972 size_t len;
|
|
973 time_t now, wait;
|
|
974 ngx_path_t *path;
|
|
975 ngx_queue_t *q;
|
|
976 ngx_http_file_cache_node_t *fcn;
|
|
977 u_char key[2 * NGX_HTTP_CACHE_KEY_LEN];
|
|
978
|
|
979 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
980 "http file cache expire");
|
|
981
|
|
982 path = cache->path;
|
|
983 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
984
|
507
|
985 name = ngx_alloc(len + 1, ngx_cycle->log);
|
|
986 if (name == NULL) {
|
518
|
987 return 10;
|
507
|
988 }
|
|
989
|
|
990 ngx_memcpy(name, path->name.data, path->name.len);
|
|
991
|
503
|
992 now = ngx_time();
|
|
993
|
|
994 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
995
|
|
996 for ( ;; ) {
|
|
997
|
520
|
998 if (ngx_queue_empty(&cache->sh->queue)) {
|
518
|
999 wait = 10;
|
503
|
1000 break;
|
|
1001 }
|
|
1002
|
520
|
1003 q = ngx_queue_last(&cache->sh->queue);
|
503
|
1004
|
|
1005 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
|
|
1006
|
507
|
1007 wait = fcn->expire - now;
|
503
|
1008
|
507
|
1009 if (wait > 0) {
|
518
|
1010 wait = wait > 10 ? 10 : wait;
|
507
|
1011 break;
|
503
|
1012 }
|
|
1013
|
|
1014 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1015 "http file cache expire: #%d %d %02xd%02xd%02xd%02xd",
|
|
1016 fcn->count, fcn->exists,
|
|
1017 fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);
|
|
1018
|
|
1019 if (fcn->count) {
|
|
1020
|
507
|
1021 p = ngx_hex_dump(key, (u_char *) &fcn->node.key,
|
|
1022 sizeof(ngx_rbtree_key_t));
|
503
|
1023
|
507
|
1024 len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
|
|
1025 (void) ngx_hex_dump(p, fcn->key, len);
|
503
|
1026
|
507
|
1027 /*
|
|
1028 * abnormally exited workers may leave locked cache entries,
|
|
1029 * and although it may be safe to remove them completely,
|
|
1030 * we prefer to remove them from inactive queue and rbtree
|
|
1031 * only, and to allow other leaks
|
|
1032 */
|
503
|
1033
|
507
|
1034 ngx_queue_remove(q);
|
520
|
1035 ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
|
503
|
1036
|
507
|
1037 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
|
505
|
1038 "ignore long locked inactive cache entry %*s, count:%d",
|
|
1039 2 * NGX_HTTP_CACHE_KEY_LEN, key, fcn->count);
|
503
|
1040
|
507
|
1041 continue;
|
503
|
1042 }
|
|
1043
|
|
1044 if (!fcn->exists) {
|
|
1045
|
|
1046 ngx_queue_remove(q);
|
520
|
1047 ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
|
503
|
1048 ngx_slab_free_locked(cache->shpool, fcn);
|
|
1049
|
|
1050 continue;
|
|
1051 }
|
|
1052
|
507
|
1053 ngx_http_file_cache_delete(cache, q, name);
|
503
|
1054 }
|
|
1055
|
|
1056 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1057
|
507
|
1058 ngx_free(name);
|
|
1059
|
503
|
1060 return wait;
|
|
1061 }
|
|
1062
|
|
1063
|
507
|
1064 static void
|
|
1065 ngx_http_file_cache_delete(ngx_http_file_cache_t *cache, ngx_queue_t *q,
|
|
1066 u_char *name)
|
|
1067 {
|
|
1068 u_char *p;
|
|
1069 size_t len;
|
|
1070 ngx_path_t *path;
|
|
1071 ngx_http_file_cache_node_t *fcn;
|
|
1072
|
|
1073 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
|
|
1074
|
520
|
1075 cache->sh->size -= (fcn->length + cache->bsize - 1) / cache->bsize;
|
507
|
1076
|
|
1077 path = cache->path;
|
|
1078
|
|
1079 p = name + path->name.len + 1 + path->len;
|
|
1080
|
|
1081 p = ngx_hex_dump(p, (u_char *) &fcn->node.key, sizeof(ngx_rbtree_key_t));
|
|
1082
|
|
1083 len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
|
|
1084 p = ngx_hex_dump(p, fcn->key, len);
|
|
1085 *p = '\0';
|
|
1086
|
|
1087 ngx_queue_remove(q);
|
|
1088
|
520
|
1089 ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
|
507
|
1090
|
|
1091 ngx_slab_free_locked(cache->shpool, fcn);
|
|
1092
|
|
1093 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1094
|
|
1095 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
1096
|
|
1097 ngx_create_hashed_filename(path, name, len);
|
|
1098
|
|
1099 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1100 "http file cache expire: \"%s\"", name);
|
|
1101
|
|
1102 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
|
|
1103 ngx_log_error(NGX_LOG_CRIT, ngx_cycle->log, ngx_errno,
|
|
1104 ngx_delete_file_n " \"%s\" failed", name);
|
|
1105 }
|
|
1106
|
|
1107 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1108 }
|
|
1109
|
|
1110
|
|
1111 static time_t
|
|
1112 ngx_http_file_cache_manager(void *data)
|
503
|
1113 {
|
|
1114 ngx_http_file_cache_t *cache = data;
|
|
1115
|
554
|
1116 off_t size;
|
|
1117 time_t next;
|
503
|
1118
|
507
|
1119 next = ngx_http_file_cache_expire(cache);
|
|
1120
|
|
1121 cache->last = ngx_current_msec;
|
|
1122 cache->files = 0;
|
|
1123
|
|
1124 for ( ;; ) {
|
|
1125 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1126
|
520
|
1127 size = cache->sh->size;
|
503
|
1128
|
507
|
1129 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1130
|
509
|
1131 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1132 "http file cache size: %O", size);
|
|
1133
|
507
|
1134 if (size < cache->max_size) {
|
|
1135 return next;
|
|
1136 }
|
503
|
1137
|
507
|
1138 next = ngx_http_file_cache_forced_expire(cache);
|
|
1139
|
|
1140 if (ngx_http_file_cache_manager_sleep(cache) != NGX_OK) {
|
|
1141 return next;
|
|
1142 }
|
|
1143 }
|
503
|
1144 }
|
|
1145
|
|
1146
|
554
|
1147 static void
|
|
1148 ngx_http_file_cache_loader(void *data)
|
|
1149 {
|
|
1150 ngx_http_file_cache_t *cache = data;
|
|
1151
|
|
1152 ngx_tree_ctx_t tree;
|
|
1153
|
|
1154 if (!cache->sh->cold || cache->sh->loading) {
|
|
1155 return;
|
|
1156 }
|
|
1157
|
|
1158 if (!ngx_atomic_cmp_set(&cache->sh->loading, 0, ngx_pid)) {
|
|
1159 return;
|
|
1160 }
|
|
1161
|
|
1162 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1163 "http file cache loader");
|
|
1164
|
|
1165 tree.init_handler = NULL;
|
|
1166 tree.file_handler = ngx_http_file_cache_manage_file;
|
|
1167 tree.pre_tree_handler = ngx_http_file_cache_noop;
|
|
1168 tree.post_tree_handler = ngx_http_file_cache_noop;
|
|
1169 tree.spec_handler = ngx_http_file_cache_delete_file;
|
|
1170 tree.data = cache;
|
|
1171 tree.alloc = 0;
|
|
1172 tree.log = ngx_cycle->log;
|
|
1173
|
|
1174 cache->last = ngx_current_msec;
|
|
1175 cache->files = 0;
|
|
1176
|
|
1177 if (ngx_walk_tree(&tree, &cache->path->name) == NGX_ABORT) {
|
|
1178 cache->sh->loading = 0;
|
|
1179 return;
|
|
1180 }
|
|
1181
|
|
1182 cache->sh->cold = 0;
|
|
1183 cache->sh->loading = 0;
|
|
1184
|
|
1185 ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
|
|
1186 "http file cache: %V %.3fM, bsize: %uz",
|
|
1187 &cache->path->name,
|
|
1188 ((double) cache->sh->size * cache->bsize) / (1024 * 1024),
|
|
1189 cache->bsize);
|
|
1190 }
|
|
1191
|
|
1192
|
503
|
1193 static ngx_int_t
|
507
|
1194 ngx_http_file_cache_manager_sleep(ngx_http_file_cache_t *cache)
|
|
1195 {
|
|
1196 ngx_msec_t elapsed;
|
|
1197
|
|
1198 if (cache->files++ > 100) {
|
|
1199
|
|
1200 ngx_time_update(0, 0);
|
|
1201
|
|
1202 elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
|
|
1203
|
|
1204 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1205 "http file cache manager time: %M", elapsed);
|
|
1206
|
|
1207 if (elapsed > 200) {
|
|
1208
|
|
1209 /*
|
|
1210 * if processing 100 files takes more than 200ms,
|
|
1211 * it seems that many operations require disk i/o,
|
|
1212 * therefore sleep 200ms
|
|
1213 */
|
|
1214
|
|
1215 ngx_msleep(200);
|
|
1216
|
|
1217 ngx_time_update(0, 0);
|
|
1218 }
|
|
1219
|
|
1220 cache->last = ngx_current_msec;
|
|
1221 cache->files = 0;
|
|
1222 }
|
|
1223
|
|
1224 return (ngx_quit || ngx_terminate) ? NGX_ABORT : NGX_OK;
|
|
1225 }
|
|
1226
|
|
1227
|
|
1228 static ngx_int_t
|
|
1229 ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
503
|
1230 {
|
|
1231 return NGX_OK;
|
|
1232 }
|
|
1233
|
|
1234
|
|
1235 static ngx_int_t
|
507
|
1236 ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
503
|
1237 {
|
507
|
1238 ngx_http_file_cache_t *cache;
|
|
1239
|
|
1240 cache = ctx->data;
|
|
1241
|
|
1242 if (ngx_http_file_cache_add_file(ctx, path) != NGX_OK) {
|
|
1243 (void) ngx_http_file_cache_delete_file(ctx, path);
|
|
1244 }
|
|
1245
|
|
1246 return ngx_http_file_cache_manager_sleep(cache);
|
|
1247 }
|
|
1248
|
503
|
1249
|
507
|
1250 static ngx_int_t
|
|
1251 ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name)
|
|
1252 {
|
|
1253 u_char *p;
|
|
1254 ngx_fd_t fd;
|
|
1255 ngx_int_t n;
|
|
1256 ngx_uint_t i;
|
|
1257 ngx_file_info_t fi;
|
|
1258 ngx_http_cache_t c;
|
|
1259 ngx_http_file_cache_t *cache;
|
|
1260 ngx_http_file_cache_header_t h;
|
|
1261
|
|
1262 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
|
|
1263 return NGX_ERROR;
|
503
|
1264 }
|
|
1265
|
507
|
1266 ngx_memzero(&c, sizeof(ngx_http_cache_t));
|
|
1267
|
|
1268 fd = ngx_open_file(name->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
|
|
1269
|
|
1270 if (fd == NGX_INVALID_FILE) {
|
|
1271 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
|
|
1272 ngx_open_file_n " \"%s\" failed", name->data);
|
|
1273 return NGX_ERROR;
|
|
1274 }
|
|
1275
|
|
1276 c.file.fd = fd;
|
|
1277 c.file.name = *name;
|
|
1278 c.file.log = ctx->log;
|
|
1279
|
|
1280 n = ngx_read_file(&c.file, (u_char *) &h,
|
|
1281 sizeof(ngx_http_file_cache_header_t), 0);
|
|
1282 if (n == NGX_ERROR) {
|
|
1283 return NGX_ERROR;
|
|
1284 }
|
|
1285
|
|
1286 if ((size_t) n < sizeof(ngx_http_file_cache_header_t)) {
|
|
1287 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
|
|
1288 "cache file \"%s\" is too small", name->data);
|
|
1289 return NGX_ERROR;
|
|
1290 }
|
|
1291
|
|
1292 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
|
|
1293 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
|
|
1294 ngx_fd_info_n " \"%s\" failed", name->data);
|
|
1295
|
|
1296 } else {
|
|
1297 c.uniq = ngx_file_uniq(&fi);
|
|
1298 c.valid_sec = h.valid_sec;
|
|
1299 c.valid_msec = h.valid_msec;
|
|
1300 c.body_start = h.body_start;
|
|
1301 c.length = ngx_file_size(&fi);
|
|
1302 }
|
|
1303
|
|
1304 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
1305 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
|
|
1306 ngx_close_file_n " \"%s\" failed", name->data);
|
|
1307 }
|
|
1308
|
|
1309 if (c.body_start == 0) {
|
|
1310 return NGX_ERROR;
|
|
1311 }
|
|
1312
|
|
1313 p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN];
|
503
|
1314
|
|
1315 for (i = 0; i < NGX_HTTP_CACHE_KEY_LEN; i++) {
|
|
1316 n = ngx_hextoi(p, 2);
|
|
1317
|
|
1318 if (n == NGX_ERROR) {
|
507
|
1319 return NGX_ERROR;
|
503
|
1320 }
|
|
1321
|
|
1322 p += 2;
|
|
1323
|
507
|
1324 c.key[i] = (u_char) n;
|
503
|
1325 }
|
|
1326
|
|
1327 cache = ctx->data;
|
|
1328
|
507
|
1329 return ngx_http_file_cache_add(cache, &c);
|
|
1330 }
|
|
1331
|
|
1332
|
|
1333 static ngx_int_t
|
|
1334 ngx_http_file_cache_add(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
|
|
1335 {
|
|
1336 ngx_http_file_cache_node_t *fcn;
|
|
1337
|
503
|
1338 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1339
|
507
|
1340 fcn = ngx_http_file_cache_lookup(cache, c->key);
|
|
1341
|
|
1342 if (fcn == NULL) {
|
|
1343
|
|
1344 fcn = ngx_slab_alloc_locked(cache->shpool,
|
|
1345 sizeof(ngx_http_file_cache_node_t));
|
|
1346 if (fcn == NULL) {
|
|
1347 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1348 return NGX_ERROR;
|
|
1349 }
|
|
1350
|
|
1351 ngx_memcpy((u_char *) &fcn->node.key, c->key, sizeof(ngx_rbtree_key_t));
|
|
1352
|
|
1353 ngx_memcpy(fcn->key, &c->key[sizeof(ngx_rbtree_key_t)],
|
|
1354 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
|
|
1355
|
520
|
1356 ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
|
507
|
1357
|
|
1358 fcn->uses = 1;
|
|
1359 fcn->count = 0;
|
|
1360 fcn->valid_msec = c->valid_msec;
|
|
1361 fcn->error = 0;
|
|
1362 fcn->exists = 1;
|
|
1363 fcn->uniq = c->uniq;
|
|
1364 fcn->valid_sec = c->valid_sec;
|
|
1365 fcn->body_start = c->body_start;
|
|
1366 fcn->length = c->length;
|
|
1367
|
520
|
1368 cache->sh->size += (c->length + cache->bsize - 1) / cache->bsize;
|
507
|
1369
|
|
1370 } else {
|
|
1371 ngx_queue_remove(&fcn->queue);
|
|
1372 }
|
|
1373
|
|
1374 fcn->expire = ngx_time() + cache->inactive;
|
|
1375
|
520
|
1376 ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
|
503
|
1377
|
|
1378 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1379
|
507
|
1380 return NGX_OK;
|
|
1381 }
|
|
1382
|
503
|
1383
|
507
|
1384 static ngx_int_t
|
|
1385 ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
|
1386 {
|
503
|
1387 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
|
507
|
1388 "http file cache delete: \"%s\"", path->data);
|
503
|
1389
|
|
1390 if (ngx_delete_file(path->data) == NGX_FILE_ERROR) {
|
|
1391 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
|
|
1392 ngx_delete_file_n " \"%s\" failed", path->data);
|
|
1393 }
|
26
|
1394
|
0
|
1395 return NGX_OK;
|
|
1396 }
|
|
1397
|
|
1398
|
503
|
1399 time_t
|
|
1400 ngx_http_file_cache_valid(ngx_array_t *cache_valid, ngx_uint_t status)
|
0
|
1401 {
|
503
|
1402 ngx_uint_t i;
|
|
1403 ngx_http_cache_valid_t *valid;
|
0
|
1404
|
509
|
1405 if (cache_valid == NULL) {
|
|
1406 return 0;
|
|
1407 }
|
|
1408
|
503
|
1409 valid = cache_valid->elts;
|
|
1410 for (i = 0; i < cache_valid->nelts; i++) {
|
0
|
1411
|
503
|
1412 if (valid[i].status == 0) {
|
|
1413 return valid[i].valid;
|
0
|
1414 }
|
|
1415
|
503
|
1416 if (valid[i].status == status) {
|
|
1417 return valid[i].valid;
|
0
|
1418 }
|
503
|
1419 }
|
0
|
1420
|
503
|
1421 return 0;
|
0
|
1422 }
|
|
1423
|
|
1424
|
503
|
1425 char *
|
|
1426 ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1427 {
|
507
|
1428 off_t max_size;
|
503
|
1429 u_char *last, *p;
|
507
|
1430 time_t inactive;
|
503
|
1431 ssize_t size;
|
|
1432 ngx_str_t s, name, *value;
|
|
1433 ngx_uint_t i, n;
|
|
1434 ngx_http_file_cache_t *cache;
|
|
1435
|
|
1436 cache = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_cache_t));
|
|
1437 if (cache == NULL) {
|
|
1438 return NGX_CONF_ERROR;
|
|
1439 }
|
|
1440
|
|
1441 cache->path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
|
|
1442 if (cache->path == NULL) {
|
|
1443 return NGX_CONF_ERROR;
|
|
1444 }
|
|
1445
|
|
1446 inactive = 600;
|
|
1447
|
|
1448 name.len = 0;
|
|
1449 size = 0;
|
507
|
1450 max_size = NGX_MAX_OFF_T_VALUE;
|
503
|
1451
|
|
1452 value = cf->args->elts;
|
|
1453
|
|
1454 cache->path->name = value[1];
|
|
1455
|
|
1456 if (cache->path->name.data[cache->path->name.len - 1] == '/') {
|
|
1457 cache->path->name.len--;
|
|
1458 }
|
|
1459
|
|
1460 if (ngx_conf_full_name(cf->cycle, &cache->path->name, 0) != NGX_OK) {
|
|
1461 return NGX_CONF_ERROR;
|
|
1462 }
|
|
1463
|
|
1464 for (i = 2; i < cf->args->nelts; i++) {
|
|
1465
|
|
1466 if (ngx_strncmp(value[i].data, "levels=", 7) == 0) {
|
|
1467
|
|
1468 p = value[i].data + 7;
|
|
1469 last = value[i].data + value[i].len;
|
|
1470
|
512
|
1471 for (n = 0; n < 3 && p < last; n++) {
|
503
|
1472
|
512
|
1473 if (*p > '0' && *p < '3') {
|
503
|
1474
|
|
1475 cache->path->level[n] = *p++ - '0';
|
|
1476 cache->path->len += cache->path->level[n] + 1;
|
|
1477
|
|
1478 if (p == last) {
|
|
1479 break;
|
|
1480 }
|
|
1481
|
512
|
1482 if (*p++ == ':' && n < 2 && p != last) {
|
503
|
1483 continue;
|
|
1484 }
|
512
|
1485
|
|
1486 goto invalid_levels;
|
503
|
1487 }
|
|
1488
|
|
1489 goto invalid_levels;
|
|
1490 }
|
|
1491
|
|
1492 if (cache->path->len < 10 + 3) {
|
|
1493 continue;
|
|
1494 }
|
|
1495
|
|
1496 invalid_levels:
|
|
1497
|
|
1498 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1499 "invalid \"levels\" \"%V\"", &value[i]);
|
|
1500 return NGX_CONF_ERROR;
|
|
1501 }
|
|
1502
|
|
1503 if (ngx_strncmp(value[i].data, "keys_zone=", 10) == 0) {
|
|
1504
|
|
1505 name.data = value[i].data + 10;
|
|
1506
|
|
1507 p = (u_char *) ngx_strchr(name.data, ':');
|
|
1508
|
|
1509 if (p) {
|
520
|
1510 *p = '\0';
|
|
1511
|
503
|
1512 name.len = p - name.data;
|
|
1513
|
|
1514 p++;
|
|
1515
|
|
1516 s.len = value[i].data + value[i].len - p;
|
|
1517 s.data = p;
|
|
1518
|
|
1519 size = ngx_parse_size(&s);
|
|
1520 if (size > 8191) {
|
|
1521 continue;
|
|
1522 }
|
|
1523 }
|
|
1524
|
|
1525 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1526 "invalid keys zone size \"%V\"", &value[i]);
|
|
1527 return NGX_CONF_ERROR;
|
|
1528 }
|
|
1529
|
|
1530 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
|
|
1531
|
|
1532 s.len = value[i].len - 9;
|
|
1533 s.data = value[i].data + 9;
|
|
1534
|
|
1535 inactive = ngx_parse_time(&s, 1);
|
|
1536 if (inactive < 0) {
|
|
1537 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1538 "invalid inactive value \"%V\"", &value[i]);
|
|
1539 return NGX_CONF_ERROR;
|
|
1540 }
|
|
1541
|
|
1542 continue;
|
|
1543 }
|
|
1544
|
507
|
1545 if (ngx_strncmp(value[i].data, "max_size=", 9) == 0) {
|
503
|
1546
|
507
|
1547 s.len = value[i].len - 9;
|
|
1548 s.data = value[i].data + 9;
|
503
|
1549
|
507
|
1550 max_size = ngx_parse_offset(&s);
|
|
1551 if (max_size < 0) {
|
503
|
1552 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
507
|
1553 "invalid max_size value \"%V\"", &value[i]);
|
503
|
1554 return NGX_CONF_ERROR;
|
|
1555 }
|
|
1556
|
|
1557 continue;
|
|
1558 }
|
|
1559
|
|
1560 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1561 "invalid parameter \"%V\"", &value[i]);
|
|
1562 return NGX_CONF_ERROR;
|
|
1563 }
|
|
1564
|
|
1565 if (name.len == 0 || size == 0) {
|
|
1566 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1567 "\"%V\" must have \"keys_zone\" parameter",
|
|
1568 &cmd->name);
|
|
1569 return NGX_CONF_ERROR;
|
|
1570 }
|
|
1571
|
507
|
1572 cache->path->manager = ngx_http_file_cache_manager;
|
554
|
1573 cache->path->loader = ngx_http_file_cache_loader;
|
503
|
1574 cache->path->data = cache;
|
|
1575
|
|
1576 if (ngx_add_path(cf, &cache->path) != NGX_OK) {
|
|
1577 return NGX_CONF_ERROR;
|
|
1578 }
|
|
1579
|
|
1580 cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
|
|
1581 if (cache->shm_zone == NULL) {
|
|
1582 return NGX_CONF_ERROR;
|
|
1583 }
|
|
1584
|
|
1585 if (cache->shm_zone->data) {
|
|
1586 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1587 "duplicate zone \"%V\"", &name);
|
|
1588 return NGX_CONF_ERROR;
|
|
1589 }
|
|
1590
|
|
1591
|
|
1592 cache->shm_zone->init = ngx_http_file_cache_init;
|
|
1593 cache->shm_zone->data = cache;
|
|
1594
|
|
1595 cache->inactive = inactive;
|
507
|
1596 cache->max_size = max_size;
|
503
|
1597
|
|
1598 return NGX_CONF_OK;
|
|
1599 }
|
26
|
1600
|
|
1601
|
503
|
1602 char *
|
|
1603 ngx_http_file_cache_valid_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1604 void *conf)
|
0
|
1605 {
|
503
|
1606 char *p = conf;
|
0
|
1607
|
503
|
1608 time_t valid;
|
|
1609 ngx_str_t *value;
|
|
1610 ngx_uint_t i, n, status;
|
|
1611 ngx_array_t **a;
|
|
1612 ngx_http_cache_valid_t *v;
|
|
1613 static ngx_uint_t statuses[] = { 200, 301, 302 };
|
26
|
1614
|
503
|
1615 a = (ngx_array_t **) (p + cmd->offset);
|
0
|
1616
|
503
|
1617 if (*a == NGX_CONF_UNSET_PTR) {
|
|
1618 *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_cache_valid_t));
|
|
1619 if (*a == NULL) {
|
|
1620 return NGX_CONF_ERROR;
|
|
1621 }
|
|
1622 }
|
0
|
1623
|
503
|
1624 value = cf->args->elts;
|
|
1625 n = cf->args->nelts - 1;
|
0
|
1626
|
503
|
1627 valid = ngx_parse_time(&value[n], 1);
|
|
1628 if (valid < 0) {
|
|
1629 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1630 "invalid time value \"%V\"", &value[n]);
|
|
1631 return NGX_CONF_ERROR;
|
0
|
1632 }
|
|
1633
|
503
|
1634 if (n == 1) {
|
|
1635
|
|
1636 for (i = 0; i < 3; i++) {
|
|
1637 v = ngx_array_push(*a);
|
|
1638 if (v == NULL) {
|
|
1639 return NGX_CONF_ERROR;
|
|
1640 }
|
|
1641
|
|
1642 v->status = statuses[i];
|
|
1643 v->valid = valid;
|
|
1644 }
|
|
1645
|
|
1646 return NGX_CONF_OK;
|
0
|
1647 }
|
|
1648
|
503
|
1649 for (i = 1; i < n; i++) {
|
|
1650
|
|
1651 if (ngx_strcmp(value[i].data, "any") == 0) {
|
|
1652
|
|
1653 status = 0;
|
|
1654
|
|
1655 } else {
|
0
|
1656
|
503
|
1657 status = ngx_atoi(value[i].data, value[i].len);
|
|
1658 if (status < 100) {
|
|
1659 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1660 "invalid status \"%V\"", &value[i]);
|
|
1661 return NGX_CONF_ERROR;
|
|
1662 }
|
|
1663 }
|
|
1664
|
|
1665 v = ngx_array_push(*a);
|
|
1666 if (v == NULL) {
|
|
1667 return NGX_CONF_ERROR;
|
|
1668 }
|
|
1669
|
|
1670 v->status = status;
|
|
1671 v->valid = valid;
|
|
1672 }
|
|
1673
|
|
1674 return NGX_CONF_OK;
|
0
|
1675 }
|