Repository navigation
fix: bound rate-limit retries and honor Retry-After #134
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
Open
Shubham-Padkonde
wants to merge
2
commits into
chargebee:master
Choose a base branch
from
Shubham-Padkonde:fix/bounded-rate-limit-retries
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import asyncio | ||
| import datetime | ||
| import unittest | ||
| from unittest.mock import AsyncMock, Mock, patch | ||
|
|
||
| from chargebee.api_error import APIError | ||
| from chargebee.http_request import ( | ||
| _process_response, | ||
| MAX_RETRY_AFTER_MS, | ||
| _process_response_async, | ||
| parse_retry_after, | ||
| ) | ||
| from chargebee.retry_config import RetryConfig | ||
|
|
||
|
|
||
| class RateLimitRetryTests(unittest.TestCase): | ||
| def test_rate_limit_respects_retry_limit(self): | ||
| for asynchronous in (False, True): | ||
| for retries in (0, 2): | ||
| with self.subTest(asynchronous=asynchronous, retries=retries): | ||
| error = APIError(429, {"message": "limited"}, {"Retry-After": "2"}) | ||
| # A final successful response bounds the regression on broken code. | ||
| effects = [error] * (retries + 1) + [({}, {}, 200)] | ||
| request = ( | ||
| AsyncMock(side_effect=effects) | ||
| if asynchronous | ||
| else Mock(side_effect=effects) | ||
| ) | ||
| sleep = AsyncMock() if asynchronous else Mock() | ||
| suffix = "_async" if asynchronous else "" | ||
| with ( | ||
| patch("chargebee.http_request._make_request" + suffix, request), | ||
| patch("chargebee.http_request.sleep" + suffix, sleep), | ||
| ): | ||
| with self.assertRaises(APIError) as caught: | ||
| args = ( | ||
| "https://example.com", | ||
| {"method": "GET", "headers": {}}, | ||
| RetryConfig(enabled=True, max_retries=retries), | ||
| False, | ||
| ) | ||
| if asynchronous: | ||
| asyncio.run(_process_response_async(*args)) | ||
| else: | ||
| _process_response(*args) | ||
| self.assertIs(caught.exception, error) | ||
| self.assertEqual(request.call_count, retries + 1) | ||
| self.assertEqual(sleep.call_count, retries) | ||
| for call in sleep.call_args_list: | ||
| self.assertEqual(call.args, (2000,)) | ||
|
|
||
| def test_retry_after_from_api_error_headers(self): | ||
| for value, expected in ( | ||
| ("2", 2000), | ||
| ("0", 0), | ||
| ("-5", 0), | ||
| ("invalid", None), | ||
| ("60", MAX_RETRY_AFTER_MS), | ||
| ("61", None), | ||
| ("86400", None), | ||
| ): | ||
| with self.subTest(value=value): | ||
| error = APIError(429, {}, {"Retry-After": value}) | ||
| self.assertEqual(parse_retry_after(error), expected) | ||
|
|
||
| def test_retry_after_http_date(self): | ||
| error = APIError(429, {}, {"Retry-After": "Thu, 01 Oct 2026 12:00:02 GMT"}) | ||
| now = datetime.datetime(2026, 10, 1, 12, tzinfo=datetime.timezone.utc) | ||
|
|
||
| class FrozenDatetime(datetime.datetime): | ||
| @classmethod | ||
| def now(cls, tz=None): | ||
| return now | ||
|
|
||
| with patch("chargebee.http_request.datetime.datetime", FrozenDatetime): | ||
| self.assertEqual(parse_retry_after(error), 2000) | ||
|
|
||
| def test_retry_after_http_date_too_far_uses_default_delay(self): | ||
| error = APIError(429, {}, {"Retry-After": "Fri, 01 Oct 2027 12:00:00 GMT"}) | ||
| now = datetime.datetime(2026, 10, 1, 12, tzinfo=datetime.timezone.utc) | ||
|
|
||
| class FrozenDatetime(datetime.datetime): | ||
| @classmethod | ||
| def now(cls, tz=None): | ||
| return now | ||
|
|
||
| with patch("chargebee.http_request.datetime.datetime", FrozenDatetime): | ||
| self.assertIsNone(parse_retry_after(error)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.