I'm using golang.org/x/time/rate for rate limiting api requests.
lim := rate.NewLimiter(rateLimit, burstLimit)
for range time.NewTicker(time.Second).C {
fmt.Println(time.Now(), "tick")
go func() {
res := lim.Reserve()
if res.OK() && res.Delay() == 0 {
fmt.Println(time.Now(), "done")
} else {
fmt.Println(time.Now(), "dropped")
res.Cancel()
}
}()
}
For allowed requests that can execute immediately, it may sometimes be necessary to retry the request. Those retried requests should not count against the rate limit. Using res.Cancel() in that case does not return the reserved token to the pool due to https://cs.opensource.google/go/x/time/+/1f47c861:rate/rate.go;l=161.
How can reserved tokens be unreserved or the limiter's status adjusted?