Class: Gitlab::ExclusiveLeaseHelpers::SleepingLock

Inherits:
Object
  • Object
show all
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

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

Returns:

  • (Boolean)


26
27
28
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 26

def retried?
  attempts > 1
end