Skip to content

Commit

Permalink
Retry with Exponential backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan-M-Thomaz committed Mar 28, 2019
1 parent d7ccf74 commit 9fdc66a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/fcm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def recover_notification_key(key_name, project_id)
notification_key_name: key_name
}
}

extra_headers = {
'project_id' => project_id
}
Expand Down Expand Up @@ -144,7 +144,7 @@ def get_instance_id_info iid_token, options={}
params = {
query: options
}

for_uri(INSTANCE_ID_API) do |connection|
response = connection.get('/iid/info/'+iid_token, params)
build_response(response)
Expand Down Expand Up @@ -177,7 +177,14 @@ def send_to_topic_condition(condition, options = {})
private

def for_uri(uri, extra_headers = {})
retryable_exceptions = Faraday::Request::Retry::DEFAULT_EXCEPTIONS +[ServerError]
connection = ::Faraday.new(:url => uri) do |faraday|
faraday.request :retry, max: 5, interval: 0.1, interval_randomness: 0.5, backoff_factor: 2,
exceptions: retryable_exceptions, retry_statuses: [200], methods: [],
retry_if: Proc.new do |env, exception|
return true unless exception.is_a?(Faraday::RetriableResponse)
return true if exception.response.body[:results].any? { |result| result[:error] == "Unavailable" }
end
faraday.adapter Faraday.default_adapter
faraday.headers["Content-Type"] = "application/json"
faraday.headers["Authorization"] = "key=#{api_key}"
Expand Down

1 comment on commit 9fdc66a

@Alan-M-Thomaz
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing Retry with Exponential backoff for 5XX errors and 200+error:Unavaible
Google encourage to implement it for the servers, and also warn that senders can get blacklisted if they cause problems due not implementing it.

Please sign in to comment.