respond early

This commit is contained in:
Cory Schwartz 2022-04-19 19:21:27 -07:00
parent fc42d03ea4
commit 9558e9f0ee

View File

@ -165,9 +165,12 @@ func (gw *Node) checkTimestamp(at time.Time) error {
func (gw *Node) limit(ctx context.Context, tokens int) error {
ctx2, cancel := context.WithTimeout(ctx, gw.rateLimitTimeout)
defer cancel()
if !gw.rateLimiter.AllowN(time.Now(), tokens) {
return fmt.Errorf("server busy")
}
err := gw.rateLimiter.WaitN(ctx2, tokens)
if err != nil {
return fmt.Errorf("rate limited: %w", err)
return fmt.Errorf("server busy, cannot complete before timeout %w", err)
}
return nil
}