mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 07:53:49 +08:00
FIX: in redis readonly raise an exception from DistributedMutex
If we detect redis is in readonly we can not correctly get a mutex raise an exception to notify caller When getting optimized images avoid the distributed mutex unless for some reason it is the first call and we need to generate a thumb In redis readonly no thumbnails will be generated
This commit is contained in:
@ -7,15 +7,26 @@ class DistributedMutex
|
||||
|
||||
def initialize(key, redis = nil)
|
||||
@key = key
|
||||
@using_global_redis = true if !redis
|
||||
@redis = redis || $redis
|
||||
@mutex = Mutex.new
|
||||
end
|
||||
|
||||
CHECK_READONLY_ATTEMPT ||= 10
|
||||
|
||||
# NOTE wrapped in mutex to maintain its semantics
|
||||
def synchronize
|
||||
|
||||
@mutex.lock
|
||||
attempts = 0
|
||||
|
||||
while !try_to_get_lock
|
||||
sleep 0.001
|
||||
attempts += 1
|
||||
# in readonly we will never be able to get a lock
|
||||
if @using_global_redis && attempts > CHECK_READONLY_ATTEMPT
|
||||
raise Discourse::ReadOnly
|
||||
end
|
||||
end
|
||||
|
||||
yield
|
||||
|
Reference in New Issue
Block a user