MCPcopy Index your code
hub / github.com/google/go-github / checkRateLimitBeforeDo

Method checkRateLimitBeforeDo

github/github.go:1355–1390  ·  view source on GitHub ↗

checkRateLimitBeforeDo does not make any network calls, but uses existing knowledge from current client state in order to quickly check if *RateLimitError can be immediately returned from Client.Do, and if so, returns it so that Client.Do can skip making a network API call unnecessarily. Otherwise i

(req *http.Request, rateLimitCategory RateLimitCategory)

Source from the content-addressed store, hash-verified

1353// from Client.Do, and if so, returns it so that Client.Do can skip making a network API call unnecessarily.
1354// Otherwise it returns nil, and Client.Do should proceed normally.
1355func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory RateLimitCategory) *RateLimitError {
1356 ctx := req.Context()
1357
1358 c.rateMu.Lock()
1359 rate := c.rateLimits[rateLimitCategory]
1360 c.rateMu.Unlock()
1361 if !rate.Reset.Time.IsZero() && rate.Remaining == 0 && time.Now().Before(rate.Reset.Time) {
1362 // Create a fake response.
1363 resp := &http.Response{
1364 Status: http.StatusText(http.StatusForbidden),
1365 StatusCode: http.StatusForbidden,
1366 Request: req,
1367 Header: make(http.Header),
1368 Body: io.NopCloser(strings.NewReader("")),
1369 }
1370
1371 if ctx.Value(SleepUntilPrimaryRateLimitResetWhenRateLimited) != nil {
1372 if err := sleepUntilResetWithBuffer(ctx, rate.Reset.Time); err == nil {
1373 return nil
1374 }
1375 return &RateLimitError{
1376 Rate: rate,
1377 Response: resp,
1378 Message: fmt.Sprintf("Context cancelled while waiting for rate limit to reset until %v, not making remote request.", rate.Reset.Time),
1379 }
1380 }
1381
1382 return &RateLimitError{
1383 Rate: rate,
1384 Response: resp,
1385 Message: fmt.Sprintf("API rate limit of %v still exceeded until %v, not making remote request.", rate.Limit, rate.Reset.Time),
1386 }
1387 }
1388
1389 return nil
1390}
1391
1392// checkSecondaryRateLimitBeforeDo does not make any network calls, but uses existing knowledge from
1393// current client state in order to quickly check if *AbuseRateLimitError can be immediately returned

Callers 1

bareDoMethod · 0.95

Calls 3

LockMethod · 0.80
UnlockMethod · 0.80

Tested by

no test coverage detected