Class: Gapic::Common::RetryPolicy
- Inherits:
-
Object
- Object
- Gapic::Common::RetryPolicy
- Defined in:
- lib/gapic/common/retry_policy.rb
Overview
Gapic Common retry policy base class.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_INITIAL_DELAY =
Returns Default initial delay in seconds.
1- DEFAULT_MAX_DELAY =
Returns Default maximum delay in seconds.
15- DEFAULT_MULTIPLIER =
Returns Default delay scaling factor for subsequent retry attempts.
1.3- DEFAULT_RETRY_CODES =
Returns Default list of retry codes.
[].freeze
- DEFAULT_TIMEOUT =
Returns Default timeout threshold value in seconds.
3600
Instance Method Summary collapse
-
#call(error = nil) ⇒ Boolean
(also: #perform_delay)
Perform delay if and only if retriable.
-
#delay ⇒ Numeric
Current delay value in seconds.
-
#dup ⇒ RetryPolicy
Returns a duplicate in a non-executing state, i.e.
-
#initial_delay ⇒ Numeric
Initial delay in seconds.
-
#initialize(initial_delay: nil, max_delay: nil, multiplier: nil, retry_codes: nil, timeout: nil, jitter: nil) ⇒ RetryPolicy
constructor
Create new Gapic::Common::RetryPolicy instance.
-
#jitter ⇒ Numeric
Random jitter added to the delay in seconds.
-
#max_delay ⇒ Numeric
Maximum delay in seconds.
-
#multiplier ⇒ Numeric
The delay scaling factor for each subsequent retry attempt.
-
#perform_delay! ⇒ Boolean
Perform delay.
-
#perform_delay_count ⇒ Integer
Current number of times the delay has been performed.
-
#retry_codes ⇒ Array<Integer>
List of retry codes.
-
#start!(mock_delay: false) ⇒ Object
Start tracking the deadline and delay by initializing those values.
-
#timeout ⇒ Numeric
Timeout threshold value in seconds.
Constructor Details
#initialize(initial_delay: nil, max_delay: nil, multiplier: nil, retry_codes: nil, timeout: nil, jitter: nil) ⇒ RetryPolicy
Create new Gapic::Common::RetryPolicy instance.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gapic/common/retry_policy.rb', line 49 def initialize initial_delay: nil, max_delay: nil, multiplier: nil, retry_codes: nil, timeout: nil, jitter: nil raise ArgumentError, "jitter cannot be negative" if jitter&.negative? # Instance values are set as `nil` to determine whether values are overriden from default. @initial_delay = initial_delay @max_delay = max_delay @multiplier = multiplier @retry_codes = convert_codes retry_codes @timeout = timeout @jitter = jitter start! end |
Instance Method Details
#call(error = nil) ⇒ Boolean Also known as: perform_delay
Perform delay if and only if retriable.
If positional argument error is provided, the retriable logic uses
retry_codes. Otherwise, timeout is used.
115 116 117 118 119 |
# File 'lib/gapic/common/retry_policy.rb', line 115 def call error = nil should_retry = error.nil? ? retry_with_deadline? : retry_error?(error) return false unless should_retry perform_delay! end |
#delay ⇒ Numeric
Current delay value in seconds.
139 140 141 |
# File 'lib/gapic/common/retry_policy.rb', line 139 def delay @delay end |
#dup ⇒ RetryPolicy
Returns a duplicate in a non-executing state, i.e. with the deadline and current delay reset.
98 99 100 101 102 103 104 105 |
# File 'lib/gapic/common/retry_policy.rb', line 98 def dup RetryPolicy.new initial_delay: @initial_delay, max_delay: @max_delay, multiplier: @multiplier, retry_codes: @retry_codes, timeout: @timeout, jitter: @jitter end |
#initial_delay ⇒ Numeric
Returns Initial delay in seconds.
63 64 65 |
# File 'lib/gapic/common/retry_policy.rb', line 63 def initial_delay @initial_delay || DEFAULT_INITIAL_DELAY end |
#jitter ⇒ Numeric
Returns Random jitter added to the delay in seconds.
88 89 90 |
# File 'lib/gapic/common/retry_policy.rb', line 88 def jitter @jitter || DEFAULT_JITTER end |
#max_delay ⇒ Numeric
Returns Maximum delay in seconds.
68 69 70 |
# File 'lib/gapic/common/retry_policy.rb', line 68 def max_delay @max_delay || DEFAULT_MAX_DELAY end |
#multiplier ⇒ Numeric
Returns The delay scaling factor for each subsequent retry attempt.
73 74 75 |
# File 'lib/gapic/common/retry_policy.rb', line 73 def multiplier @multiplier || DEFAULT_MULTIPLIER end |
#perform_delay! ⇒ Boolean
Perform delay.
127 128 129 130 131 132 |
# File 'lib/gapic/common/retry_policy.rb', line 127 def perform_delay! delay! increment_delay! @perform_delay_count += 1 true end |
#perform_delay_count ⇒ Integer
Current number of times the delay has been performed
148 149 150 |
# File 'lib/gapic/common/retry_policy.rb', line 148 def perform_delay_count @perform_delay_count end |
#retry_codes ⇒ Array<Integer>
Returns List of retry codes.
78 79 80 |
# File 'lib/gapic/common/retry_policy.rb', line 78 def retry_codes @retry_codes || DEFAULT_RETRY_CODES end |
#start!(mock_delay: false) ⇒ Object
Start tracking the deadline and delay by initializing those values.
This is normally done when the object is constructed, but it can be done explicitly in order to reinitialize the state in case this retry policy was created in the past or is being reused.
165 166 167 168 169 170 171 172 |
# File 'lib/gapic/common/retry_policy.rb', line 165 def start! mock_delay: false @mock_time = mock_delay ? Process.clock_gettime(Process::CLOCK_MONOTONIC) : nil @mock_delay_callback = mock_delay.respond_to?(:call) ? mock_delay : nil @deadline = cur_time + timeout @delay = initial_delay @perform_delay_count = 0 self end |
#timeout ⇒ Numeric
Returns Timeout threshold value in seconds.
83 84 85 |
# File 'lib/gapic/common/retry_policy.rb', line 83 def timeout @timeout || DEFAULT_TIMEOUT end |