Skip to content

Commit

Permalink
ISSUE-925: Handle rate limit error
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Stefano Boriero authored and stefanoboriero committed Apr 19, 2022
1 parent e00c1f7 commit 353d4fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 4 additions & 5 deletions jira/resilientsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def raise_on_error(r: Optional[Response], verb="???", **kwargs):
class ResilientSession(Session):
"""This class is supposed to retry requests that do return temporary errors.
At this moment it supports: 502, 503, 504
At this moment it supports: 429, 502, 503, 504
"""

def __init__(self, timeout=None):
Expand All @@ -113,11 +113,10 @@ def __recoverable(
f"Got ConnectionError [{response}] errno:{response.errno} on {request} {url}\n{vars(response)}\n{response.__dict__}"
)
if isinstance(response, Response):
if response.status_code in [502, 503, 504, 401]:
# 401 UNAUTHORIZED still randomly returned by Atlassian Cloud as of 2017-01-16
if response.status_code in [429, 502, 503, 504]:
# 401 UNAUTHORIZED still randomly returned by Atlassian Cloud as of 2017-01-16 and are handled by jira.JiraCookieAuth.handle_401
msg = f"{response.status_code} {response.reason}"
# 2019-07-25: Disabled recovery for codes above^
return False
return True
elif not (
response.status_code == 200
and len(response.content) == 0
Expand Down
18 changes: 18 additions & 0 deletions tests/test_resilientsession.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import logging
from unittest.mock import Mock, patch

import pytest
from requests import Response

import jira.resilientsession
from jira.exceptions import JIRAError
from tests.conftest import JiraTestCase


Expand Down Expand Up @@ -53,3 +58,16 @@ def test_logging_with_connection_error(self):
def tearDown(self):
jira.resilientsession.logging.getLogger().removeHandler(self.loggingHandler)
del self.loggingHandler


@patch("requests.Session.get")
def test_throttling_error_is_retried(mocked_method: Mock):
mocked_throttled_response: Response = Response()
mocked_throttled_response.status_code = 429
mocked_method.return_value = mocked_throttled_response
session: jira.resilientsession.ResilientSession = (
jira.resilientsession.ResilientSession()
)
with pytest.raises(JIRAError):
session.get("mocked_url")
assert mocked_method.call_count == 4

0 comments on commit 353d4fe

Please sign in to comment.