-
Notifications
You must be signed in to change notification settings - Fork 72
Support Collection.count() and exponential-backoff-and-retry #83
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
Open
jjinno
wants to merge
9
commits into
dropbox:master
Choose a base branch
from
jjinno:count_and_retry
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
9 commits
Select commit
Hold shift + click to select a range
df55b00
Update Collection.count() to v2 compatible
jjinno 8cabbb3
add backoff/retry logic
jjinno f71a14c
fixed flake8 comma complaints
jjinno ef33ced
fixed flake8 unused variable complaint
jjinno 936a5b1
fixed URLError Exception hierarchy
jjinno 8e70bb3
JSON for testing Incident.log_entries
jjinno 1bb6896
tests for log_entries.count() & log_entries.list()
jjinno b9f2a32
separated log_entries test into 2 tests
jjinno eb1129a
added retry on 5xx errors
jjinno 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,15 +5,19 @@ | |
| from .exceptions import Error, IntegrationAPIError, BadRequest, NotFound | ||
| from six import string_types | ||
| from six.moves import urllib | ||
| from random import randint | ||
| from time import sleep | ||
|
|
||
| ISO8601_FORMAT = "%Y-%m-%dT%H:%M:%SZ" | ||
|
|
||
|
|
||
| class Requester(object): | ||
| def __init__(self, timeout=10, proxies=None, parse_datetime=False): | ||
| def __init__(self, timeout=10, proxies=None, parse_datetime=False, min_backoff=15.0, max_retries=5): | ||
| self.timeout = timeout | ||
| self.json_loader = json.loads | ||
| self.min_backoff = min_backoff | ||
| self.max_retries = max_retries | ||
|
|
||
| self.json_loader = json.loads | ||
| if parse_datetime: | ||
| self.json_loader = _json_loader | ||
|
|
||
|
|
@@ -22,7 +26,11 @@ def __init__(self, timeout=10, proxies=None, parse_datetime=False): | |
| handlers.append(urllib.request.ProxyHandler(proxies)) | ||
| self.opener = urllib.request.build_opener(*handlers) | ||
|
|
||
| def execute_request(self, request): | ||
| def execute_request(self, request, retry_count=0): | ||
| if retry_count > 0: | ||
| exponential_backoff = self.min_backoff * (2 ** retry_count) | ||
| randomness = randint(0, 1000) / 1000.0 | ||
| sleep(exponential_backoff + randomness) | ||
|
|
||
| try: | ||
| response = (self.opener.open(request, timeout=self.timeout). | ||
|
|
@@ -38,7 +46,20 @@ def execute_request(self, request): | |
| raise NotFound("URL ({0}) Not Found.".format( | ||
| request.get_full_url())) | ||
| elif err.code == 429: | ||
| if retry_count < self.max_retries: | ||
| return self.execute_request(request, retry_count + 1) | ||
| else: | ||
| raise | ||
| elif err.code / 100 == 5: | ||
|
Contributor
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. Probably want integer division here, |
||
| if retry_count < self.max_retries: | ||
| return self.execute_request(request, retry_count + 1) | ||
| else: | ||
| raise | ||
| else: | ||
| raise | ||
| except urllib.error.URLError: | ||
| if retry_count < self.max_retries: | ||
| return self.execute_request(request, retry_count + 1) | ||
| else: | ||
| raise | ||
|
|
||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
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.
Previously the timeout was 10s with no retries; with the retries + backoff it'll be a minimum 75s before the request will give up by default.
I'd prefer to lower the default values (maybe min_backoff=5 and retries=3), and we should provide a way for users to plumb through their own values, here: https://github.com/dropbox/pygerduty/blob/master/pygerduty/v2.py#L611