[PATCH] proxy_cache_lock: Make lock timer configurable

Nejc Lovrencic nejc.lovrencic at gmail.com
Thu Mar 21 07:30:39 UTC 2024


# HG changeset patch
# User Nejc Lovrencic <nejc.lovrencic at gmail.com>
# Date 1711005111 -3600
#      Thu Mar 21 08:11:51 2024 +0100
# Node ID 8d38e6642e82bb219bb5b586f1dcca5222a036e8
# Parent  89bff782528a91ad123b63b624f798e6fd9c8e68
proxy_cache_lock: Make lock timer configurable

Default timer is set to 500ms. This in a worst-case scenario adds 500ms latency to MISS requests. This commit adds proxy_cache_lock_wait_time directive, which makes the timer configurable.

diff -r 89bff782528a -r 8d38e6642e82 src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c	Wed Feb 14 20:03:00 2024 +0400
+++ b/src/http/modules/ngx_http_proxy_module.c	Thu Mar 21 08:11:51 2024 +0100
@@ -592,6 +592,12 @@
       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_age),
       NULL },
 
+    { ngx_string("proxy_cache_lock_wait_time"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_wait_time),
+      NULL },
     { ngx_string("proxy_cache_revalidate"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
       ngx_conf_set_flag_slot,
@@ -3390,6 +3396,7 @@
     conf->upstream.cache_lock = NGX_CONF_UNSET;
     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
+    conf->upstream.cache_lock_wait_time = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
     conf->upstream.cache_convert_head = NGX_CONF_UNSET;
     conf->upstream.cache_background_update = NGX_CONF_UNSET;
@@ -3705,6 +3712,9 @@
     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
                               prev->upstream.cache_lock_age, 5000);
 
+    ngx_conf_merge_msec_value(conf->upstream.cache_lock_wait_time,
+                              prev->upstream.cache_lock_wait_time, 500);
+
     ngx_conf_merge_value(conf->upstream.cache_revalidate,
                               prev->upstream.cache_revalidate, 0);
 
diff -r 89bff782528a -r 8d38e6642e82 src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h	Wed Feb 14 20:03:00 2024 +0400
+++ b/src/http/ngx_http_cache.h	Thu Mar 21 08:11:51 2024 +0100
@@ -103,6 +103,7 @@
     ngx_msec_t                       lock_timeout;
     ngx_msec_t                       lock_age;
     ngx_msec_t                       lock_time;
+    ngx_msec_t                       lock_wait_time;
     ngx_msec_t                       wait_time;
 
     ngx_event_t                      wait_event;
diff -r 89bff782528a -r 8d38e6642e82 src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c	Wed Feb 14 20:03:00 2024 +0400
+++ b/src/http/ngx_http_file_cache.c	Thu Mar 21 08:11:51 2024 +0100
@@ -452,7 +452,10 @@
 
     timer = c->wait_time - now;
 
-    ngx_add_timer(&c->wait_event, (timer > 500) ? 500 : timer);
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http file cache lock timer tm:%M lwt:%M", timer, c->lock_wait_time);
+
+    ngx_add_timer(&c->wait_event, (timer > c->lock_wait_time) ? c->lock_wait_time : timer);
 
     r->main->blocked++;
 
@@ -531,7 +534,7 @@
     ngx_shmtx_unlock(&cache->shpool->mutex);
 
     if (wait) {
-        ngx_add_timer(&c->wait_event, (timer > 500) ? 500 : timer);
+        ngx_add_timer(&c->wait_event, (timer > c->lock_wait_time) ? c->lock_wait_time : timer);
         return NGX_AGAIN;
     }
 
diff -r 89bff782528a -r 8d38e6642e82 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Wed Feb 14 20:03:00 2024 +0400
+++ b/src/http/ngx_http_upstream.c	Thu Mar 21 08:11:51 2024 +0100
@@ -894,6 +894,7 @@
         c->lock = u->conf->cache_lock;
         c->lock_timeout = u->conf->cache_lock_timeout;
         c->lock_age = u->conf->cache_lock_age;
+        c->lock_wait_time = u->conf->cache_lock_wait_time;
 
         u->cache_status = NGX_HTTP_CACHE_MISS;
     }
diff -r 89bff782528a -r 8d38e6642e82 src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h	Wed Feb 14 20:03:00 2024 +0400
+++ b/src/http/ngx_http_upstream.h	Thu Mar 21 08:11:51 2024 +0100
@@ -204,6 +204,7 @@
     ngx_flag_t                       cache_lock;
     ngx_msec_t                       cache_lock_timeout;
     ngx_msec_t                       cache_lock_age;
+    ngx_msec_t                       cache_lock_wait_time;
 
     ngx_flag_t                       cache_revalidate;
     ngx_flag_t                       cache_convert_head;


More information about the nginx-devel mailing list