Repository navigation
fix: honor Retry-After delay seconds #340
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
base: main
Are you sure you want to change the base?
Changes from all commits
ec88544
8cdeacc
29ab01b
9ec3fdb
22c1a2a
3e30717
d98d36f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| import time | ||
| import urllib | ||
|
|
||
| from email.utils import parsedate_to_datetime | ||
| from multiprocessing.pool import ThreadPool | ||
|
|
||
| from dateutil.parser import parse # type: ignore[import-untyped] | ||
|
|
@@ -17,6 +18,7 @@ | |
| from openfga_sdk.configuration import Configuration | ||
| from openfga_sdk.constants import ( | ||
| MAX_BACKOFF_TIME_IN_SEC, | ||
| RETRY_AFTER_HEADER_NAME, | ||
| RETRY_HEADER_MAX_ALLOWABLE_DURATION_IN_SEC, | ||
| ) | ||
| from openfga_sdk.exceptions import ( | ||
|
|
@@ -299,6 +301,7 @@ def __call_api( | |
| configuration=self.configuration.telemetry, | ||
| ) | ||
|
|
||
| wait_time_in_sec: float | ||
| try: | ||
| wait_time_in_sec = self._parse_retry_after_header(e.header) | ||
| except ValueError: | ||
|
|
@@ -424,23 +427,24 @@ def __call_api( | |
| return (return_data, response_data.status, response_data.headers) | ||
|
|
||
| def _parse_retry_after_header(self, headers) -> int: | ||
| retry_after_header = headers.get("retry-after") | ||
| retry_after_header = headers.get(RETRY_AFTER_HEADER_NAME.lower()) | ||
| if not retry_after_header: | ||
| raise ValueError("Retry-After header is not present") | ||
|
|
||
| # RFC 9110: Retry-After is either delay-seconds or an HTTP-date | ||
| try: | ||
| parsed_http_date = self.__deserialize_datetime(retry_after_header).replace( | ||
| tzinfo=datetime.timezone.utc | ||
| ) | ||
| now = datetime.datetime.now(datetime.timezone.utc) | ||
| wait_time_in_sec = (parsed_http_date - now).total_seconds() | ||
| except ApiException: | ||
| wait_time_in_sec = int(retry_after_header) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. THIS IS NOT A BLOCKING ISSUE Now that we're first checking if Retry-After header contains seconds first before checking for date, the HTTP spec writes the seconds form as delay-seconds = 1*DIGIT, which means "one or more of the digits 0–9 and nothing else." But int() also accepts +60, 1_0 (read as 10) and non-ASCII digits. I checked over real HTTP: +60 and 1_0 are honored by both clients. ٦٠ is honored as 60s by the async client, but the sync client falls back to backoff, so the two clients behave differently. The 1–1800s cap limits the impact, but checking for ASCII digits first would match the spec and keep both clients consistent, something like this (and indent the HTTP-date branch under else:, in both api_client.py and sync/api_client.py). Adding "+60" and "1_0" to test_parse_retry_after_rejects_invalid would cover it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SoulPancake take a look at this also. If you want to go by this approach or not.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like no real server sends non-aSCII digits though 🤔
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to keep int() here since the JS, Go, Java and .NET SDKs all accept +60 too, and the 1 to 1800s range limits the impact
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can standardise across SDKs later if we want to have this @cluelessno1 |
||
| except ValueError: | ||
| try: | ||
| retry_at = parsedate_to_datetime(retry_after_header) | ||
|
SoulPancake marked this conversation as resolved.
Resolved
|
||
| except (OverflowError, ValueError) as e: | ||
| raise ValueError("Retry-After header is invalid") from e | ||
| if retry_at.tzinfo is None: | ||
| retry_at = retry_at.replace(tzinfo=datetime.timezone.utc) | ||
| now = datetime.datetime.now(datetime.timezone.utc) | ||
| wait_time_in_sec = (retry_at - now).total_seconds() | ||
|
|
||
| if ( | ||
| wait_time_in_sec > RETRY_HEADER_MAX_ALLOWABLE_DURATION_IN_SEC | ||
| or wait_time_in_sec < 1 | ||
| ): | ||
| if not 1 <= wait_time_in_sec <= RETRY_HEADER_MAX_ALLOWABLE_DURATION_IN_SEC: | ||
|
SoulPancake marked this conversation as resolved.
Resolved
|
||
| raise ValueError("Retry-After header is invalid") | ||
|
|
||
| return math.ceil(wait_time_in_sec) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.