Repository navigation
Expose Management API rate limit info via a client callback - #774
ProdigyTom wants to merge 3 commits into
Conversation
|
@ProdigyTom I can see some merge conflicts. Can you please resolve them? Post that I can take a look |
1943366 to
f119645
Compare
|
@kishore7snehil my original change was against v5 and after the change to v6 I wasn't really able to resolve my merge conflicts in a way that still solved our problem. The rate limiting information gets dropped earlier in some generated code. So I wound up going a slightly different direction. let me know if it makes sense |
Exposes Auth0's rate limit info (x-ratelimit-limit / -remaining / -reset) from every Management API response via an opt-in callback, so callers can monitor how close they are to the limit (per auth0#606). - Add Auth0::Internal::Http::RateLimit (limit/remaining/reset; blank and non-numeric header values become nil rather than 0) - RawClient gains a rate_limit_handler, invoked in #send after retries on every response; handler errors are swallowed so they can't break a request. #send's return value is unchanged, so generated callers are unaffected. - Wire it through the custom client: Auth0::Client.new(management_rate_limit_handler:) attaches the handler to the management raw client - Unit tests for RateLimit, RawClient#send handler behavior, and client wiring All changes live in fernignored files, so they survive regeneration. Refs auth0#606.
f119645 to
df19130
Compare
| def attach_rate_limit_handler(management) | ||
| return if @management_rate_limit_handler.nil? | ||
|
|
||
| raw_client = management.instance_variable_get(:@raw_client) |
There was a problem hiding this comment.
This reaches into the generated Management client for its @raw_client by instance variable, and the if raw_client means that if a future regeneration renames or restructures that variable, the handler just never gets attached and the feature goes quiet with no error anywhere. This file is kept across regeneration but the generated class it reaches into is not, so the two can drift apart and nothing would flag it.
We should be handling this carefully.
There was a problem hiding this comment.
I have made this raise a Auth0::Unsupported error for now so that a future internals change surfaces immediately instead of silently skipping.
I'm using the ivar because the generated Auth0::Management doesn't expose its raw client, and since that class is regenerated, we can't add an accessor from our side without it being clobbered. If you'd be open to exposing a public reader for the raw client on the generated Management class, I would switch to it and drop the instance_variable_get entirely.
There was a problem hiding this comment.
A public reader on Auth0::Management would be the cleaner fix, but that class is generated, so a hand edit would be overwritten on the next regen. It would need to come from the Fern side, so not blocking this PR on it.
The respond_to? check with the raise is enough for now, since a rename would fail loudly instead of silently dropping the handler.
One small question on the error. Auth0::Unsupported is part of the HTTP error family, and this isn't an HTTP failure. Would a plain Auth0::Exception fit better here?
- Notify on every response (inside the retry loop) so intermediate 429s reach the handler, not just the final response - Fail loud if the handler can't be attached to the management raw client (raise) instead of silently no-opping if internals drift - Warn (but still swallow) when a handler raises, so broken monitoring is visible - Extend support to the Authentication API path (HTTPProxy), via a single rate_limit_handler option covering both APIs - RateLimit gains from_http_response (Net::HTTP) and from_headers (RestClient) builders; reset/whitespace parsing pinned - Tests: retried-request notification, token-rebuild re-attach, case-insensitive header lookup, reset garbage/whitespace, Authentication-path handler - Docs: README options row + Rate Limit Monitoring section, EXAMPLES snippet
Changes
Exposes Auth0's rate-limit information (
x-ratelimit-limit/-remaining/-reset) from every Management and Authentication API response, via an opt-in handler, so callers can monitor how close they are to the limit (the ask in #606).Auth0::Internal::Http::RateLimit— value object (limit,remainingas Integers;resetas a UTCTime). Blank/non-numeric header values becomenil(never a misleading0). Two builders:from_http_response(Management/RawClient,Net::HTTP) andfrom_headers(Authentication/HTTPProxy, RestClient).rate_limit_handleroption onAuth0::Client, invoked with the parsedRateLimitafter every response on both API paths.429s that trigger an automatic retry, so a handler watchingremainingsees the point where it ran out.warned (so broken monitoring is visible) but never breaks a request.lib/auth0/internal/**,lib/auth0/mixins/**,lib/auth0/auth_client.rb), so they survive regeneration.References
Testing
Unit tests under
test/unit/cover header parsing (symbol/dashed/mixed-case keys,0vs blank/non-numeric, reset + whitespace), notification on every response across a retried request, the handler error being swallowed, the Authentication-API path, and the client wiring (including re-attach after a token-triggered rebuild). Fullrake testpasses locally with no failures.Checklist