Class: SenroUsecaser::RetryConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/senro_usecaser/retry_configuration.rb

Overview

Configuration for automatic retry behavior

This class defines when and how retries should occur based on error codes or exception classes, with configurable backoff strategies.

Examples:

Basic retry configuration

RetryConfiguration.new(
  matchers: [:network_error, Net::OpenTimeout],
  attempts: 3,
  wait: 1.0
)

With exponential backoff

RetryConfiguration.new(
  matchers: [:rate_limited],
  attempts: 5,
  wait: 2.0,
  backoff: :exponential,
  max_wait: 60
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matchers:, attempts: 3, wait: 0, backoff: :fixed, max_wait: nil, jitter: 0) ⇒ RetryConfiguration

Initializes a new retry configuration

rubocop:disable Metrics/ParameterLists : (matchers: Array[(Symbol | Class)], ?attempts: Integer, ?wait: (Float | Integer), : ?backoff: Symbol, ?max_wait: (Float | Integer)?, ?jitter: (Float | Integer)) -> void

Parameters:

  • matchers (Array<Symbol, Class>) —

    Error codes or exception classes to match

  • attempts (Integer) (defaults to: 3) —

    Maximum number of attempts (default: 3)

  • wait (Numeric) (defaults to: 0) —

    Base wait time in seconds (default: 0)

  • backoff (Symbol) (defaults to: :fixed) —

    Backoff strategy: :fixed, :linear, or :exponential (default: :fixed)

  • max_wait (Numeric) (defaults to: nil) —

    Maximum wait time in seconds (default: 3600)

  • jitter (Numeric) (defaults to: 0) —

    Jitter factor 0.0-1.0 to randomize wait times (default: 0)



63
64
65
66
67
68
69
70
71
# File 'lib/senro_usecaser/retry_configuration.rb', line 63

def initialize(matchers:, attempts: 3, wait: 0, backoff: :fixed, max_wait: nil, jitter: 0)
  # rubocop:enable Metrics/ParameterLists
  @matchers = matchers
  @attempts = attempts
  @wait = wait
  @backoff = backoff
  @max_wait = max_wait || 3600
  @jitter = jitter
end

Instance Attribute Details

#attempts ⇒ Object (readonly)

Returns the maximum number of attempts : () -> Integer



33
34
35
# File 'lib/senro_usecaser/retry_configuration.rb', line 33

def attempts
  @attempts
end

#backoff ⇒ Object (readonly)

Returns the backoff strategy (:fixed, :linear, :exponential) : () -> Symbol



41
42
43
# File 'lib/senro_usecaser/retry_configuration.rb', line 41

def backoff
  @backoff
end

#jitter ⇒ Object (readonly)

Returns the jitter factor (0.0 to 1.0) : () -> (Float | Integer)



49
50
51
# File 'lib/senro_usecaser/retry_configuration.rb', line 49

def jitter
  @jitter
end

#matchers ⇒ Object (readonly)

Returns the list of error matchers (Symbols for error codes, Classes for exceptions) : () -> Array[(Symbol | Class)]



29
30
31
# File 'lib/senro_usecaser/retry_configuration.rb', line 29

def matchers
  @matchers
end

#max_wait ⇒ Object (readonly)

Returns the maximum wait time in seconds : () -> (Float | Integer)



45
46
47
# File 'lib/senro_usecaser/retry_configuration.rb', line 45

def max_wait
  @max_wait
end

#wait ⇒ Object (readonly)

Returns the base wait time in seconds : () -> (Float | Integer)



37
38
39
# File 'lib/senro_usecaser/retry_configuration.rb', line 37

def wait
  @wait
end

Instance Method Details

#calculate_wait(attempt) ⇒ Object

Calculates the wait time for the given attempt number

: (Integer) -> Float



85
86
87
88
89
# File 'lib/senro_usecaser/retry_configuration.rb', line 85

def calculate_wait(attempt)
  base = calculate_base_wait(attempt)
  base = [base, @max_wait].min
  apply_jitter(base)
end

#matches?(result) ⇒ Boolean

Checks if this configuration matches the given result

: (Result) -> bool

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/senro_usecaser/retry_configuration.rb', line 76

def matches?(result)
  return false unless result.failure?

  result.errors.any? { |error| matches_error?(error) }
end