Repository navigation
Expose Management API rate limit info via a client callback #774
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
ProdigyTom
wants to merge
11
commits into
auth0:master
Choose a base branch
from
ProdigyTom:feat/expose-rate-limit-data
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
11 commits
Select commit
Hold shift + click to select a range
df19130
Expose Management API rate limit info via a client callback (v6)
ProdigyTom 05bd194
Merge branch 'master' into feat/expose-rate-limit-data
kishore7snehil d42e861
Address review feedback on rate limit handler
ProdigyTom 0173174
Raise Auth0::Exception instead of Auth0::Unsupported for handler attach
ProdigyTom 2616a60
Merge branch 'master' into feat/expose-rate-limit-data
kishore7snehil c1409b0
Merge branch 'master' into feat/expose-rate-limit-data
kishore7snehil 76432fa
Address second review pass on rate limit handler
ProdigyTom 4a75791
Shorten rate_limit_handler README options row
ProdigyTom 0ff7e97
Address earlier review sub-batch on rate limit handler
ProdigyTom 1a338f9
Use an explicit nil check for the Authentication-path notify guard
ProdigyTom 4f54338
Merge branch 'master' into feat/expose-rate-limit-data
kishore7snehil 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
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,103 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Auth0 | ||
| module Internal | ||
| module Http | ||
| # Rate limit information parsed from the `x-ratelimit-*` headers Auth0 | ||
| # returns on API responses. | ||
| # | ||
| # @see https://auth0.com/docs/troubleshoot/customer-support/operational-policies/rate-limit-policy | ||
| class RateLimit | ||
| # @return [Integer, nil] the maximum number of requests allowed in the current window | ||
| attr_reader :limit | ||
| # @return [Integer, nil] the number of requests remaining in the current window | ||
| attr_reader :remaining | ||
| # @return [Time, nil] the UTC time at which the current window resets | ||
| attr_reader :reset | ||
|
|
||
| # @param limit [Integer, nil] | ||
| # @param remaining [Integer, nil] | ||
| # @param reset [Time, nil] | ||
| def initialize(limit:, remaining:, reset:) | ||
| @limit = limit | ||
| @remaining = remaining | ||
| @reset = reset | ||
| end | ||
|
|
||
| # Build from an object that exposes headers via `#[]` using the HTTP | ||
| # header name (e.g. a `Net::HTTPResponse`, whose `#[]` is | ||
| # case-insensitive). Used by the Management API (RawClient) path. | ||
| # | ||
| # @param response [#[]] responds to `[]` with header access | ||
| # @return [Auth0::Internal::Http::RateLimit] | ||
| def self.from_http_response(response) | ||
| build( | ||
| response["x-ratelimit-limit"], | ||
| response["x-ratelimit-remaining"], | ||
| response["x-ratelimit-reset"] | ||
| ) | ||
| end | ||
|
|
||
| # Build from a plain headers hash (e.g. RestClient's, whose keys are | ||
| # symbols like `:x_ratelimit_remaining`). Keys are matched | ||
| # case-insensitively and dash/underscore-agnostically. Used by the | ||
| # Authentication API (HTTPProxy) path. | ||
| # | ||
| # @param headers [Hash, nil] | ||
| # @return [Auth0::Internal::Http::RateLimit] | ||
| def self.from_headers(headers) | ||
| normalized = (headers || {}).each_with_object({}) do |(key, value), acc| | ||
| acc[key.to_s.downcase.tr("-", "_")] = value | ||
| end | ||
|
|
||
| build( | ||
| normalized["x_ratelimit_limit"], | ||
| normalized["x_ratelimit_remaining"], | ||
| normalized["x_ratelimit_reset"] | ||
| ) | ||
| end | ||
|
|
||
| # Invokes the handler with the given rate limit. A handler error must | ||
| # never break the caller's request, so it is rescued and warned rather | ||
| # than raised. Parsing happens before this call, so a parsing bug is not | ||
| # misattributed to the handler. | ||
| # | ||
| # @param handler [#call, nil] the configured rate limit handler | ||
| # @param rate_limit [Auth0::Internal::Http::RateLimit] | ||
| # @return [void] | ||
| def self.deliver(handler, rate_limit) | ||
| return if handler.nil? | ||
|
|
||
| handler.call(rate_limit) | ||
| rescue StandardError => e | ||
| warn "[auth0] rate_limit_handler raised #{e.class}: #{e.message}" | ||
| end | ||
|
|
||
| # @param limit [String, nil] raw `x-ratelimit-limit` value | ||
| # @param remaining [String, nil] raw `x-ratelimit-remaining` value | ||
| # @param reset [String, nil] raw `x-ratelimit-reset` value | ||
| # @return [Auth0::Internal::Http::RateLimit] | ||
| def self.build(limit, remaining, reset) | ||
|
kishore7snehil marked this conversation as resolved.
|
||
| reset_epoch = to_integer(reset) | ||
|
|
||
| new( | ||
| limit: to_integer(limit), | ||
| remaining: to_integer(remaining), | ||
| reset: reset_epoch.nil? ? nil : Time.at(reset_epoch).utc | ||
| ) | ||
| end | ||
| private_class_method :build | ||
|
|
||
| # Parse an integer header value, returning nil for blank or non-numeric | ||
| # input (so a malformed header is never silently reported as 0). | ||
| # | ||
| # @param value [String, nil] the raw header value | ||
| # @return [Integer, nil] | ||
| def self.to_integer(value) | ||
|
kishore7snehil marked this conversation as resolved.
|
||
| Integer(value.to_s.strip, exception: false) | ||
| end | ||
| private_class_method :to_integer | ||
| end | ||
| end | ||
| end | ||
| end | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.