@@ -16,20 +16,32 @@ type rateLimits struct {
16
16
Resource string
17
17
}
18
18
19
- // githubRateLimits checks the returned response headers and
19
+ // githubRateLimits returns rate limit information from a GitHub response.
20
+ // GitHub rate limits are on a per-user basis, and tracking each user as
21
+ // a prometheus label might be too much. So only track rate limits for
22
+ // unauthorized responses.
23
+ //
24
+ // Unauthorized responses have a much stricter rate limit of 60 per hour.
25
+ // Tracking this is vital to ensure we do not hit the limit.
20
26
func githubRateLimits (resp * http.Response , err error ) (rateLimits , bool ) {
21
27
if err != nil || resp == nil {
22
28
return rateLimits {}, false
23
29
}
24
30
31
+ // Only track 401 responses which indicates we are using the 60 per hour
32
+ // rate limit.
33
+ if resp .StatusCode != http .StatusUnauthorized {
34
+ return rateLimits {}, false
35
+ }
36
+
25
37
p := headerParser {header : resp .Header }
26
38
// See
27
39
// https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#checking-the-status-of-your-rate-limit
28
40
limits := rateLimits {
29
41
Limit : p .int ("x-ratelimit-limit" ),
30
42
Remaining : p .int ("x-ratelimit-remaining" ),
31
43
Used : p .int ("x-ratelimit-used" ),
32
- Resource : p .string ("x-ratelimit-resource" ),
44
+ Resource : p .string ("x-ratelimit-resource" ) + "-unauthorized" ,
33
45
}
34
46
35
47
if limits .Limit == 0 &&
@@ -50,18 +62,6 @@ func githubRateLimits(resp *http.Response, err error) (rateLimits, bool) {
50
62
}
51
63
limits .Reset = resetAt
52
64
53
- // Unauthorized requests have their own rate limit, so we should
54
- // track them separately.
55
- if resp .StatusCode == http .StatusUnauthorized {
56
- limits .Resource += "-unauthorized"
57
- }
58
-
59
- // A 401 or 429 means too many requests. This might mess up the
60
- // "resource" string because we could hit the unauthorized limit,
61
- // and we do not want that to override the authorized one.
62
- // However, in testing, it seems a 401 is always a 401, even if
63
- // the limit is hit.
64
-
65
65
if len (p .errors ) > 0 {
66
66
// If we are missing any headers, then do not try and guess
67
67
// what the rate limits are.
0 commit comments