Skip to content

Commit 9fdc66a

Browse files
committed
Retry with Exponential backoff
1 parent d7ccf74 commit 9fdc66a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/fcm.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def recover_notification_key(key_name, project_id)
9494
notification_key_name: key_name
9595
}
9696
}
97-
97+
9898
extra_headers = {
9999
'project_id' => project_id
100100
}
@@ -144,7 +144,7 @@ def get_instance_id_info iid_token, options={}
144144
params = {
145145
query: options
146146
}
147-
147+
148148
for_uri(INSTANCE_ID_API) do |connection|
149149
response = connection.get('/iid/info/'+iid_token, params)
150150
build_response(response)
@@ -177,7 +177,14 @@ def send_to_topic_condition(condition, options = {})
177177
private
178178

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

0 commit comments

Comments
 (0)