diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /lib/gitlab/exclusive_lease.rb | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'lib/gitlab/exclusive_lease.rb')
-rw-r--r-- | lib/gitlab/exclusive_lease.rb | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/gitlab/exclusive_lease.rb b/lib/gitlab/exclusive_lease.rb index 425ef5d738a..1d2c1c69423 100644 --- a/lib/gitlab/exclusive_lease.rb +++ b/lib/gitlab/exclusive_lease.rb @@ -12,6 +12,9 @@ module Gitlab # ExclusiveLease. # class ExclusiveLease + PREFIX = 'gitlab:exclusive_lease' + NoKey = Class.new(ArgumentError) + LUA_CANCEL_SCRIPT = <<~EOS.freeze local key, uuid = KEYS[1], ARGV[1] if redis.call("get", key) == uuid then @@ -34,13 +37,21 @@ module Gitlab end def self.cancel(key, uuid) + return unless key.present? + Gitlab::Redis::SharedState.with do |redis| - redis.eval(LUA_CANCEL_SCRIPT, keys: [redis_shared_state_key(key)], argv: [uuid]) + redis.eval(LUA_CANCEL_SCRIPT, keys: [ensure_prefixed_key(key)], argv: [uuid]) end end def self.redis_shared_state_key(key) - "gitlab:exclusive_lease:#{key}" + "#{PREFIX}:#{key}" + end + + def self.ensure_prefixed_key(key) + raise NoKey unless key.present? + + key.start_with?(PREFIX) ? key : redis_shared_state_key(key) end # Removes any existing exclusive_lease from redis @@ -94,6 +105,11 @@ module Gitlab ttl if ttl.positive? end end + + # Gives up this lease, allowing it to be obtained by others. + def cancel + self.class.cancel(@redis_shared_state_key, @uuid) + end end end |