From edb51b3d8727ccfab578f314a7b296905341643b Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Fri, 13 Dec 2024 13:13:00 +0000 Subject: [PATCH 1/2] export `BypassRateLimitCheck` to allow toggling of rate limit checks by consumers --- github/github.go | 4 ++-- github/github_test.go | 2 +- github/rate_limit.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/github/github.go b/github/github.go index 7cdd73eb25e..b36a455527c 100644 --- a/github/github.go +++ b/github/github.go @@ -801,7 +801,7 @@ func parseTokenExpiration(r *http.Response) Timestamp { type requestContext uint8 const ( - bypassRateLimitCheck requestContext = iota + BypassRateLimitCheck requestContext = iota SleepUntilPrimaryRateLimitResetWhenRateLimited ) @@ -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{ diff --git a/github/github_test.go b/github/github_test.go index 43c9bde4f82..f2957242d78 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -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 { diff --git a/github/rate_limit.go b/github/rate_limit.go index 5b01b573d8a..d55c545e7a5 100644 --- a/github/rate_limit.go +++ b/github/rate_limit.go @@ -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 From 9b253b9df8d0b72d6e4ecd4ed86cdad2e87f594e Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Mon, 16 Dec 2024 23:09:20 +0000 Subject: [PATCH 2/2] doc comment for `BypassRateLimitCheck` (context key) --- github/github.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/github/github.go b/github/github.go index b36a455527c..ebb56605e4f 100644 --- a/github/github.go +++ b/github/github.go @@ -801,7 +801,11 @@ func parseTokenExpiration(r *http.Response) Timestamp { type requestContext uint8 const ( + // BypassRateLimitCheck prevents a pre-emptive check for exceeded primary rate limits + // Specify this by providing a context with this key, e.g. + // context.WithValue(context.Background(), github.BypassRateLimitCheck, true) BypassRateLimitCheck requestContext = iota + SleepUntilPrimaryRateLimitResetWhenRateLimited )