Skip to content

Commit 4999a76

Browse files
Improve handling of Jira's retry-after handling (#1825)
* Respect the "Retry-After" times requested by Jira The time Jira sends in the Retry-After header is the minimum time Jira wants us to wait before retrying our request. However, the former implementation used this as a maximum waiting time for the next request. In result, there was a chance that we reached three retries without reaching the time that Jira expected us to wait and our request would fail. This implementation does also affect the other retry cases, as while previously we jittered our backoff between 0 and the target backoff, we now only jitter between 50% and 100% of the target backoff. However, this should still protect us from thundering herds and safes us from introducing a new minimum backoff variable for the retry-after case. * Also retry requests where Jira specifies a Retry-after of 0 seconds When rejecting request with a 429 response, Jira sometimes sends a Retry-after header asking for a backoff of 0 seconds. With the existing retry logic this would mark the request as non-retryable and thus fail the request. With this change, such requests are treated as if Jira had send a retry-after value of 1 second.
1 parent ff6985b commit 4999a76

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

jira/resilientsession.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ def __recoverable(
315315
if response.status_code in recoverable_error_codes:
316316
retry_after = response.headers.get("Retry-After")
317317
if retry_after:
318-
suggested_delay = int(retry_after) # Do as told
318+
suggested_delay = 2 * max(
319+
int(retry_after), 1
320+
) # Do as told but always wait at least a little
319321
elif response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
320322
suggested_delay = 10 * 2**counter # Exponential backoff
321323

@@ -326,7 +328,9 @@ def __recoverable(
326328
is_recoverable = suggested_delay > 0
327329
if is_recoverable:
328330
# Apply jitter to prevent thundering herd
329-
delay = min(self.max_retry_delay, suggested_delay) * random.random()
331+
delay = min(self.max_retry_delay, suggested_delay) * random.uniform(
332+
0.5, 1.0
333+
)
330334
LOG.warning(
331335
f"Got recoverable error from {request_method} {url}, will retry [{counter}/{self.max_retries}] in {delay}s. Err: {msg}" # type: ignore[str-bytes-safe]
332336
)

tests/test_resilientsession.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,17 @@ def tearDown(self):
6565

6666

6767
# Retry test data tuples: (status_code, with_rate_limit_header, with_retry_after_header, retry_expected)
68-
with_rate_limit = with_retry_after = True
69-
without_rate_limit = without_retry_after = False
68+
with_rate_limit = True
69+
with_retry_after = 1
70+
without_rate_limit = False
71+
without_retry_after = None
7072
status_codes_retries_test_data = [
7173
# Always retry 429 responses
7274
(HTTPStatus.TOO_MANY_REQUESTS, with_rate_limit, with_retry_after, True),
75+
(HTTPStatus.TOO_MANY_REQUESTS, with_rate_limit, 0, True),
7376
(HTTPStatus.TOO_MANY_REQUESTS, with_rate_limit, without_retry_after, True),
7477
(HTTPStatus.TOO_MANY_REQUESTS, without_rate_limit, with_retry_after, True),
78+
(HTTPStatus.TOO_MANY_REQUESTS, without_rate_limit, 0, True),
7579
(HTTPStatus.TOO_MANY_REQUESTS, without_rate_limit, without_retry_after, True),
7680
# Retry 503 responses only when 'Retry-After' in headers
7781
(HTTPStatus.SERVICE_UNAVAILABLE, with_rate_limit, with_retry_after, True),
@@ -103,10 +107,11 @@ def test_status_codes_retries(
103107
mocked_request_method: Mock,
104108
status_code: int,
105109
with_rate_limit_header: bool,
106-
with_retry_after_header: bool,
110+
with_retry_after_header: int | None,
107111
retry_expected: bool,
108112
):
109-
RETRY_AFTER_HEADER = {"Retry-After": "1"}
113+
RETRY_AFTER_SECONDS = with_retry_after_header or 0
114+
RETRY_AFTER_HEADER = {"Retry-After": f"{RETRY_AFTER_SECONDS}"}
110115
RATE_LIMIT_HEADERS = {
111116
"X-RateLimit-FillRate": "1",
112117
"X-RateLimit-Interval-Seconds": "1",
@@ -124,7 +129,7 @@ def test_status_codes_retries(
124129

125130
mocked_response: Response = Response()
126131
mocked_response.status_code = status_code
127-
if with_retry_after_header:
132+
if with_retry_after_header is not None:
128133
mocked_response.headers.update(RETRY_AFTER_HEADER)
129134
if with_rate_limit_header:
130135
mocked_response.headers.update(RATE_LIMIT_HEADERS)
@@ -141,6 +146,11 @@ def test_status_codes_retries(
141146
assert mocked_request_method.call_count == expected_number_of_requests
142147
assert mocked_sleep_method.call_count == expected_number_of_sleep_invocations
143148

149+
for actual_sleep in (
150+
call_args.args[0] for call_args in mocked_sleep_method.call_args_list
151+
):
152+
assert actual_sleep >= RETRY_AFTER_SECONDS
153+
144154

145155
errors_parsing_test_data = [
146156
(403, {"x-authentication-denied-reason": "err1"}, "", ["err1"]),

0 commit comments

Comments
 (0)