2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-11 00:43:33 -05:00
|
|
|
# Cross-process locking using Redis.
|
|
|
|
class DistributedMutex
|
2019-02-25 10:34:58 -06:00
|
|
|
DEFAULT_VALIDITY ||= 60
|
2014-04-11 00:43:33 -05:00
|
|
|
|
2019-02-19 19:23:42 -06:00
|
|
|
def self.synchronize(key, redis: nil, validity: DEFAULT_VALIDITY, &blk)
|
2019-02-19 19:28:10 -06:00
|
|
|
self.new(
|
|
|
|
key,
|
|
|
|
redis: redis,
|
|
|
|
validity: validity
|
|
|
|
).synchronize(&blk)
|
2014-07-29 23:04:27 -05:00
|
|
|
end
|
|
|
|
|
2019-02-19 19:28:10 -06:00
|
|
|
def initialize(key, redis: nil, validity: DEFAULT_VALIDITY)
|
2014-04-11 00:43:33 -05:00
|
|
|
@key = key
|
2018-09-19 00:49:18 -05:00
|
|
|
@using_global_redis = true if !redis
|
2014-04-11 00:43:33 -05:00
|
|
|
@redis = redis || $redis
|
2014-04-13 19:51:46 -05:00
|
|
|
@mutex = Mutex.new
|
2019-02-19 19:28:10 -06:00
|
|
|
@validity = validity
|
2014-04-11 00:43:33 -05:00
|
|
|
end
|
|
|
|
|
2018-09-19 00:49:18 -05:00
|
|
|
CHECK_READONLY_ATTEMPT ||= 10
|
|
|
|
|
2014-04-13 19:51:46 -05:00
|
|
|
# NOTE wrapped in mutex to maintain its semantics
|
2019-02-19 19:28:10 -06:00
|
|
|
def synchronize
|
2019-08-01 03:12:05 -05:00
|
|
|
@mutex.synchronize do
|
|
|
|
expire_time = get_lock
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
current_time = redis.time[0]
|
|
|
|
unless current_time < expire_time
|
|
|
|
warn("held for too long")
|
|
|
|
end
|
|
|
|
|
|
|
|
unless unlock(expire_time)
|
|
|
|
warn("didn't unlock cleanly")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :key
|
|
|
|
attr_reader :redis
|
|
|
|
attr_reader :validity
|
|
|
|
|
|
|
|
def warn(msg)
|
|
|
|
Rails.logger.warn("DistributedMutex(#{key.inspect}): #{msg}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_lock
|
2018-09-19 00:49:18 -05:00
|
|
|
attempts = 0
|
|
|
|
|
2019-08-01 03:12:05 -05:00
|
|
|
while true
|
|
|
|
got_lock, expire_time = try_to_get_lock
|
|
|
|
if got_lock
|
|
|
|
return expire_time
|
|
|
|
end
|
|
|
|
|
2014-04-13 19:51:46 -05:00
|
|
|
sleep 0.001
|
2018-09-19 00:49:18 -05:00
|
|
|
# in readonly we will never be able to get a lock
|
2018-09-20 00:15:29 -05:00
|
|
|
if @using_global_redis && Discourse.recently_readonly?
|
2018-09-19 01:07:29 -05:00
|
|
|
attempts += 1
|
2019-02-19 19:23:42 -06:00
|
|
|
|
2018-09-19 01:07:29 -05:00
|
|
|
if attempts > CHECK_READONLY_ATTEMPT
|
|
|
|
raise Discourse::ReadOnly
|
|
|
|
end
|
2018-09-19 00:49:18 -05:00
|
|
|
end
|
2014-04-13 19:51:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-19 19:28:10 -06:00
|
|
|
def try_to_get_lock
|
2014-04-13 19:51:46 -05:00
|
|
|
got_lock = false
|
2019-02-19 19:23:42 -06:00
|
|
|
|
2019-08-01 03:12:05 -05:00
|
|
|
now = redis.time[0]
|
|
|
|
expire_time = now + validity
|
2019-02-19 19:23:42 -06:00
|
|
|
|
2019-08-01 03:12:05 -05:00
|
|
|
redis.watch key
|
|
|
|
|
|
|
|
current_expire_time = redis.get key
|
|
|
|
|
|
|
|
if current_expire_time.present? && current_expire_time.to_i > now
|
|
|
|
redis.unwatch
|
|
|
|
|
|
|
|
got_lock = false
|
|
|
|
else
|
|
|
|
result =
|
|
|
|
redis.multi do
|
|
|
|
redis.set key, expire_time.to_s
|
|
|
|
redis.expire key, validity
|
2014-04-11 00:43:33 -05:00
|
|
|
end
|
2019-08-01 03:12:05 -05:00
|
|
|
|
|
|
|
got_lock = result.present?
|
2014-04-11 00:43:33 -05:00
|
|
|
end
|
|
|
|
|
2019-08-01 03:12:05 -05:00
|
|
|
[got_lock, expire_time]
|
2014-04-11 00:43:33 -05:00
|
|
|
end
|
|
|
|
|
2019-08-01 03:12:05 -05:00
|
|
|
def unlock(expire_time)
|
|
|
|
redis.watch key
|
|
|
|
current_expire_time = redis.get key
|
|
|
|
|
|
|
|
if current_expire_time == expire_time.to_s
|
|
|
|
result =
|
|
|
|
redis.multi do
|
|
|
|
redis.del key
|
|
|
|
end
|
|
|
|
return result.present?
|
|
|
|
else
|
|
|
|
redis.unwatch
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2014-04-11 00:43:33 -05:00
|
|
|
end
|