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

Instance Method Summary collapse

Constructor Details

#initialize(key, timeout:, delay:) ⇒ SleepingLock

Returns a new instance of SleepingLock.


9
10
11
12
13
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 9

def initialize(key, timeout:, delay:)
  @lease = ::Gitlab::ExclusiveLease.new(key, timeout: timeout)
  @delay = delay
  @attempts = 0
end

Instance Method Details

#obtain(max_attempts) ⇒ Object


15
16
17
18
19
20
21
22
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 15

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

#retried?Boolean

Returns:

  • (Boolean)

24
25
26
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 24

def retried?
  attempts > 1
end