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 =

Returns Default initial delay in seconds.

Returns:

  • (Numeric)

    Default initial delay in seconds.

1
DEFAULT_MAX_DELAY =

Returns Default maximum delay in seconds.

Returns:

  • (Numeric)

    Default maximum delay in seconds.

15
DEFAULT_MULTIPLIER =

Returns Default delay scaling factor for subsequent retry attempts.

Returns:

  • (Numeric)

    Default delay scaling factor for subsequent retry attempts.

1.3
DEFAULT_RETRY_CODES =

Returns Default list of retry codes.

Returns:

  • (Array<String|Integer>)

    Default list of retry codes.

[].freeze
DEFAULT_TIMEOUT =

Returns Default timeout threshold value in seconds.

Returns:

  • (Numeric)

    Default timeout threshold value in seconds.

3600

Instance Method Summary collapse

Constructor Details

#initialize(initial_delay: nil, max_delay: nil, multiplier: nil, retry_codes: nil, timeout: nil) ⇒ RetryPolicy

Create new Gapic::Common::RetryPolicy instance.

Parameters:

  • initial_delay (Numeric) (defaults to: nil)

    Initial delay in seconds.

  • max_delay (Numeric) (defaults to: nil)

    Maximum delay in seconds.

  • multiplier (Numeric) (defaults to: nil)

    The delay scaling factor for each subsequent retry attempt.

  • retry_codes (Array<String|Integer>) (defaults to: nil)

    List of retry codes.

  • timeout (Numeric) (defaults to: nil)

    Timeout threshold value in seconds.



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.

Returns:

  • (Boolean)

    Whether the delay was executed.



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

#delayNumeric

Current delay value in seconds.

Returns:

  • (Numeric)

    Time delay in seconds.



124
125
126
# File 'lib/gapic/common/retry_policy.rb', line 124

def delay
  @delay
end

#dupRetryPolicy

Returns a duplicate in a non-executing state, i.e. with the deadline and current delay reset.

Returns:



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_delayNumeric

Returns Initial delay in seconds.

Returns:

  • (Numeric)

    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_delayNumeric

Returns Maximum delay in seconds.

Returns:

  • (Numeric)

    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

#multiplierNumeric

Returns The delay scaling factor for each subsequent retry attempt.

Returns:

  • (Numeric)

    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.

Returns:

  • (Boolean)

    Whether the delay was executed.



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_countInteger

Current number of times the delay has been performed

Returns:

  • (Integer)


133
134
135
# File 'lib/gapic/common/retry_policy.rb', line 133

def perform_delay_count
  @perform_delay_count
end

#retry_codesArray<Integer>

Returns List of retry codes.

Returns:

  • (Array<Integer>)

    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.

Parameters:

  • mock_delay (boolean, Proc) (defaults to: false)

    if truthy, delays are "mocked", meaning they do not actually take time, but are measured as if they did, which is useful for tests. If set to a Proc, it will be called whenever a delay would happen, and passed the delay in seconds, also useful for testing.



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

#timeoutNumeric

Returns Timeout threshold value in seconds.

Returns:

  • (Numeric)

    Timeout threshold value in seconds.



74
75
76
# File 'lib/gapic/common/retry_policy.rb', line 74

def timeout
  @timeout || DEFAULT_TIMEOUT
end