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_helpers | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'lib/gitlab/exclusive_lease_helpers')
-rw-r--r-- | lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb b/lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb new file mode 100644 index 00000000000..8213c9bc042 --- /dev/null +++ b/lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +module Gitlab + module ExclusiveLeaseHelpers + # Wrapper around ExclusiveLease that adds retry logic + class SleepingLock + delegate :cancel, to: :@lease + + def initialize(key, timeout:, delay:) + @lease = ::Gitlab::ExclusiveLease.new(key, timeout: timeout) + @delay = delay + @attempts = 0 + end + + def obtain(max_attempts) + until held? + raise FailedToObtainLockError, 'Failed to obtain a lock' if attempts >= max_attempts + + sleep(sleep_sec) unless first_attempt? + try_obtain + end + end + + def retried? + attempts > 1 + end + + private + + attr_reader :delay, :attempts + + def held? + @uuid.present? + end + + def try_obtain + @uuid ||= @lease.try_obtain + @attempts += 1 + end + + def first_attempt? + attempts.zero? + end + + def sleep_sec + delay.respond_to?(:call) ? delay.call(attempts) : delay + end + end + end +end |