From bd24118212f3be6b5acfa86bcb807c62aad7f2cb Mon Sep 17 00:00:00 2001 From: Remi Tricot-Le Breton Date: Thu, 16 Nov 2023 17:38:18 +0100 Subject: [PATCH] MEDIUM: cache: Use rdlock on cache in cache_use When looking for a valid entry in the cache tree in http_action_req_cache_use, we do not need to delete an expired entry at once because even if an expired entry exists, since the request will be forwarded to the server, then the expired entry will be overwritten when the updated response is seen. We can then use a simpler rdlock during cache_use operation. --- src/cache.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cache.c b/src/cache.c index 6d8f43613..f191cc492 100644 --- a/src/cache.c +++ b/src/cache.c @@ -1851,7 +1851,7 @@ enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *p _HA_ATOMIC_INC(&px->be_counters.p.http.cache_lookups); shctx_lock(shctx_ptr(cache)); - cache_wrlock(cache); + cache_rdlock(cache); res = entry_exist(cache, s->txn->cache_hash, 0); /* We must not use an entry that is not complete but the check will be * performed after we look for a potential secondary entry (in case of @@ -1860,7 +1860,7 @@ enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *p struct appctx *appctx; entry_block = block_ptr(res); shctx_row_inc_hot(shctx_ptr(cache), entry_block); - cache_wrunlock(cache); + cache_rdunlock(cache); shctx_unlock(shctx_ptr(cache)); /* In case of Vary, we could have multiple entries with the same @@ -1869,7 +1869,7 @@ enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *p if (res->secondary_key_signature) { if (!http_request_build_secondary_key(s, res->secondary_key_signature)) { shctx_lock(shctx_ptr(cache)); - cache_wrlock(cache); + cache_rdlock(cache); sec_entry = secondary_entry_exist(cache, res, s->txn->cache_secondary_hash, 0); if (sec_entry && sec_entry != res) { @@ -1879,7 +1879,7 @@ enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *p shctx_row_inc_hot(shctx_ptr(cache), entry_block); } res = sec_entry; - cache_wrunlock(cache); + cache_rdunlock(cache); shctx_unlock(shctx_ptr(cache)); } else @@ -1922,7 +1922,7 @@ enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *p return ACT_RET_CONT; } } - cache_wrunlock(cache); + cache_rdunlock(cache); shctx_unlock(shctx_ptr(cache)); /* Shared context does not need to be locked while we calculate the -- 2.47.3