comparison src/http/ngx_http_file_cache.c @ 5905:2f7e557eab5b

Cache: proxy_cache_lock_age and friends. Once this age is reached, the cache lock is discarded and another request can acquire the lock. Requests which failed to acquire the lock are not allowed to cache the response.
author Roman Arutyunyan <arut@nginx.com>
date Tue, 18 Nov 2014 20:41:12 +0300
parents f0af7ba616d8
children 8dfee01ff0bd
comparison
equal deleted inserted replaced
5904:abb466a57a22 5905:2f7e557eab5b
394 394
395 if (!c->lock) { 395 if (!c->lock) {
396 return NGX_DECLINED; 396 return NGX_DECLINED;
397 } 397 }
398 398
399 now = ngx_current_msec;
400
399 cache = c->file_cache; 401 cache = c->file_cache;
400 402
401 ngx_shmtx_lock(&cache->shpool->mutex); 403 ngx_shmtx_lock(&cache->shpool->mutex);
402 404
403 if (!c->node->updating) { 405 timer = c->node->lock_time - now;
406
407 if (!c->node->updating || (ngx_msec_int_t) timer <= 0) {
404 c->node->updating = 1; 408 c->node->updating = 1;
409 c->node->lock_time = now + c->lock_age;
405 c->updating = 1; 410 c->updating = 1;
411 c->lock_time = c->node->lock_time;
406 } 412 }
407 413
408 ngx_shmtx_unlock(&cache->shpool->mutex); 414 ngx_shmtx_unlock(&cache->shpool->mutex);
409 415
410 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 416 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
413 419
414 if (c->updating) { 420 if (c->updating) {
415 return NGX_DECLINED; 421 return NGX_DECLINED;
416 } 422 }
417 423
424 if (c->lock_timeout == 0) {
425 return NGX_HTTP_CACHE_SCARCE;
426 }
427
418 c->waiting = 1; 428 c->waiting = 1;
419
420 now = ngx_current_msec;
421 429
422 if (c->wait_time == 0) { 430 if (c->wait_time == 0) {
423 c->wait_time = now + c->lock_timeout; 431 c->wait_time = now + c->lock_timeout;
424 432
425 c->wait_event.handler = ngx_http_file_cache_lock_wait_handler; 433 c->wait_event.handler = ngx_http_file_cache_lock_wait_handler;
439 447
440 static void 448 static void
441 ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev) 449 ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev)
442 { 450 {
443 ngx_uint_t wait; 451 ngx_uint_t wait;
444 ngx_msec_t timer; 452 ngx_msec_t now, timer;
445 ngx_http_cache_t *c; 453 ngx_http_cache_t *c;
446 ngx_http_request_t *r; 454 ngx_http_request_t *r;
447 ngx_http_file_cache_t *cache; 455 ngx_http_file_cache_t *cache;
448 456
449 r = ev->data; 457 r = ev->data;
450 c = r->cache; 458 c = r->cache;
451 459
460 now = ngx_current_msec;
461
452 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ev->log, 0, 462 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ev->log, 0,
453 "http file cache wait handler wt:%M cur:%M", 463 "http file cache wait handler wt:%M cur:%M",
454 c->wait_time, ngx_current_msec); 464 c->wait_time, now);
455 465
456 timer = c->wait_time - ngx_current_msec; 466 timer = c->wait_time - now;
457 467
458 if ((ngx_msec_int_t) timer <= 0) { 468 if ((ngx_msec_int_t) timer <= 0) {
459 ngx_log_error(NGX_LOG_INFO, ev->log, 0, "cache lock timeout"); 469 ngx_log_error(NGX_LOG_INFO, ev->log, 0, "cache lock timeout");
460 c->lock = 0; 470 c->lock_timeout = 0;
461 goto wakeup; 471 goto wakeup;
462 } 472 }
463 473
464 cache = c->file_cache; 474 cache = c->file_cache;
465 wait = 0; 475 wait = 0;
466 476
467 ngx_shmtx_lock(&cache->shpool->mutex); 477 ngx_shmtx_lock(&cache->shpool->mutex);
468 478
469 if (c->node->updating) { 479 timer = c->node->lock_time - now;
480
481 if (c->node->updating && (ngx_msec_int_t) timer > 0) {
470 wait = 1; 482 wait = 1;
471 } 483 }
472 484
473 ngx_shmtx_unlock(&cache->shpool->mutex); 485 ngx_shmtx_unlock(&cache->shpool->mutex);
474 486
586 rc = NGX_HTTP_CACHE_UPDATING; 598 rc = NGX_HTTP_CACHE_UPDATING;
587 599
588 } else { 600 } else {
589 c->node->updating = 1; 601 c->node->updating = 1;
590 c->updating = 1; 602 c->updating = 1;
603 c->lock_time = c->node->lock_time;
591 rc = NGX_HTTP_CACHE_STALE; 604 rc = NGX_HTTP_CACHE_STALE;
592 } 605 }
593 606
594 ngx_shmtx_unlock(&cache->shpool->mutex); 607 ngx_shmtx_unlock(&cache->shpool->mutex);
595 608
1451 ngx_shmtx_lock(&cache->shpool->mutex); 1464 ngx_shmtx_lock(&cache->shpool->mutex);
1452 1465
1453 fcn = c->node; 1466 fcn = c->node;
1454 fcn->count--; 1467 fcn->count--;
1455 1468
1456 if (c->updating) { 1469 if (c->updating && fcn->lock_time == c->lock_time) {
1457 fcn->updating = 0; 1470 fcn->updating = 0;
1458 } 1471 }
1459 1472
1460 if (c->error) { 1473 if (c->error) {
1461 fcn->error = c->error; 1474 fcn->error = c->error;