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

use Okta's response headers for rate limits #11

Closed
gabrielsroka opened this issue Mar 28, 2024 · 1 comment · Fixed by #63
Closed

use Okta's response headers for rate limits #11

gabrielsroka opened this issue Mar 28, 2024 · 1 comment · Fixed by #63

Comments

@gabrielsroka
Copy link

gabrielsroka commented Mar 28, 2024

transcript of (Gabriel's half of) a conversation with @somethingnew2-0

why do u use exponential backoff when Okta tells you when u can retry the api call?

async def _retry(func: Callable[[Any], Any], *args: Any, **kwargs: Any) -> Any:
"""Retry Okta API requests with specific status codes using exponential backoff."""
for attempt in range(1 + REQUEST_MAX_RETRIES):
result = await func(*args, **kwargs)
if len(result) == 2:
response, error = result
elif len(result) == 3:
_, response, error = result
else:
raise Exception("Unexpected result structure from Okta client.")
if (attempt == REQUEST_MAX_RETRIES or
error is None or
response is None or
(response is not None and response.get_status() not in RETRIABLE_STATUS_CODES)):
return result
if response is None:
logger.warning('Got None response from Okta resource. Retrying...')
else:
logger.warning(f'Got {response.get_status()} response from Okta resource {response._url}, with error:'
f' {error}. Retrying...'
)
await asyncio.sleep(RETRY_BACKOFF_FACTOR * (2**attempt))

if u get a 429 error that tells u when to retry, why not look at those headers?
eg (and this isn't perfect, but...)
https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/console/index.html#L169-L187

ie, if u reach the rate limit at 10:00:00 and it tells u to retry at 10:01:00, there's no point in retrying at 10:00:01.2, 10:00:02.4, 10:00:04.8. ur just gonna get more errors

Okta provides three headers in each response to report on both concurrent and org-wide rate limits.
For org-wide rate limits, the three headers show the limit that is being enforced, when it resets, and how close you are to hitting the limit:
X-Rate-Limit-Limit - the rate limit ceiling that is applicable for the current request.
X-Rate-Limit-Remaining - the number of requests left for the current rate-limit window.
X-Rate-Limit-Reset - the time at which the rate limit resets, specified in UTC epoch time (in seconds).

https://developer.okta.com/docs/reference/rl-best-practices/#check-your-rate-limits-with-okta-s-rate-limit-headers

@exitcode0
Copy link

Perhaps _retry() can use the Okta python SDK's built-in 429 retry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants