Class: Shikibu::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/shikibu/retry_policy.rb

Overview

Configuration for retry behavior on activity failures

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_attempts: 5, base_delay: 1.0, max_delay: 60.0, backoff_coefficient: 2.0, max_duration: 300.0, retryable_errors: [StandardError], non_retryable_errors: []) ⇒ RetryPolicy



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shikibu/retry_policy.rb', line 16

def initialize(
  max_attempts: 5,
  base_delay: 1.0,
  max_delay: 60.0,
  backoff_coefficient: 2.0,
  max_duration: 300.0,
  retryable_errors: [StandardError],
  non_retryable_errors: []
)
  @max_attempts = max_attempts
  @base_delay = base_delay.to_f
  @max_delay = max_delay.to_f
  @backoff_coefficient = backoff_coefficient.to_f
  @max_duration = max_duration&.to_f
  @retryable_errors = Array(retryable_errors)
  @non_retryable_errors = Array(non_retryable_errors)
end

Instance Attribute Details

#backoff_coefficientObject (readonly)

Returns the value of attribute backoff_coefficient.



6
7
8
# File 'lib/shikibu/retry_policy.rb', line 6

def backoff_coefficient
  @backoff_coefficient
end

#base_delayObject (readonly)

Returns the value of attribute base_delay.



6
7
8
# File 'lib/shikibu/retry_policy.rb', line 6

def base_delay
  @base_delay
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



6
7
8
# File 'lib/shikibu/retry_policy.rb', line 6

def max_attempts
  @max_attempts
end

#max_delayObject (readonly)

Returns the value of attribute max_delay.



6
7
8
# File 'lib/shikibu/retry_policy.rb', line 6

def max_delay
  @max_delay
end

#max_durationObject (readonly)

Returns the value of attribute max_duration.



6
7
8
# File 'lib/shikibu/retry_policy.rb', line 6

def max_duration
  @max_duration
end

#non_retryable_errorsObject (readonly)

Returns the value of attribute non_retryable_errors.



6
7
8
# File 'lib/shikibu/retry_policy.rb', line 6

def non_retryable_errors
  @non_retryable_errors
end

#retryable_errorsObject (readonly)

Returns the value of attribute retryable_errors.



6
7
8
# File 'lib/shikibu/retry_policy.rb', line 6

def retryable_errors
  @retryable_errors
end

Class Method Details

.defaultObject

Default retry policy



72
73
74
# File 'lib/shikibu/retry_policy.rb', line 72

def self.default
  @default ||= new
end

.noneObject

No retry policy (single attempt)



77
78
79
# File 'lib/shikibu/retry_policy.rb', line 77

def self.none
  @none ||= new(max_attempts: 1)
end

Instance Method Details

#delay_for(attempt) ⇒ Float

Calculate delay for a given attempt



66
67
68
69
# File 'lib/shikibu/retry_policy.rb', line 66

def delay_for(attempt)
  delay = @base_delay * (@backoff_coefficient**(attempt - 1))
  [delay, @max_delay].min
end

#retryable?(error) ⇒ Boolean

Check if an error should be retried



37
38
39
40
41
42
43
44
45
46
# File 'lib/shikibu/retry_policy.rb', line 37

def retryable?(error)
  # TerminalError is never retried
  return false if error.is_a?(TerminalError)

  # Non-retryable errors take precedence
  return false if @non_retryable_errors.any? { |klass| error.is_a?(klass) }

  # Check if error matches retryable classes
  @retryable_errors.any? { |klass| error.is_a?(klass) }
end

#should_retry?(attempt, started_at = nil) ⇒ Boolean

Check if we should continue retrying



52
53
54
55
56
57
58
59
60
61
# File 'lib/shikibu/retry_policy.rb', line 52

def should_retry?(attempt, started_at = nil)
  return false if @max_attempts && attempt >= @max_attempts

  if @max_duration && started_at
    elapsed = Time.now - started_at
    return false if elapsed >= @max_duration
  end

  true
end