-
Notifications
You must be signed in to change notification settings - Fork 13
/
detect.go
70 lines (58 loc) · 2.09 KB
/
detect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package github_ratelimit
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strings"
)
type SecondaryRateLimitBody struct {
Message string `json:"message"`
DocumentURL string `json:"documentation_url"`
}
const (
SecondaryRateLimitMessage = `You have exceeded a secondary rate limit`
SecondaryRateLimitDocumentationPathSuffix = `secondary-rate-limits`
)
// IsSecondaryRateLimit checks whether the response is a legitimate secondary rate limit.
// It checks the prefix of the message and the suffix of the documentation URL in the response body in case
// the message or documentation URL is modified in the future.
// https://docs.github.com/en/rest/overview/rate-limits-for-the-rest-api#about-secondary-rate-limits
func (s SecondaryRateLimitBody) IsSecondaryRateLimit() bool {
return strings.HasPrefix(s.Message, SecondaryRateLimitMessage) ||
strings.HasSuffix(s.DocumentURL, SecondaryRateLimitDocumentationPathSuffix)
}
// isRateLimitStatus checks whether the status code is a rate limit status code.
// see https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api#exceeding-the-rate-limit
func isRateLimitStatus(statusCode int) bool {
return statusCode == http.StatusForbidden || statusCode == http.StatusTooManyRequests
}
// isSecondaryRateLimit checks whether the response is a legitimate secondary rate limit.
func isSecondaryRateLimit(resp *http.Response) bool {
if !isRateLimitStatus(resp.StatusCode) {
return false
}
if resp.Header == nil {
return false
}
// a primary rate limit
if remaining, ok := httpHeaderIntValue(resp.Header, HeaderXRateLimitRemaining); ok && remaining == 0 {
return false
}
// an authentic HTTP response (not a primary rate limit)
defer resp.Body.Close()
rawBody, err := io.ReadAll(resp.Body)
if err != nil {
return false // unexpected error
}
// restore original body
resp.Body = io.NopCloser(bytes.NewReader(rawBody))
var body SecondaryRateLimitBody
if err := json.Unmarshal(rawBody, &body); err != nil {
return false // unexpected error
}
if !body.IsSecondaryRateLimit() {
return false
}
return true
}