mirror of
https://github.com/discourse/discourse.git
synced 2025-02-17 01:32:44 +08:00
DEV: Validity of distributed mutex configurable once per instance.
Follow up to 4f9e5e19c8
.
This commit is contained in:
parent
f2efa0da66
commit
4d31b425e3
|
@ -3,24 +3,29 @@ class DistributedMutex
|
||||||
DEFAULT_VALIDITY = 60
|
DEFAULT_VALIDITY = 60
|
||||||
|
|
||||||
def self.synchronize(key, redis: nil, validity: DEFAULT_VALIDITY, &blk)
|
def self.synchronize(key, redis: nil, validity: DEFAULT_VALIDITY, &blk)
|
||||||
self.new(key, redis).synchronize(validity: DEFAULT_VALIDITY, &blk)
|
self.new(
|
||||||
|
key,
|
||||||
|
redis: redis,
|
||||||
|
validity: validity
|
||||||
|
).synchronize(&blk)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(key, redis = nil)
|
def initialize(key, redis: nil, validity: DEFAULT_VALIDITY)
|
||||||
@key = key
|
@key = key
|
||||||
@using_global_redis = true if !redis
|
@using_global_redis = true if !redis
|
||||||
@redis = redis || $redis
|
@redis = redis || $redis
|
||||||
@mutex = Mutex.new
|
@mutex = Mutex.new
|
||||||
|
@validity = validity
|
||||||
end
|
end
|
||||||
|
|
||||||
CHECK_READONLY_ATTEMPT ||= 10
|
CHECK_READONLY_ATTEMPT ||= 10
|
||||||
|
|
||||||
# NOTE wrapped in mutex to maintain its semantics
|
# NOTE wrapped in mutex to maintain its semantics
|
||||||
def synchronize(validity: DEFAULT_VALIDITY)
|
def synchronize
|
||||||
@mutex.lock
|
@mutex.lock
|
||||||
attempts = 0
|
attempts = 0
|
||||||
|
|
||||||
while !try_to_get_lock(validity)
|
while !try_to_get_lock
|
||||||
sleep 0.001
|
sleep 0.001
|
||||||
# in readonly we will never be able to get a lock
|
# in readonly we will never be able to get a lock
|
||||||
if @using_global_redis && Discourse.recently_readonly?
|
if @using_global_redis && Discourse.recently_readonly?
|
||||||
|
@ -41,11 +46,11 @@ class DistributedMutex
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def try_to_get_lock(validity)
|
def try_to_get_lock
|
||||||
got_lock = false
|
got_lock = false
|
||||||
|
|
||||||
if @redis.setnx @key, Time.now.to_i + validity
|
if @redis.setnx @key, Time.now.to_i + @validity
|
||||||
@redis.expire @key, validity
|
@redis.expire @key, @validity
|
||||||
got_lock = true
|
got_lock = true
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
|
@ -54,7 +59,7 @@ class DistributedMutex
|
||||||
|
|
||||||
if time && time.to_i < Time.now.to_i
|
if time && time.to_i < Time.now.to_i
|
||||||
got_lock = @redis.multi do
|
got_lock = @redis.multi do
|
||||||
@redis.set @key, Time.now.to_i + validity
|
@redis.set @key, Time.now.to_i + @validity
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
|
|
|
@ -44,13 +44,15 @@ describe DistributedMutex do
|
||||||
it 'allows the validity of the lock to be configured' do
|
it 'allows the validity of the lock to be configured' do
|
||||||
freeze_time
|
freeze_time
|
||||||
|
|
||||||
mutex = DistributedMutex.new(key)
|
mutex = DistributedMutex.new(key, validity: 2)
|
||||||
|
|
||||||
mutex.synchronize(validity: 2) do
|
mutex.synchronize do
|
||||||
expect($redis.ttl(key)).to eq(2)
|
expect($redis.ttl(key)).to eq(2)
|
||||||
expect($redis.get(key).to_i).to eq(Time.now.to_i + 2)
|
expect($redis.get(key).to_i).to eq(Time.now.to_i + 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
mutex = DistributedMutex.new(key)
|
||||||
|
|
||||||
mutex.synchronize do
|
mutex.synchronize do
|
||||||
expect($redis.ttl(key)).to eq(DistributedMutex::DEFAULT_VALIDITY)
|
expect($redis.ttl(key)).to eq(DistributedMutex::DEFAULT_VALIDITY)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user