-
-
Notifications
You must be signed in to change notification settings - Fork 877
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
Add handling of 503 Service Unavailable retries #1713
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3bcf524
Made behaviour of create_issues consistent with create_issue
5db4214
Merge branch 'pycontribs:main' into main
gmainguet a95d180
Added handling of 503 Service Unavailable as recoverable error (Jira …
dea286d
Improved code layout
55f8203
Added use of HTTPStatus enum
576b7f9
Added 503 unit test for retries
c4401fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5362250
Merge branch 'pycontribs:main' into main
gmainguet 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import logging | ||
import random | ||
import time | ||
from http import HTTPStatus | ||
from typing import Any | ||
|
||
from requests import Response, Session | ||
|
@@ -278,7 +279,7 @@ def __recoverable( | |
|
||
Exponentially delays if recoverable. | ||
|
||
At this moment it supports: 429 | ||
At this moment it supports: 429, 503 | ||
|
||
Args: | ||
response (Optional[Union[ConnectionError, Response]]): The response or exception. | ||
|
@@ -290,11 +291,14 @@ def __recoverable( | |
Returns: | ||
bool: True if the request should be retried. | ||
""" | ||
is_recoverable = False # Controls return value AND whether we delay or not, Not-recoverable by default | ||
suggested_delay = ( | ||
-1 | ||
) # Controls return value AND whether we delay or not, Not-recoverable by default | ||
msg = str(response) | ||
|
||
if isinstance(response, ConnectionError): | ||
is_recoverable = True | ||
suggested_delay = 10 * 2**counter | ||
|
||
LOG.warning( | ||
f"Got ConnectionError [{response}] errno:{response.errno} on {request_method} " | ||
+ f"{url}\n" # type: ignore[str-bytes-safe] | ||
|
@@ -304,45 +308,27 @@ def __recoverable( | |
"Response headers for ConnectionError are only printed for log level DEBUG." | ||
) | ||
|
||
if isinstance(response, Response): | ||
if response.status_code in [429]: | ||
is_recoverable = True | ||
number_of_tokens_issued_per_interval = response.headers.get( | ||
"X-RateLimit-FillRate" | ||
) | ||
token_issuing_rate_interval_seconds = response.headers.get( | ||
"X-RateLimit-Interval-Seconds" | ||
) | ||
maximum_number_of_tokens = response.headers.get("X-RateLimit-Limit") | ||
retry_after = response.headers.get("retry-after") | ||
msg = f"{response.status_code} {response.reason}" | ||
warning_msg = "Request rate limited by Jira." | ||
|
||
warning_msg += ( | ||
f" Request should be retried after {retry_after} seconds.\n" | ||
if retry_after is not None | ||
else "\n" | ||
) | ||
if ( | ||
number_of_tokens_issued_per_interval is not None | ||
and token_issuing_rate_interval_seconds is not None | ||
): | ||
warning_msg += f"{number_of_tokens_issued_per_interval} tokens are issued every {token_issuing_rate_interval_seconds} seconds.\n" | ||
if maximum_number_of_tokens is not None: | ||
warning_msg += ( | ||
f"You can accumulate up to {maximum_number_of_tokens} tokens.\n" | ||
) | ||
warning_msg = ( | ||
warning_msg | ||
+ "Consider adding an exemption for the user as explained in: " | ||
+ "https://confluence.atlassian.com/adminjiraserver/improving-instance-stability-with-rate-limiting-983794911.html" | ||
) | ||
elif isinstance(response, Response): | ||
recoverable_error_codes = [ | ||
HTTPStatus.TOO_MANY_REQUESTS, | ||
HTTPStatus.SERVICE_UNAVAILABLE, | ||
] | ||
|
||
LOG.warning(warning_msg) | ||
if response.status_code in recoverable_error_codes: | ||
retry_after = response.headers.get("Retry-After") | ||
if retry_after: | ||
Comment on lines
+317
to
+319
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. this looks like regression: 429 should be still always retried 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. 429s are retried, just with a different delay as per previous behaviour -- see line 322 |
||
suggested_delay = int(retry_after) # Do as told | ||
elif response.status_code == HTTPStatus.TOO_MANY_REQUESTS: | ||
suggested_delay = 10 * 2**counter # Exponential backoff | ||
|
||
if response.status_code == HTTPStatus.TOO_MANY_REQUESTS: | ||
msg = f"{response.status_code} {response.reason}" | ||
self.__log_http_429_response(response) | ||
|
||
is_recoverable = suggested_delay > 0 | ||
if is_recoverable: | ||
# Exponential backoff with full jitter. | ||
delay = min(self.max_retry_delay, 10 * 2**counter) * random.random() | ||
# Apply jitter to prevent thundering herd | ||
delay = min(self.max_retry_delay, suggested_delay) * random.random() | ||
LOG.warning( | ||
f"Got recoverable error from {request_method} {url}, will retry [{counter}/{self.max_retries}] in {delay}s. Err: {msg}" # type: ignore[str-bytes-safe] | ||
) | ||
|
@@ -355,3 +341,39 @@ def __recoverable( | |
time.sleep(delay) | ||
|
||
return is_recoverable | ||
|
||
def __log_http_429_response(self, response: Response): | ||
retry_after = response.headers.get("Retry-After") | ||
number_of_tokens_issued_per_interval = response.headers.get( | ||
"X-RateLimit-FillRate" | ||
) | ||
token_issuing_rate_interval_seconds = response.headers.get( | ||
"X-RateLimit-Interval-Seconds" | ||
) | ||
maximum_number_of_tokens = response.headers.get("X-RateLimit-Limit") | ||
|
||
warning_msg = "Request rate limited by Jira." | ||
warning_msg += ( | ||
f" Request should be retried after {retry_after} seconds.\n" | ||
if retry_after is not None | ||
else "\n" | ||
) | ||
|
||
if ( | ||
number_of_tokens_issued_per_interval is not None | ||
and token_issuing_rate_interval_seconds is not None | ||
): | ||
warning_msg += f"{number_of_tokens_issued_per_interval} tokens are issued every {token_issuing_rate_interval_seconds} seconds.\n" | ||
|
||
if maximum_number_of_tokens is not None: | ||
warning_msg += ( | ||
f"You can accumulate up to {maximum_number_of_tokens} tokens.\n" | ||
) | ||
|
||
warning_msg = ( | ||
warning_msg | ||
+ "Consider adding an exemption for the user as explained in: " | ||
+ "https://confluence.atlassian.com/adminjiraserver/improving-instance-stability-with-rate-limiting-983794911.html" | ||
) | ||
|
||
LOG.warning(warning_msg) |
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.
right now this comment doesn't quite make sense
renaming this var to
recovery_delay
would make it much more readableThere 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.
what would you change the comment to? (it makes sense to me but I'm ok to add more meaning to it)
I kind of agree with
recovery_delay
, but then it won't eventually be the recovery delay because of the max limit.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.
then you go with -1 right? anyhow, this was just a minor suggestion, don't worry about it