Class: Gapic::Common::PollingHarness

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_policyGapic::Common::RetryPolicy (readonly)



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.

Yield Returns:

  • (Object)

    The result of the polling logic, either a result to return, or the wait_sentinel value.



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