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