Class: OnlineMigrations::ExponentialLockRetrier

Inherits:
LockRetrier
  • Object
show all
Defined in:
lib/online_migrations/lock_retrier.rb

Overview

‘LockRetrier` implementation that uses exponential delay with jitter between tries and constant lock timeout for each try

Examples:

# This will attempt 30 retries starting with delay of 10ms between each unsuccessful try, increasing exponentially
# up to the maximum delay of 1 minute and 200ms set as lock timeout for each try:

config.retrier = OnlineMigrations::ConstantLockRetrier.new(attempts: 30,
    base_delay: 0.01.seconds, max_delay: 1.minute, lock_timeout: 0.2.seconds)

Instance Attribute Summary collapse

Attributes inherited from LockRetrier

#connection

Instance Method Summary collapse

Methods inherited from LockRetrier

#with_lock_retries

Constructor Details

#initialize(attempts:, base_delay:, max_delay:, lock_timeout: nil) ⇒ ExponentialLockRetrier

Create a new ExponentialLockRetrier instance

Parameters:

  • attempts (Integer)

    Maximum number of attempts

  • base_delay (Numeric)

    Base sleep time to calculate total sleep time after unsuccessful lock attempt (in seconds)

  • max_delay (Numeric)

    Maximum sleep time after unsuccessful lock attempt (in seconds)

  • lock_timeout (Numeric) (defaults to: nil)

    Database lock timeout value (in seconds)



197
198
199
200
201
202
203
# File 'lib/online_migrations/lock_retrier.rb', line 197

def initialize(attempts:, base_delay:, max_delay:, lock_timeout: nil)
  super()
  @attempts = attempts
  @base_delay = base_delay
  @max_delay = max_delay
  @lock_timeout = lock_timeout
end

Instance Attribute Details

#attemptsInteger (readonly)

LockRetrier API implementation

Returns:

  • (Integer)

    Number of retrying attempts

See Also:



188
189
190
# File 'lib/online_migrations/lock_retrier.rb', line 188

def attempts
  @attempts
end

Instance Method Details

#delay(attempt) ⇒ Numeric

LockRetrier API implementation

Returns:

  • (Numeric)

    Sleep time after unsuccessful lock attempt (in seconds)

See Also:



220
221
222
# File 'lib/online_migrations/lock_retrier.rb', line 220

def delay(attempt)
  (rand * [@max_delay, @base_delay * (2**(attempt - 1))].min).ceil
end

#lock_timeout(_attempt) ⇒ Numeric

LockRetrier API implementation

Returns:

  • (Numeric)

    Database lock timeout value (in seconds)

See Also:



210
211
212
# File 'lib/online_migrations/lock_retrier.rb', line 210

def lock_timeout(_attempt)
  @lock_timeout
end