-
Notifications
You must be signed in to change notification settings - Fork 937
chore: instrument github oauth2 limits #11532
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
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,100 @@ | ||
package promoauth | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type rateLimits struct { | ||
Limit int | ||
Remaining int | ||
Used int | ||
Reset time.Time | ||
Resource string | ||
} | ||
|
||
// githubRateLimits checks the returned response headers and | ||
func githubRateLimits(resp *http.Response, err error) (rateLimits, bool) { | ||
if err != nil || resp == nil { | ||
return rateLimits{}, false | ||
} | ||
|
||
p := headerParser{header: resp.Header} | ||
// See | ||
// https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#checking-the-status-of-your-rate-limit | ||
limits := rateLimits{ | ||
Limit: p.int("x-ratelimit-limit"), | ||
Remaining: p.int("x-ratelimit-remaining"), | ||
Used: p.int("x-ratelimit-used"), | ||
Resource: p.string("x-ratelimit-resource"), | ||
} | ||
|
||
if limits.Limit == 0 && | ||
limits.Remaining == 0 && | ||
limits.Used == 0 { | ||
// For some requests, github has no rate limit. In which case, | ||
// it returns all 0s. We can just omit these. | ||
return limits, false | ||
} | ||
|
||
// Reset is when the rate limit "used" will be reset to 0. | ||
// If it's unix 0, then we do not know when it will reset. | ||
// Change it to a zero time as that is easier to handle in golang. | ||
unix := p.int("x-ratelimit-reset") | ||
resetAt := time.Unix(int64(unix), 0) | ||
if unix == 0 { | ||
resetAt = time.Time{} | ||
} | ||
limits.Reset = resetAt | ||
|
||
// Unauthorized requests have their own rate limit, so we should | ||
// track them separately. | ||
if resp.StatusCode == http.StatusUnauthorized { | ||
limits.Resource += "-unauthorized" | ||
} | ||
|
||
// A 401 or 429 means too many requests. This might mess up the | ||
// "resource" string because we could hit the unauthorized limit, | ||
// and we do not want that to override the authorized one. | ||
// However, in testing, it seems a 401 is always a 401, even if | ||
// the limit is hit. | ||
|
||
if len(p.errors) > 0 { | ||
// If we are missing any headers, then do not try and guess | ||
// what the rate limits are. | ||
return limits, false | ||
} | ||
return limits, true | ||
} | ||
|
||
type headerParser struct { | ||
errors map[string]error | ||
header http.Header | ||
} | ||
|
||
func (p *headerParser) string(key string) string { | ||
if p.errors == nil { | ||
p.errors = make(map[string]error) | ||
} | ||
|
||
v := p.header.Get(key) | ||
if v == "" { | ||
p.errors[key] = fmt.Errorf("missing header %q", key) | ||
} | ||
return v | ||
} | ||
|
||
func (p *headerParser) int(key string) int { | ||
v := p.string(key) | ||
if v == "" { | ||
return -1 | ||
} | ||
|
||
i, err := strconv.Atoi(v) | ||
if err != nil { | ||
p.errors[key] = err | ||
} | ||
return i | ||
} |
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.
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.