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

Permit toggling rate limit check by consumers #3386

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ func parseTokenExpiration(r *http.Response) Timestamp {
type requestContext uint8

const (
bypassRateLimitCheck requestContext = iota
BypassRateLimitCheck requestContext = iota
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is BypassRateLimitCheck exported? Can't it remain bypassRateLimitCheck?

Copy link
Contributor

@tomfeigin tomfeigin Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I missed that you wanted to trigger this from outside the library.
I suggest that there will be wrapper functions like:

func ContextWithBypassRateLimitCheck(ctx context.Context) context.Context {
    return context.WithValue(ctx, bypassRateLimitCheck, struct{}{})
}

You don't even need to export the getter function for this value.

Anyway, as the code is now is fine

SleepUntilPrimaryRateLimitResetWhenRateLimited
)

Expand All @@ -822,7 +822,7 @@ func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, erro

rateLimitCategory := GetRateLimitCategory(req.Method, req.URL.Path)

if bypass := ctx.Value(bypassRateLimitCheck); bypass == nil {
if bypass := ctx.Value(BypassRateLimitCheck); bypass == nil {
// If we've hit rate limit, don't make further requests before Reset time.
if err := c.checkRateLimitBeforeDo(req, rateLimitCategory); err != nil {
return &Response{
Expand Down
2 changes: 1 addition & 1 deletion github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func testNewRequestAndDoFailureCategory(t *testing.T, methodName string, client
client.BaseURL.Path = "/api-v3/"
client.rateLimits[category].Reset.Time = time.Now().Add(10 * time.Minute)
resp, err = f()
if bypass := resp.Request.Context().Value(bypassRateLimitCheck); bypass != nil {
if bypass := resp.Request.Context().Value(BypassRateLimitCheck); bypass != nil {
return
}
if want := http.StatusForbidden; resp == nil || resp.Response.StatusCode != want {
Expand Down
2 changes: 1 addition & 1 deletion github/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *RateLimitService) Get(ctx context.Context) (*RateLimits, *Response, err
})

// This resource is not subject to rate limits.
ctx = context.WithValue(ctx, bypassRateLimitCheck, true)
ctx = context.WithValue(ctx, BypassRateLimitCheck, true)
resp, err := s.client.Do(ctx, req, response)
if err != nil {
return nil, resp, err
Expand Down
Loading