Class: Gapic::Common::RetryPolicy

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

Overview

Gapic Common retry policy base class.

Constant Summary collapse

DEFAULT_INITIAL_DELAY =
1
DEFAULT_MAX_DELAY =
15
DEFAULT_MULTIPLIER =
1.3
DEFAULT_RETRY_CODES =
[].freeze
DEFAULT_TIMEOUT =
3600

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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

#delayNumeric

Current delay value in seconds.



139
140
141
# File 'lib/gapic/common/retry_policy.rb', line 139

def delay
  @delay
end

#dupRetryPolicy

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_delayNumeric



63
64
65
# File 'lib/gapic/common/retry_policy.rb', line 63

def initial_delay
  @initial_delay || DEFAULT_INITIAL_DELAY
end

#jitterNumeric



88
89
90
# File 'lib/gapic/common/retry_policy.rb', line 88

def jitter
  @jitter || DEFAULT_JITTER
end

#max_delayNumeric



68
69
70
# File 'lib/gapic/common/retry_policy.rb', line 68

def max_delay
  @max_delay || DEFAULT_MAX_DELAY
end

#multiplierNumeric



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_countInteger

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_codesArray<Integer>



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

#timeoutNumeric



83
84
85
# File 'lib/gapic/common/retry_policy.rb', line 83

def timeout
  @timeout || DEFAULT_TIMEOUT
end