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

Bug: Too many requests error missed retry #279

Merged
merged 2 commits into from
Nov 8, 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
4 changes: 2 additions & 2 deletions tosfs/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ def _get_sleep_time(err: TosError, retry_count: int) -> float:
err.status_code == TOO_MANY_REQUESTS_CODE
or err.status_code == SERVICE_UNAVAILABLE
)
and "retry-after" in err.headers
and "retry-after" in err.header
):
try:
sleep_time = max(int(err.headers["retry-after"]), int(sleep_time))
sleep_time = max(int(err.header["retry-after"]), int(sleep_time))
except Exception as e:
from tosfs.core import logger

Expand Down
37 changes: 36 additions & 1 deletion tosfs/tests/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from tos.http import Response
from urllib3.exceptions import ProtocolError

from tosfs.retry import is_retryable_exception
from tosfs.retry import _get_sleep_time, is_retryable_exception

mock_resp = Mock(spec=requests.Response)

Expand Down Expand Up @@ -86,3 +86,38 @@ def test_is_retry_exception(
expected,
):
assert is_retryable_exception(exception) == expected


def test_get_sleep_time():
mock_resp = Mock(spec=requests.Response)

sleep_time = 1000

mock_resp.status_code = 429
mock_resp.headers = {
"Content-Type": "application/json",
"Date": "Thu, 07 Nov 2024 17:19:56 GMT",
"Retry-After": str(sleep_time),
"Server": "TosServer",
"x-tos-ec": "0004-00000007",
"x-tos-id-2": "0000-00000-0000",
"x-tos-request-id": "0000-0000-0000",
"x-tos-server-time": "1",
"Content-Length": "277",
}
mock_resp.iter_content = Mock(return_value=[b"chunk1", b"chunk2", b"chunk3"])
mock_resp.json = Mock(return_value={"key": "value"})

response = Response(mock_resp)

err = TosServerError(
response,
"Exceed account external rate limit. Too much throughput in a "
"short period of time, please slow down.",
"ExceedAccountExternalRateLimit",
"KmsJSKDKhjasdlKmsduwRETYHB",
"",
"0004-00000001",
)

assert _get_sleep_time(err, 1) == sleep_time