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