Welcome! Log In Create A New Profile

Advanced

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

Sangdeuk Kwon
March 25, 2020 03:06AM
# HG changeset patch
# User Sangdeuk Kwon <sangdeuk.kwon@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;
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

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

Sangdeuk Kwon 38 March 25, 2020 03:06AM

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

Maxim Dounin 12 March 25, 2020 08:32PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 81
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready