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) ⇒ RetryPolicy
constructor
Create new Gapic::Common::RetryPolicy instance.
-
#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) ⇒ RetryPolicy
Create new Gapic::Common::RetryPolicy instance.
43 44 45 46 47 48 49 50 51 |
# File 'lib/gapic/common/retry_policy.rb', line 43 def initialize initial_delay: nil, max_delay: nil, multiplier: nil, retry_codes: nil, timeout: nil # 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 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.
100 101 102 103 104 |
# File 'lib/gapic/common/retry_policy.rb', line 100 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.
124 125 126 |
# File 'lib/gapic/common/retry_policy.rb', line 124 def delay @delay end |
#dup ⇒ RetryPolicy
Returns a duplicate in a non-executing state, i.e. with the deadline and current delay reset.
84 85 86 87 88 89 90 |
# File 'lib/gapic/common/retry_policy.rb', line 84 def dup RetryPolicy.new initial_delay: @initial_delay, max_delay: @max_delay, multiplier: @multiplier, retry_codes: @retry_codes, timeout: @timeout end |
#initial_delay ⇒ Numeric
Returns Initial delay in seconds.
54 55 56 |
# File 'lib/gapic/common/retry_policy.rb', line 54 def initial_delay @initial_delay || DEFAULT_INITIAL_DELAY end |
#max_delay ⇒ Numeric
Returns Maximum delay in seconds.
59 60 61 |
# File 'lib/gapic/common/retry_policy.rb', line 59 def max_delay @max_delay || DEFAULT_MAX_DELAY end |
#multiplier ⇒ Numeric
Returns The delay scaling factor for each subsequent retry attempt.
64 65 66 |
# File 'lib/gapic/common/retry_policy.rb', line 64 def multiplier @multiplier || DEFAULT_MULTIPLIER end |
#perform_delay! ⇒ Boolean
Perform delay.
112 113 114 115 116 117 |
# File 'lib/gapic/common/retry_policy.rb', line 112 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
133 134 135 |
# File 'lib/gapic/common/retry_policy.rb', line 133 def perform_delay_count @perform_delay_count end |
#retry_codes ⇒ Array<Integer>
Returns List of retry codes.
69 70 71 |
# File 'lib/gapic/common/retry_policy.rb', line 69 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.
150 151 152 153 154 155 156 157 |
# File 'lib/gapic/common/retry_policy.rb', line 150 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.
74 75 76 |
# File 'lib/gapic/common/retry_policy.rb', line 74 def timeout @timeout || DEFAULT_TIMEOUT end |