Skip to content

A platform for community discussion. Free, open, simple.

PERF: Add exponential backoff for DistributedMutex (#17886)

David Taylor2422ca0e6740
diff --git a/lib/distributed_mutex.rb b/lib/distributed_mutex.rb
index b724948d231..bc29d149512 100644
--- a/lib/distributed_mutex.rb
+++ b/lib/distributed_mutex.rb
@@ -4,7 +4,7 @@
# Expiration happens when the current time is greater than the expire time
class DistributedMutex
DEFAULT_VALIDITY = 60
- CHECK_READONLY_ATTEMPTS = 10
+ CHECK_READONLY_ATTEMPTS = 5
LOCK_SCRIPT = DiscourseRedis::EvalHelper.new <<~LUA
local now = redis.call("time")[1]
@@ -85,7 +85,9 @@ class DistributedMutex
return expire_time if expire_time
- sleep 0.001
+ # Exponential backoff, max duration 1s
+ interval = attempts < 10 ? (0.001 * 2**attempts) : 1
+ sleep interval
# in readonly we will never be able to get a lock
if @using_global_redis && Discourse.recently_readonly?