Class: Gapic::Common::PollingHarness
- Inherits:
-
Object
- Object
- Gapic::Common::PollingHarness
- Defined in:
- lib/gapic/common/polling_harness.rb
Overview
Provides a generic mechanism for periodic polling an operation.
Polling intervals are calculated from the provided retry policy.
Supports exponential backoff via multiplier
, and automatically
retries gRPC errors listed in retry_codes
.
Instance Attribute Summary collapse
-
#retry_policy ⇒ Gapic::Common::RetryPolicy
readonly
The retry policy associated with this instance.
Instance Method Summary collapse
-
#initialize(retry_policy: nil, **kwargs) ⇒ PollingHarness
constructor
Create new Gapic::Common::PollingHarness instance.
-
#wait(wait_sentinel: nil, timeout_result: nil, mock_delay: false) ⇒ Object
Perform polling with exponential backoff.
Constructor Details
#initialize(retry_policy: nil, **kwargs) ⇒ PollingHarness
Create new Gapic::Common::PollingHarness instance.
40 41 42 |
# File 'lib/gapic/common/polling_harness.rb', line 40 def initialize retry_policy: nil, **kwargs @retry_policy = retry_policy ? retry_policy.dup : RetryPolicy.new(**kwargs) end |
Instance Attribute Details
#retry_policy ⇒ Gapic::Common::RetryPolicy (readonly)
Returns The retry policy associated with this instance.
29 30 31 |
# File 'lib/gapic/common/polling_harness.rb', line 29 def retry_policy @retry_policy end |
Instance Method Details
#wait(wait_sentinel: nil, timeout_result: nil, mock_delay: false) ⇒ Object
Perform polling with exponential backoff. Repeatedly calls the given
block until it returns a result other than the given sentinel value
(normally nil
), then returns that final result.
Uses the retry policy regulate retries, including delays between tries,
retriable errors, and final timeout. If an error code other than those
listed in RetryPolicy#retry_codes is raised, it is propagated out. If
the timeout expires, the given timeout result (normally nil
) is
returned.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gapic/common/polling_harness.rb', line 64 def wait wait_sentinel: nil, timeout_result: nil, mock_delay: false unless block_given? raise ArgumentError, "No callback provided to wait method." end retry_policy.start! mock_delay: mock_delay loop do begin response = yield return response unless response == wait_sentinel rescue StandardError => e # Currently retry_error only accounts for ::GRPC::BadStatus. raise unless retry_policy.retry_error? e end retry_policy.perform_delay! return timeout_result unless retry_policy.retry_with_deadline? end end |