Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(core): Support for max elapsed time configuration.
  • Loading branch information
stanhu committed Jan 20, 2022
1 parent b14bede commit 241361e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions google-apis-core/lib/google/apis/core/http_command.rb
Expand Up @@ -100,6 +100,7 @@ def execute(client)
opencensus_begin_span
begin
Retriable.retriable tries: options.retries + 1,
max_elapsed_time: options.max_elapsed_time,
base_interval: 1,
multiplier: 2,
on: RETRIABLE_ERRORS do |try|
Expand Down
4 changes: 4 additions & 0 deletions google-apis-core/lib/google/apis/options.rb
Expand Up @@ -28,6 +28,7 @@ module Apis
RequestOptions = Struct.new(
:authorization,
:retries,
:max_elapsed_time,
:header,
:normalize_unicode,
:skip_serialization,
Expand Down Expand Up @@ -66,6 +67,8 @@ class RequestOptions
# @return [Signet::OAuth2::Client, #apply(Hash)] OAuth2 credentials.
# @!attribute [rw] retries
# @return [Fixnum] Number of times to retry requests on server error.
# @!attribute [rw] max_elapsed_time
# @return [Fixnum] Total time in seconds that requests are allowed to keep being retried.
# @!attribute [rw] header
# @return [Hash<String,String>] Additional HTTP headers to include in requests.
# @!attribute [rw] normalize_unicode
Expand Down Expand Up @@ -106,6 +109,7 @@ def merge(options)
ClientOptions.default.application_version = '0.0.0'
ClientOptions.default.transparent_gzip_decompression = true
RequestOptions.default.retries = 0
RequestOptions.default.max_elapsed_time = 900
RequestOptions.default.normalize_unicode = false
RequestOptions.default.skip_serialization = false
RequestOptions.default.skip_deserialization = false
Expand Down
12 changes: 12 additions & 0 deletions google-apis-core/spec/google/apis/core/http_command_spec.rb
Expand Up @@ -147,6 +147,18 @@ class DecryptResponse
it 'should call block if present' do
expect { |b| command.execute(client, &b) }.to yield_with_args('Hello world', nil)
end

it 'should retry with max elapsed_time and retries' do
expect(Retriable).to receive(:retriable).with(
tries: Google::Apis::RequestOptions.default.retries + 1,
max_elapsed_time: Google::Apis::RequestOptions.default.max_elapsed_time,
base_interval: 1,
multiplier: 2,
on: described_class::RETRIABLE_ERRORS).and_call_original
allow(Retriable).to receive(:retriable).and_call_original

command.execute(client)
end
end

context('with server errors') do
Expand Down
12 changes: 12 additions & 0 deletions google-apis-core/spec/google/apis/options_spec.rb
Expand Up @@ -17,6 +17,7 @@

RSpec.describe Google::Apis::RequestOptions do
let(:options) { Google::Apis::RequestOptions.new }
let(:defaults) { described_class.default }

it 'should not merge nil values' do
options.retries = 1
Expand All @@ -37,4 +38,15 @@
it 'should allow nil in merge' do
expect(options.merge(nil)).to be_an_instance_of(Google::Apis::RequestOptions)
end

it 'sets default values' do
expect(defaults.retries).to eq(5) # Overriden in spec_helper.rb
expect(defaults.max_elapsed_time).to eq(900)
expect(defaults.normalize_unicode).to be false
expect(defaults.skip_serialization).to be false
expect(defaults.skip_deserialization).to be false
expect(defaults.api_format_version).to be nil
expect(defaults.use_opencensus).to be true
expect(defaults.quota_project).to be_nil
end
end

0 comments on commit 241361e

Please sign in to comment.