Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion google_maps_service_ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_runtime_dependency "multi_json", "~> 1.15"
spec.add_runtime_dependency "retriable", "~> 3.1"
spec.add_runtime_dependency "base64"

spec.add_development_dependency "coveralls_reborn", "~> 0.25.0"
Expand Down
44 changes: 41 additions & 3 deletions lib/google_maps_service/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "multi_json"
require "net/http"
require "retriable"
require "google_maps_service/errors"
require "google_maps_service/convert"
require "google_maps_service/url"
Expand Down Expand Up @@ -101,6 +100,45 @@ def client

protected

# Retry a block of code with exponential backoff on specific errors.
#
# @param [Integer] timeout Maximum time in seconds to retry (nil for no timeout).
# @param [Array<Class>] on Array of error classes to retry on.
# @yield Block to retry.
#
# @return [Object] Result of the block.
def with_retry(timeout: nil, on: [])
start_time = Time.now
attempt = 0
base_delay = 0.5
max_delay = 60

loop do
begin
return yield
rescue *on => e
attempt += 1
elapsed = Time.now - start_time

if timeout && elapsed >= timeout
raise e
end

# Exponential backoff: 0.5, 0.75, 1.125, 1.6875, ...
delay = [base_delay * (1.5**(attempt - 1)), max_delay].min

# Don't sleep longer than the remaining timeout
if timeout
remaining = timeout - elapsed
delay = [delay, remaining].min
raise e if delay <= 0
end

sleep delay
end
end
end

# Initialize QPS queue. QPS queue is a "tickets" for calling API
def initialize_query_tickets
if @queries_per_second
Expand Down Expand Up @@ -137,7 +175,7 @@ def user_agent
def get(path, params, base_url: DEFAULT_BASE_URL, accepts_client_id: true, custom_response_decoder: nil)
url = URI(base_url + generate_auth_url(path, params, accepts_client_id))

Retriable.retriable timeout: @retry_timeout, on: RETRIABLE_ERRORS do |try|
with_retry(timeout: @retry_timeout, on: RETRIABLE_ERRORS) do
begin
request_query_ticket
request = Net::HTTP::Get.new(url)
Expand Down Expand Up @@ -166,7 +204,7 @@ def get(path, params, base_url: DEFAULT_BASE_URL, accepts_client_id: true, custo
def post(path, params, base_url: DEFAULT_BASE_URL, accepts_client_id: true, custom_response_decoder: nil, field_mask: nil)
url = URI(base_url + generate_auth_url(path, {}, accepts_client_id))

Retriable.retriable timeout: @retry_timeout, on: RETRIABLE_ERRORS do |try|
with_retry(timeout: @retry_timeout, on: RETRIABLE_ERRORS) do
begin
request_query_ticket
request = Net::HTTP::Post.new(url)
Expand Down
Loading