Skip to content

Commit 353d4fe

Browse files
Stefano Borierostefanoboriero
authored andcommitted
ISSUE-925: Handle rate limit error
* enable retries of 5xx status codes * add status code 429 to the list of retriable errors * remove status code 401 from the list of retriable errors by resilientsession FIXES #925
1 parent e00c1f7 commit 353d4fe

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

jira/resilientsession.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def raise_on_error(r: Optional[Response], verb="???", **kwargs):
8888
class ResilientSession(Session):
8989
"""This class is supposed to retry requests that do return temporary errors.
9090
91-
At this moment it supports: 502, 503, 504
91+
At this moment it supports: 429, 502, 503, 504
9292
"""
9393

9494
def __init__(self, timeout=None):
@@ -113,11 +113,10 @@ def __recoverable(
113113
f"Got ConnectionError [{response}] errno:{response.errno} on {request} {url}\n{vars(response)}\n{response.__dict__}"
114114
)
115115
if isinstance(response, Response):
116-
if response.status_code in [502, 503, 504, 401]:
117-
# 401 UNAUTHORIZED still randomly returned by Atlassian Cloud as of 2017-01-16
116+
if response.status_code in [429, 502, 503, 504]:
117+
# 401 UNAUTHORIZED still randomly returned by Atlassian Cloud as of 2017-01-16 and are handled by jira.JiraCookieAuth.handle_401
118118
msg = f"{response.status_code} {response.reason}"
119-
# 2019-07-25: Disabled recovery for codes above^
120-
return False
119+
return True
121120
elif not (
122121
response.status_code == 200
123122
and len(response.content) == 0

tests/test_resilientsession.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import logging
2+
from unittest.mock import Mock, patch
3+
4+
import pytest
5+
from requests import Response
26

37
import jira.resilientsession
8+
from jira.exceptions import JIRAError
49
from tests.conftest import JiraTestCase
510

611

@@ -53,3 +58,16 @@ def test_logging_with_connection_error(self):
5358
def tearDown(self):
5459
jira.resilientsession.logging.getLogger().removeHandler(self.loggingHandler)
5560
del self.loggingHandler
61+
62+
63+
@patch("requests.Session.get")
64+
def test_throttling_error_is_retried(mocked_method: Mock):
65+
mocked_throttled_response: Response = Response()
66+
mocked_throttled_response.status_code = 429
67+
mocked_method.return_value = mocked_throttled_response
68+
session: jira.resilientsession.ResilientSession = (
69+
jira.resilientsession.ResilientSession()
70+
)
71+
with pytest.raises(JIRAError):
72+
session.get("mocked_url")
73+
assert mocked_method.call_count == 4

0 commit comments

Comments
 (0)