Class: OCI::Retry::RetryConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/oci/retry/retry_config.rb

Overview

The configuration for a retry strategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_sleep_time_millis:, exponential_growth_factor:, should_retry_exception_proc:, sleep_calc_millis_proc:, max_attempts: nil, max_elapsed_time_millis: nil, max_sleep_between_attempts_millis: nil) ⇒ RetryConfig

Returns a new instance of RetryConfig.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/oci/retry/retry_config.rb', line 57

def initialize(
  base_sleep_time_millis:,
  exponential_growth_factor:,
  should_retry_exception_proc:,
  sleep_calc_millis_proc:,
  max_attempts: nil,
  max_elapsed_time_millis: nil,
  max_sleep_between_attempts_millis: nil
)
  raise 'base_sleep_time_millis must be greater than or equal to 1' \
    if base_sleep_time_millis.nil? || base_sleep_time_millis < 1

  raise 'exponential_growth_factor must be greater than or equal to 1' \
    if exponential_growth_factor.nil? || exponential_growth_factor < 1

  raise 'should_retry_exception_proc must be provided' if should_retry_exception_proc.nil?
  raise 'sleep_calc_millis_proc must be provided' if sleep_calc_millis_proc.nil?

  @base_sleep_time_millis = base_sleep_time_millis
  @exponential_growth_factor = exponential_growth_factor
  @should_retry_exception_proc = should_retry_exception_proc
  @sleep_calc_millis_proc = sleep_calc_millis_proc

  unless max_attempts.nil?
    raise 'max_attempts must be greater than or equal to 1' if max_attempts < 1

    @max_attempts = max_attempts
  end

  unless max_elapsed_time_millis.nil?
    raise 'max_elapsed_time_millis must be greater than or equal to 0' if max_elapsed_time_millis < 0

    @max_elapsed_time_millis = max_elapsed_time_millis
  end

  raise 'max_sleep_between_attempts_millis must be greater than or equal to 0' \
    if max_sleep_between_attempts_millis && max_sleep_between_attempts_millis < 0

  @max_sleep_between_attempts_millis = max_sleep_between_attempts_millis if max_sleep_between_attempts_millis
end

Instance Attribute Details

#base_sleep_time_millisInteger (readonly)

For exponential backoff and retry calculations, the base time (in milliseconds) which we multiply by some exponential value

Returns:

  • (Integer)


40
41
42
# File 'lib/oci/retry/retry_config.rb', line 40

def base_sleep_time_millis
  @base_sleep_time_millis
end

#exponential_growth_factorInteger (readonly)

For exponential backoff and retry calculations, the exponent we'll raise to the number of attempts. The result would then be multiplied by the value in #base_sleep_time_millis

Returns:

  • (Integer)


46
47
48
# File 'lib/oci/retry/retry_config.rb', line 46

def exponential_growth_factor
  @exponential_growth_factor
end

#max_attemptsInteger (readonly)

The maximum number of attempts before we stop trying to make calls. This is one-based (i.e. the first attempt is 1, the second is 2 etc.) and nil indicates that there is no maximum.

Returns:

  • (Integer)


14
15
16
# File 'lib/oci/retry/retry_config.rb', line 14

def max_attempts
  @max_attempts
end

#max_elapsed_time_millisInteger (readonly)

The maximum amount of time (in milliseconds) that can elapse for all attempts before we stop trying to make calls. A nil value indicates that there is no maximum

Returns:

  • (Integer)


20
21
22
# File 'lib/oci/retry/retry_config.rb', line 20

def max_elapsed_time_millis
  @max_elapsed_time_millis
end

#max_sleep_between_attempts_millisInteger (readonly)

For exponential backoff and retry calclulation, the maximum amount of time (in milliseconds) to sleep between attempts. A nil value indicates that there is no maximum

Returns:

  • (Integer)


52
53
54
# File 'lib/oci/retry/retry_config.rb', line 52

def max_sleep_between_attempts_millis
  @max_sleep_between_attempts_millis
end

#should_retry_exception_procProc (readonly)

A proc which can be called to determine whether a particular exception can be retried. This proc should take a single argument of type Internal::RetryState. The exception can be accessed via Intenral::RetryState#last_exception

Returns:

  • (Proc)


27
28
29
# File 'lib/oci/retry/retry_config.rb', line 27

def should_retry_exception_proc
  @should_retry_exception_proc
end

#sleep_calc_millis_procProc (readonly)

A proc which can be called to determine the next sleep time (in milliseconds) when retrying. This proc should take two arguments, the first argument of type OCI::Retry::RetryConfig and the second of type Internal::RetryState

Returns:

  • (Proc)


34
35
36
# File 'lib/oci/retry/retry_config.rb', line 34

def sleep_calc_millis_proc
  @sleep_calc_millis_proc
end

Instance Method Details

#do_sleep(retry_state) ⇒ Object

Sleeps for some amount of time based on the current state of making a retriable call

Parameters:



119
120
121
122
# File 'lib/oci/retry/retry_config.rb', line 119

def do_sleep(retry_state)
  sleep_time_millis = sleep_calc_millis_proc.call(self, retry_state)
  sleep(sleep_time_millis / 1000.0)
end

#should_retry?(retry_state) ⇒ Boolean

Determines whether we should retry a call based on the current state of making a retriable call and the values defined in this configuration.

Parameters:

Returns:

  • (Boolean)

    true if we should retry based on the current state and this configuration, and false otherwise



106
107
108
109
110
111
112
113
114
# File 'lib/oci/retry/retry_config.rb', line 106

def should_retry?(retry_state)
  current_time = (Time.now.to_f * 1000).to_i
  time_difference_millis = current_time - retry_state.start_time_epoch_millis

  return false if @max_attempts && retry_state.current_attempt_number >= @max_attempts
  return false if @max_elapsed_time_millis && time_difference_millis > @max_elapsed_time_millis

  should_retry_exception_proc.call(retry_state)
end