Skip to content

Commit

Permalink
feat: use exponential backoff interval when retrying requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jul 23, 2024
1 parent c517c85 commit d2999c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions hcloud/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ class Client:
_version = __version__
__user_agent_prefix = "hcloud-python"

_retry_interval = 0.5
_retry_interval = exponential_backoff_function(
base=1.0,
multiplier=2,
cap=60.0,
)
_retry_max_retries = 5

def __init__(
Expand Down Expand Up @@ -289,7 +293,8 @@ def request( # type: ignore[no-untyped-def]
error["code"] == "rate_limit_exceeded"
and retries < self._retry_max_retries
):
time.sleep(retries * self._retry_interval)
# pylint: disable=too-many-function-args
time.sleep(self._retry_interval(retries))
retries += 1
continue

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_request_500_empty_content(self, client, fail_response):
assert str(error) == "Internal Server Error (500)"

def test_request_limit(self, client, rate_limit_response):
client._retry_interval = 0
client._retry_interval = constant_backoff_function(0.0)
client._requests_session.request.return_value = rate_limit_response
with pytest.raises(APIException) as exception_info:
client.request(
Expand All @@ -197,7 +197,7 @@ def test_request_limit(self, client, rate_limit_response):
assert error.message == "limit of 10 requests per hour reached"

def test_request_limit_then_success(self, client, rate_limit_response):
client._retry_interval = 0
client._retry_interval = constant_backoff_function(0.0)
response = requests.Response()
response.status_code = 200
response._content = json.dumps({"result": "data"}).encode("utf-8")
Expand Down

0 comments on commit d2999c8

Please sign in to comment.