[PATCH] When cache file is expired, nginx can make a lot of upstream requests.

Sangdeuk Kwon sangdeuk.kwon at quantil.com
Wed Mar 25 07:05:37 UTC 2020


# HG changeset patch
# User Sangdeuk Kwon <sangdeuk.kwon at quantil.com>
# Date 1585118845 -32400
#      Wed Mar 25 15:47:25 2020 +0900
# Node ID fe5988cefe615bb0f2a8936221c8464a6664916e
# Parent  0cb942c1c1aa98118076e72e0b89940e85e6291c
When cache file is expired, nginx can make a lot of upstream requests.

In cache miss case, nginx makes only one upstream request
if "proxy_cache_lock" is "on".
When cache file is expired, nginx can make a lot of upstream requests
even though "proxy_cache_lock" is "on".

If origin's content is changed, this simultaneous upstream requests
can make "too long header" error log.

diff -r 0cb942c1c1aa -r fe5988cefe61 src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h Fri Mar 13 02:12:10 2020 +0300
+++ b/src/http/ngx_http_cache.h Wed Mar 25 15:47:25 2020 +0900
@@ -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_handle_updating(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 0cb942c1c1aa -r fe5988cefe61 src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c    Fri Mar 13 02:12:10 2020 +0300
+++ b/src/http/ngx_http_file_cache.c    Wed Mar 25 15:47:25 2020 +0900
@@ -522,6 +522,28 @@
 }


+ngx_int_t
+ngx_http_file_cache_handle_updating(ngx_http_request_t *r)
+{
+    ngx_http_cache_t          *c;
+    ngx_http_file_cache_t     *cache;
+    ngx_int_t                  updating;
+
+    c = r->cache;
+    cache = c->file_cache;
+
+    ngx_shmtx_lock(&cache->shpool->mutex);
+    updating = c->node->updating;
+    ngx_shmtx_unlock(&cache->shpool->mutex);
+
+    if (!updating) {
+        return NGX_OK;
+    }
+
+    return ngx_http_file_cache_lock(r, c);
+}
+
+
 static ngx_int_t
 ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c)
 {
diff -r 0cb942c1c1aa -r fe5988cefe61 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c  Fri Mar 13 02:12:10 2020 +0300
+++ b/src/http/ngx_http_upstream.c  Wed Mar 25 15:47:25 2020 +0900
@@ -906,6 +906,10 @@
             } else {
                 rc = NGX_ERROR;
             }
+        } else {
+            if (ngx_http_file_cache_handle_updating(r) == NGX_AGAIN) {
+                rc = NGX_BUSY;
+            }
         }

         break;
@@ -919,7 +923,11 @@
             rc = NGX_OK;

         } else {
-            rc = NGX_HTTP_CACHE_STALE;
+            if (ngx_http_file_cache_handle_updating(r) == NGX_AGAIN) {
+                rc = NGX_BUSY;
+            } else {
+                rc = NGX_HTTP_CACHE_STALE;
+            }
         }

         break;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20200325/e8cb3515/attachment.htm>


More information about the nginx-devel mailing list