checkSecondaryRateLimitBeforeDo does not make any network calls, but uses existing knowledge from current client state in order to quickly check if *AbuseRateLimitError can be immediately returned from Client.Do, and if so, returns it so that Client.Do can skip making a network API call unnecessaril
(req *http.Request)
| 1394 | // from Client.Do, and if so, returns it so that Client.Do can skip making a network API call unnecessarily. |
| 1395 | // Otherwise it returns nil, and Client.Do should proceed normally. |
| 1396 | func (c *Client) checkSecondaryRateLimitBeforeDo(req *http.Request) *AbuseRateLimitError { |
| 1397 | c.rateMu.Lock() |
| 1398 | secondary := c.secondaryRateLimitReset |
| 1399 | c.rateMu.Unlock() |
| 1400 | if !secondary.IsZero() && time.Now().Before(secondary) { |
| 1401 | // Create a fake response. |
| 1402 | resp := &http.Response{ |
| 1403 | Status: http.StatusText(http.StatusForbidden), |
| 1404 | StatusCode: http.StatusForbidden, |
| 1405 | Request: req, |
| 1406 | Header: make(http.Header), |
| 1407 | Body: io.NopCloser(strings.NewReader("")), |
| 1408 | } |
| 1409 | |
| 1410 | retryAfter := time.Until(secondary) |
| 1411 | return &AbuseRateLimitError{ |
| 1412 | Response: resp, |
| 1413 | Message: fmt.Sprintf("API secondary rate limit exceeded until %v, not making remote request.", secondary), |
| 1414 | RetryAfter: &retryAfter, |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | return nil |
| 1419 | } |
| 1420 | |
| 1421 | // compareHTTPResponse returns whether two http.Response objects are equal or not. |
| 1422 | // Currently, only StatusCode is checked. This function is used when implementing the |