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