mirror of https://github.com/mastodon/mastodon
Refactor how Redis locks are created (#18400)
* Refactor how Redis locks are created * Fix autorelease duration on account deletion lockpull/18408/head
parent
12535568f7
commit
6cf57c6765
@ -0,0 +1,19 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Lockable
|
||||||
|
# @param [String] lock_name
|
||||||
|
# @param [ActiveSupport::Duration] autorelease Automatically release the lock after this time
|
||||||
|
# @param [Boolean] raise_on_failure Raise an error if a lock cannot be acquired, or fail silently
|
||||||
|
# @raise [Mastodon::RaceConditionError]
|
||||||
|
def with_lock(lock_name, autorelease: 15.minutes, raise_on_failure: true)
|
||||||
|
with_redis do |redis|
|
||||||
|
RedisLock.acquire(redis: redis, key: "lock:#{lock_name}", autorelease: autorelease.seconds) do |lock|
|
||||||
|
if lock.acquired?
|
||||||
|
yield
|
||||||
|
elsif raise_on_failure
|
||||||
|
raise Mastodon::RaceConditionError, "Could not acquire lock for #{lock_name}, try again later"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,11 +1,11 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Redisable
|
module Redisable
|
||||||
extend ActiveSupport::Concern
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def redis
|
def redis
|
||||||
Thread.current[:redis] ||= RedisConfiguration.pool.checkout
|
Thread.current[:redis] ||= RedisConfiguration.pool.checkout
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def with_redis(&block)
|
||||||
|
RedisConfiguration.with(&block)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue