-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic retry with exponential backoff #58
base: master
Are you sure you want to change the base?
Changes from 7 commits
9fdc66a
ad2e977
f99a4c3
3607446
46079de
f86baf6
a8742e0
8620262
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ class FCM | |
FORMAT = :json | ||
|
||
# constants | ||
GROUP_NOTIFICATION_BASE_URI = 'https://android.googleapis.com' | ||
INSTANCE_ID_API = 'https://iid.googleapis.com' | ||
TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/ | ||
|
||
|
@@ -34,6 +33,7 @@ def initialize(api_key, client_options = {}) | |
def send_notification(registration_ids, options = {}) | ||
post_body = build_post_body(registration_ids, options) | ||
|
||
|
||
for_uri(BASE_URI) do |connection| | ||
response = connection.post('/fcm/send', post_body.to_json) | ||
build_response(response, registration_ids) | ||
|
@@ -49,8 +49,8 @@ def create_notification_key(key_name, project_id, registration_ids = []) | |
'project_id' => project_id | ||
} | ||
|
||
for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| | ||
response = connection.post('/gcm/notification', post_body.to_json) | ||
for_uri(BASE_URI, extra_headers) do |connection| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since there are four places where we need to call
maybe it would be better if we refactor this api call into a separate private method, like what def update_group_messaging_setting(body)
for_uri(BASE_URI, extra_headers) do |connection|
response = connection.post('/fcm/notification', body.to_json)
build_response(response)
end
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, except for |
||
response = connection.post('/fcm/notification', post_body.to_json) | ||
build_response(response) | ||
end | ||
end | ||
|
@@ -65,8 +65,8 @@ def add_registration_ids(key_name, project_id, notification_key, registration_id | |
'project_id' => project_id | ||
} | ||
|
||
for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| | ||
response = connection.post('/gcm/notification', post_body.to_json) | ||
for_uri(BASE_URI, extra_headers) do |connection| | ||
response = connection.post('/fcm/notification', post_body.to_json) | ||
build_response(response) | ||
end | ||
end | ||
|
@@ -81,8 +81,8 @@ def remove_registration_ids(key_name, project_id, notification_key, registration | |
'project_id' => project_id | ||
} | ||
|
||
for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| | ||
response = connection.post('/gcm/notification', post_body.to_json) | ||
for_uri(BASE_URI, extra_headers) do |connection| | ||
response = connection.post('/fcm/notification', post_body.to_json) | ||
build_response(response) | ||
end | ||
end | ||
|
@@ -94,13 +94,13 @@ def recover_notification_key(key_name, project_id) | |
notification_key_name: key_name | ||
} | ||
} | ||
|
||
extra_headers = { | ||
'project_id' => project_id | ||
} | ||
|
||
for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| | ||
response = connection.get('/gcm/notification', params) | ||
for_uri(BASE_URI, extra_headers) do |connection| | ||
response = connection.get('/fcm/notification', params) | ||
build_response(response) | ||
end | ||
end | ||
|
@@ -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) | ||
|
@@ -177,7 +177,22 @@ def send_to_topic_condition(condition, options = {}) | |
private | ||
|
||
def for_uri(uri, extra_headers = {}) | ||
retry_if_func = lambda do |env, exception| | ||
retryable_errors = [ | ||
"Unavailable", "InternalServerError", | ||
"DeviceMessageRateExceeded", "TopicsMessageRateExceeded"] | ||
if defined?(exception.response) && defined?(exception.response.status) && exception.response.status == 200 | ||
body = JSON.parse(exception.response.body) | ||
body["results"] != nil && body["results"].any? { |result| retryable_errors.include? result["error"]} | ||
else | ||
true | ||
end | ||
end | ||
retryable_exceptions = Faraday::Request::Retry::DEFAULT_EXCEPTIONS | ||
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, *(500..599)], methods: [], | ||
retry_if: retry_if_func | ||
faraday.adapter Faraday.default_adapter | ||
faraday.headers["Content-Type"] = "application/json" | ||
faraday.headers["Authorization"] = "key=#{api_key}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before Authorization: key=AIzaSyZ-1u...0GBYzPu7Udno5aA Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👇here why not just using
execute_notifications(post_body)
instead