Skip to content

Commit

Permalink
Stop requests until reset when x-rate-limit-remaining is zero.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisberkhout committed Nov 11, 2024
1 parent 4762ead commit ea8cfb4
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ func (r RateLimiter) Update(endpoint string, h http.Header, window time.Duration

// Process reset if we need to wait until reset to avoid a request against a zero quota.
if rateLimit <= 0 {
waitUntil := resetTime.UTC()
// Reset limiter to block requests until reset
limiter := rate.NewLimiter(0, 0)
r[endpoint] = limiter

// next gives us a sane next window estimate, but the
// estimate will be overwritten when we make the next
// permissible API request.
next := rate.Limit(lim / window.Seconds())
waitUntil := resetTime.UTC()
limiter.SetLimitAt(waitUntil, next)
limiter.SetBurstAt(waitUntil, burst)
log.Debugw("rate limit adjust", "reset_time", waitUntil, "next_rate", next, "next_burst", burst)
log.Debugw("rate limit reset", "reset_time", waitUntil, "next_rate", next, "next_burst", burst)
return nil
}
limiter.SetLimit(rateLimit)
Expand Down

0 comments on commit ea8cfb4

Please sign in to comment.