-
Notifications
You must be signed in to change notification settings - Fork 156
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
Open
Alan-M-Thomaz
wants to merge
8
commits into
decision-labs:master
Choose a base branch
from
Alan-M-Thomaz:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9fdc66a
Retry with Exponential backoff
Alan-M-Thomaz ad2e977
Update to FCM URLs for device groups features
Alan-M-Thomaz f99a4c3
fix tests to use FCM device group URL
Alan-M-Thomaz 3607446
fix,refactor and added tests for retry
Alan-M-Thomaz 46079de
cover all retryable errors
Alan-M-Thomaz f86baf6
removing Faraday raise_error middleware.
Alan-M-Thomaz a8742e0
fix syntax in specs for ruby version 2.1 or lower
Alan-M-Thomaz 8620262
refactor notification_key related methods
Alan-M-Thomaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
@@ -45,14 +45,7 @@ def create_notification_key(key_name, project_id, registration_ids = []) | |
post_body = build_post_body(registration_ids, operation: 'create', | ||
notification_key_name: key_name) | ||
|
||
extra_headers = { | ||
'project_id' => project_id | ||
} | ||
|
||
for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| | ||
response = connection.post('/gcm/notification', post_body.to_json) | ||
build_response(response) | ||
end | ||
update_group_messaging_setting(post_body, project_id) | ||
end | ||
alias create create_notification_key | ||
|
||
|
@@ -61,14 +54,7 @@ def add_registration_ids(key_name, project_id, notification_key, registration_id | |
notification_key_name: key_name, | ||
notification_key: notification_key) | ||
|
||
extra_headers = { | ||
'project_id' => project_id | ||
} | ||
|
||
for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| | ||
response = connection.post('/gcm/notification', post_body.to_json) | ||
build_response(response) | ||
end | ||
update_group_messaging_setting(post_body, project_id) | ||
end | ||
alias add add_registration_ids | ||
|
||
|
@@ -77,14 +63,7 @@ def remove_registration_ids(key_name, project_id, notification_key, registration | |
notification_key_name: key_name, | ||
notification_key: notification_key) | ||
|
||
extra_headers = { | ||
'project_id' => project_id | ||
} | ||
|
||
for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| | ||
response = connection.post('/gcm/notification', post_body.to_json) | ||
build_response(response) | ||
end | ||
update_group_messaging_setting(post_body, project_id) | ||
end | ||
alias remove remove_registration_ids | ||
|
||
|
@@ -94,13 +73,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 +123,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 +156,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 |
||
|
@@ -238,6 +232,17 @@ def build_not_registered_ids(body, registration_id) | |
not_registered_ids | ||
end | ||
|
||
def update_group_messaging_setting(body, project_id) | ||
extra_headers = { | ||
'project_id' => project_id | ||
} | ||
|
||
for_uri(BASE_URI, extra_headers) do |connection| | ||
response = connection.post('/fcm/notification', body.to_json) | ||
build_response(response) | ||
end | ||
end | ||
|
||
def execute_notification(body) | ||
for_uri(BASE_URI) do |connection| | ||
response = connection.post('/fcm/send', body.to_json) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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