Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use exponential backoff when retrying requests #417

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions hcloud/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ 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, jitter=True
)
_retry_max_retries = 5

def __init__(
Expand Down Expand Up @@ -289,7 +291,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