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