diff options
author | Claire <claire.github-309c@sitedethib.com> | 2022-05-16 09:42:32 +0200 |
---|---|---|
committer | Claire <claire.github-309c@sitedethib.com> | 2022-05-16 09:42:32 +0200 |
commit | 040b7d37a460e5f023f7654b1b619368cbbc24ea (patch) | |
tree | 251a14cd362a6d98c1721e8f3f54fc386165e464 /app/models/concerns | |
parent | 94e98864e39c010635e839fea984f2b4893bef1a (diff) | |
parent | c3fac61f56b3ad63534961f3d3c426cdf8ac6213 (diff) |
Merge branch 'main' into glitch-soc/merge-upstream
Conflicts: - `app/services/remove_status_service.rb`: Conflict due to glitch-soc having extra code for a proper direct visibility timeline, in a part of the code upstream refactored. Restored glitch-soc's extra code in the refactored bit.
Diffstat (limited to 'app/models/concerns')
-rw-r--r-- | app/models/concerns/lockable.rb | 19 | ||||
-rw-r--r-- | app/models/concerns/redisable.rb | 8 |
2 files changed, 23 insertions, 4 deletions
diff --git a/app/models/concerns/lockable.rb b/app/models/concerns/lockable.rb new file mode 100644 index 000000000..55a9714ca --- /dev/null +++ b/app/models/concerns/lockable.rb @@ -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 diff --git a/app/models/concerns/redisable.rb b/app/models/concerns/redisable.rb index 8d76b6b82..0dad3abb2 100644 --- a/app/models/concerns/redisable.rb +++ b/app/models/concerns/redisable.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true module Redisable - extend ActiveSupport::Concern - - private - def redis Thread.current[:redis] ||= RedisConfiguration.pool.checkout end + + def with_redis(&block) + RedisConfiguration.with(&block) + end end |