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(client): adjust retry behavior to be exponential backoff #149

Merged
merged 1 commit into from
Oct 24, 2023
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
10 changes: 5 additions & 5 deletions src/finch/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class BasePage(GenericModel, Generic[ModelT]):

Methods:
has_next_page(): Check if there is another page available
next_page_info(): Get the necesary information to make a request for the next page
next_page_info(): Get the necessary information to make a request for the next page
"""

_options: FinalRequestOptions = PrivateAttr()
Expand Down Expand Up @@ -691,15 +691,15 @@ def _calculate_retry_timeout(
return retry_after

initial_retry_delay = 0.5
max_retry_delay = 2.0
max_retry_delay = 8.0
nb_retries = max_retries - remaining_retries

# Apply exponential backoff, but not more than the max.
sleep_seconds = min(initial_retry_delay * pow(nb_retries - 1, 2), max_retry_delay)
sleep_seconds = min(initial_retry_delay * pow(2.0, nb_retries), max_retry_delay)

# Apply some jitter, plus-or-minus half a second.
jitter = random() - 0.5
timeout = sleep_seconds + jitter
jitter = 1 - 0.25 * random()
timeout = sleep_seconds * jitter
return timeout if timeout >= 0 else 0

def _should_retry(self, response: httpx.Response) -> bool:
Expand Down
48 changes: 26 additions & 22 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,28 +639,30 @@ class Model(BaseModel):
"remaining_retries,retry_after,timeout",
[
[3, "20", 20],
[3, "0", 2],
[3, "-10", 2],
[3, "0", 0.5],
[3, "-10", 0.5],
[3, "60", 60],
[3, "61", 2],
[3, "61", 0.5],
[3, "Fri, 29 Sep 2023 16:26:57 GMT", 20],
[3, "Fri, 29 Sep 2023 16:26:37 GMT", 2],
[3, "Fri, 29 Sep 2023 16:26:27 GMT", 2],
[3, "Fri, 29 Sep 2023 16:26:37 GMT", 0.5],
[3, "Fri, 29 Sep 2023 16:26:27 GMT", 0.5],
[3, "Fri, 29 Sep 2023 16:27:37 GMT", 60],
[3, "Fri, 29 Sep 2023 16:27:38 GMT", 2],
[3, "99999999999999999999999999999999999", 2],
[3, "Zun, 29 Sep 2023 16:26:27 GMT", 2],
[3, "", 2],
[3, "Fri, 29 Sep 2023 16:27:38 GMT", 0.5],
[3, "99999999999999999999999999999999999", 0.5],
[3, "Zun, 29 Sep 2023 16:26:27 GMT", 0.5],
[3, "", 0.5],
[2, "", 0.5 * 2.0],
[1, "", 0.5 * 4.0],
],
)
@mock.patch("time.time", mock.MagicMock(return_value=1696004797))
def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str, timeout: float) -> None:
client = Finch(base_url=base_url, access_token=access_token, _strict_response_validation=True)

headers = httpx.Headers({"retry-after": retry_after})
options = FinalRequestOptions(method="get", url="/foo", max_retries=2)
options = FinalRequestOptions(method="get", url="/foo", max_retries=3)
calculated = client._calculate_retry_timeout(remaining_retries, options, headers)
assert calculated == pytest.approx(timeout, 0.6) # pyright: ignore[reportUnknownMemberType]
assert calculated == pytest.approx(timeout, 0.5 * 0.875) # pyright: ignore[reportUnknownMemberType]


class TestAsyncFinch:
Expand Down Expand Up @@ -1272,18 +1274,20 @@ class Model(BaseModel):
"remaining_retries,retry_after,timeout",
[
[3, "20", 20],
[3, "0", 2],
[3, "-10", 2],
[3, "0", 0.5],
[3, "-10", 0.5],
[3, "60", 60],
[3, "61", 2],
[3, "61", 0.5],
[3, "Fri, 29 Sep 2023 16:26:57 GMT", 20],
[3, "Fri, 29 Sep 2023 16:26:37 GMT", 2],
[3, "Fri, 29 Sep 2023 16:26:27 GMT", 2],
[3, "Fri, 29 Sep 2023 16:26:37 GMT", 0.5],
[3, "Fri, 29 Sep 2023 16:26:27 GMT", 0.5],
[3, "Fri, 29 Sep 2023 16:27:37 GMT", 60],
[3, "Fri, 29 Sep 2023 16:27:38 GMT", 2],
[3, "99999999999999999999999999999999999", 2],
[3, "Zun, 29 Sep 2023 16:26:27 GMT", 2],
[3, "", 2],
[3, "Fri, 29 Sep 2023 16:27:38 GMT", 0.5],
[3, "99999999999999999999999999999999999", 0.5],
[3, "Zun, 29 Sep 2023 16:26:27 GMT", 0.5],
[3, "", 0.5],
[2, "", 0.5 * 2.0],
[1, "", 0.5 * 4.0],
],
)
@mock.patch("time.time", mock.MagicMock(return_value=1696004797))
Expand All @@ -1292,6 +1296,6 @@ async def test_parse_retry_after_header(self, remaining_retries: int, retry_afte
client = AsyncFinch(base_url=base_url, access_token=access_token, _strict_response_validation=True)

headers = httpx.Headers({"retry-after": retry_after})
options = FinalRequestOptions(method="get", url="/foo", max_retries=2)
options = FinalRequestOptions(method="get", url="/foo", max_retries=3)
calculated = client._calculate_retry_timeout(remaining_retries, options, headers)
assert calculated == pytest.approx(timeout, 0.6) # pyright: ignore[reportUnknownMemberType]
assert calculated == pytest.approx(timeout, 0.5 * 0.875) # pyright: ignore[reportUnknownMemberType]