# HG changeset patch
# User Elliot Thomas <elliot.thomas@bbc.co.uk>
# Date 1544710101 0
#      Thu Dec 13 14:08:21 2018 +0000
# Node ID 3d235c1dee472f8daeeaf021e25a0a8ca3cecd12
# Parent  cbc5dee8d5d2085bbfe30168487479d54006b4c7
Apply cache locking behaviour to stale cache entries.

Previously, this was only done for new cache entries.

diff -r cbc5dee8d5d2 -r 3d235c1dee47 src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h	Tue Dec 11 19:41:22 2018 +0300
+++ b/src/http/ngx_http_cache.h	Thu Dec 13 14:08:21 2018 +0000
@@ -188,6 +188,7 @@
 ngx_int_t ngx_http_file_cache_create(ngx_http_request_t *r);
 void ngx_http_file_cache_create_key(ngx_http_request_t *r);
 ngx_int_t ngx_http_file_cache_open(ngx_http_request_t *r);
+ngx_int_t ngx_http_file_cache_lock(ngx_http_request_t *r);
 ngx_int_t ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf);
 void ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf);
 void ngx_http_file_cache_update_header(ngx_http_request_t *r);
diff -r cbc5dee8d5d2 -r 3d235c1dee47 src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c	Tue Dec 11 19:41:22 2018 +0300
+++ b/src/http/ngx_http_file_cache.c	Thu Dec 13 14:08:21 2018 +0000
@@ -11,8 +11,6 @@
 #include <ngx_md5.h>
 
 
-static ngx_int_t ngx_http_file_cache_lock(ngx_http_request_t *r,
-    ngx_http_cache_t *c);
 static void ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev);
 static void ngx_http_file_cache_lock_wait(ngx_http_request_t *r,
     ngx_http_cache_t *c);
@@ -340,7 +338,7 @@
     }
 
     if (!test) {
-        goto done;
+        return rv;
     }
 
     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
@@ -364,7 +362,7 @@
 
         case NGX_ENOENT:
         case NGX_ENOTDIR:
-            goto done;
+            return rv;
 
         default:
             ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
@@ -388,25 +386,20 @@
     }
 
     return ngx_http_file_cache_read(r, c);
-
-done:
-
-    if (rv == NGX_DECLINED) {
-        return ngx_http_file_cache_lock(r, c);
-    }
-
-    return rv;
 }
 
 
-static ngx_int_t
-ngx_http_file_cache_lock(ngx_http_request_t *r, ngx_http_cache_t *c)
+ngx_int_t
+ngx_http_file_cache_lock(ngx_http_request_t *r)
 {
     ngx_msec_t                 now, timer;
+    ngx_http_cache_t          *c;
     ngx_http_file_cache_t     *cache;
 
+    c = r->cache;
+
     if (!c->lock) {
-        return NGX_DECLINED;
+        return NGX_OK;
     }
 
     now = ngx_current_msec;
@@ -431,11 +424,11 @@
                    c->updating, c->wait_time);
 
     if (c->updating) {
-        return NGX_DECLINED;
+        return NGX_OK;
     }
 
     if (c->lock_timeout == 0) {
-        return NGX_HTTP_CACHE_SCARCE;
+        return NGX_BUSY;
     }
 
     c->waiting = 1;
diff -r cbc5dee8d5d2 -r 3d235c1dee47 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Tue Dec 11 19:41:22 2018 +0300
+++ b/src/http/ngx_http_upstream.c	Thu Dec 13 14:08:21 2018 +0000
@@ -981,6 +981,19 @@
 
     r->cached = 0;
 
+    if (u->cacheable) {
+        switch (ngx_http_file_cache_lock(r)) {
+
+        case NGX_AGAIN:
+
+            return NGX_BUSY;
+
+        case NGX_BUSY:
+
+            u->cacheable = 0;
+        }
+    }
+
     return NGX_DECLINED;
 }
 
