Skip to content

Commit b711121

Browse files
Stefano Borierostefanoboriero
Stefano Boriero
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 pycontribs#925
1 parent e00c1f7 commit b711121

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

jira/resilientsession.py

+4-5
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

+16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import logging
22

3+
import pytest
4+
from jira.exceptions import JIRAError
5+
36
import jira.resilientsession
47
from tests.conftest import JiraTestCase
8+
from requests import Response
9+
from unittest.mock import Mock, patch
510

611

712
class ListLoggingHandler(logging.Handler):
@@ -53,3 +58,14 @@ 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(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 = jira.resilientsession.ResilientSession()
69+
with pytest.raises(JIRAError):
70+
session.get('mocked_url')
71+
assert mocked_method.call_count == 4

0 commit comments

Comments
 (0)