|
| 1 | +package github_ratelimit_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type SecondaryRateLimitInjecter struct { |
| 13 | + base http.RoundTripper |
| 14 | + options *SecondaryRateLimitInjecterOptions |
| 15 | + blockUntil time.Time |
| 16 | + lock sync.Mutex |
| 17 | + AbuseAttempts int |
| 18 | +} |
| 19 | + |
| 20 | +const ( |
| 21 | + RateLimitInjecterEvery = "SECONDARY_RATE_LIMIT_INJECTER_EVERY" |
| 22 | + RateLimitInjecterDuration = "SECONDARY_RATE_LIMIT_INJECTER_DURATION" |
| 23 | +) |
| 24 | + |
| 25 | +type SecondaryRateLimitInjecterOptions struct { |
| 26 | + Every time.Duration |
| 27 | + Sleep time.Duration |
| 28 | +} |
| 29 | + |
| 30 | +func NewRateLimitInjecter(base http.RoundTripper, options *SecondaryRateLimitInjecterOptions) (http.RoundTripper, error) { |
| 31 | + if options.IsNoop() { |
| 32 | + return base, nil |
| 33 | + } |
| 34 | + if err := options.Validate(); err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + |
| 38 | + injecter := &SecondaryRateLimitInjecter{ |
| 39 | + base: base, |
| 40 | + options: options, |
| 41 | + } |
| 42 | + return injecter, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (t *SecondaryRateLimitInjecter) RoundTrip(request *http.Request) (*http.Response, error) { |
| 46 | + resp, err := t.base.RoundTrip(request) |
| 47 | + if err != nil { |
| 48 | + return resp, err |
| 49 | + } |
| 50 | + |
| 51 | + t.lock.Lock() |
| 52 | + defer t.lock.Unlock() |
| 53 | + |
| 54 | + // initialize on first use |
| 55 | + now := time.Now() |
| 56 | + if t.blockUntil.IsZero() { |
| 57 | + t.blockUntil = now |
| 58 | + } |
| 59 | + |
| 60 | + // on-going rate limit |
| 61 | + if t.blockUntil.After(now) { |
| 62 | + t.AbuseAttempts++ |
| 63 | + return t.toRetryResponse(resp), nil |
| 64 | + } |
| 65 | + |
| 66 | + nextStart := t.NextSleepStart() |
| 67 | + |
| 68 | + // start a rate limit period |
| 69 | + if !now.Before(nextStart) { |
| 70 | + t.blockUntil = nextStart.Add(t.options.Sleep) |
| 71 | + return t.toRetryResponse(resp), nil |
| 72 | + } |
| 73 | + |
| 74 | + return resp, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (r *SecondaryRateLimitInjecterOptions) IsNoop() bool { |
| 78 | + return r.Every == 0 || r.Sleep == 0 |
| 79 | +} |
| 80 | + |
| 81 | +func (r *SecondaryRateLimitInjecterOptions) Validate() error { |
| 82 | + if r.Every < 0 { |
| 83 | + return fmt.Errorf("injecter expects a positive trigger interval") |
| 84 | + } |
| 85 | + if r.Sleep < 0 { |
| 86 | + return fmt.Errorf("injecter expects a positive sleep interval") |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | +func (r *SecondaryRateLimitInjecter) CurrentSleepEnd() time.Time { |
| 91 | + return r.blockUntil |
| 92 | +} |
| 93 | + |
| 94 | +func (r *SecondaryRateLimitInjecter) NextSleepStart() time.Time { |
| 95 | + return r.blockUntil.Add(r.options.Every) |
| 96 | +} |
| 97 | + |
| 98 | +func (t *SecondaryRateLimitInjecter) toRetryResponse(resp *http.Response) *http.Response { |
| 99 | + resp.StatusCode = http.StatusForbidden |
| 100 | + timeUntil := time.Until(t.blockUntil) |
| 101 | + if timeUntil.Nanoseconds()%int64(time.Second) > 0 { |
| 102 | + timeUntil += time.Second |
| 103 | + } |
| 104 | + resp.Header.Set("Retry-After", fmt.Sprintf("%v", int(timeUntil.Seconds()))) |
| 105 | + doc_url := "https://docs.github.com/en/rest/guides/best-practices-for-integrators?apiVersion=2022-11-28#secondary-rate-limits" |
| 106 | + resp.Body = io.NopCloser(strings.NewReader(`{"documentation_url":"` + doc_url + `"}`)) |
| 107 | + return resp |
| 108 | +} |
0 commit comments