Module: ActiveRecordDeadlockHandler::Backoff

Defined in:
lib/active_record_deadlock_handler/backoff.rb

Class Method Summary collapse

Class Method Details

.compute(attempt:, config:) ⇒ Float

Computes sleep duration for a given retry attempt using exponential backoff with jitter.

Formula: min(base_delay * multiplier^(attempt-1), max_delay) + jitter Jitter is a random fraction of the computed exponential delay to reduce thundering herd.

Parameters:

  • attempt (Integer)

    1-indexed retry attempt number

  • config (Configuration)

Returns:

  • (Float)

    seconds to sleep



13
14
15
16
17
# File 'lib/active_record_deadlock_handler/backoff.rb', line 13

def self.compute(attempt:, config:)
  exponential = [config.base_delay * (config.backoff_multiplier**(attempt - 1)), config.max_delay].min
  jitter = exponential * config.jitter_factor * rand
  exponential + jitter
end