Skip to content
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 8 commits into from
Dec 21, 2023
Prev Previous commit
Next Next commit
Added use of HTTPStatus enum
  • Loading branch information
Gael Mainguet committed Aug 15, 2023
commit 55f8203636cb05921d2863879c915433e3320cc3
27 changes: 17 additions & 10 deletions jira/resilientsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import random
import time
from typing import Any
from http import HTTPStatus

from requests import Response, Session
from requests.exceptions import ConnectionError
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -306,15 +307,21 @@ def __recoverable(
)

elif isinstance(response, Response):
retry_after = response.headers.get('Retry-After')
if retry_after:
suggested_delay = int(retry_after) # Do as told
elif response.status_code == 429:
suggested_delay = 10 * 2**counter # Exponential backoff

if response.status_code == 429:
msg = f"{response.status_code} {response.reason}"
self.__log_http_429_response(response)
recoverable_error_codes = [
HTTPStatus.TOO_MANY_REQUESTS,
HTTPStatus.SERVICE_UNAVAILABLE
]

if response.status_code in recoverable_error_codes:
retry_after = response.headers.get('Retry-After')
if retry_after:
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:
Expand Down