Class: Gitlab::ExclusiveLeaseHelpers::SleepingLock
- Inherits:
-
Object
- Object
- Gitlab::ExclusiveLeaseHelpers::SleepingLock
- Defined in:
- lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb
Overview
Wrapper around ExclusiveLease that adds retry logic
Constant Summary collapse
- MAX_ATTEMPTS =
65
- DEFAULT_ATTEMPTS =
10
Instance Method Summary collapse
-
#initialize(key, timeout:, delay:) ⇒ SleepingLock
constructor
A new instance of SleepingLock.
- #obtain(max_attempts = DEFAULT_ATTEMPTS) ⇒ Object
- #retried? ⇒ Boolean
Constructor Details
#initialize(key, timeout:, delay:) ⇒ SleepingLock
Returns a new instance of SleepingLock.
11 12 13 14 15 |
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 11 def initialize(key, timeout:, delay:) @lease = ::Gitlab::ExclusiveLease.new(key, timeout: timeout) @delay = delay @attempts = 0 end |
Instance Method Details
#obtain(max_attempts = DEFAULT_ATTEMPTS) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 17 def obtain(max_attempts = DEFAULT_ATTEMPTS) until held? raise FailedToObtainLockError, 'Failed to obtain a lock' if attempts >= [max_attempts, MAX_ATTEMPTS].min sleep(sleep_sec) unless first_attempt? try_obtain end end |
#retried? ⇒ Boolean
26 27 28 |
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 26 def retried? attempts > 1 end |