From 9558e9f0ee135ac9fa58f4476e5b10838d4441dd Mon Sep 17 00:00:00 2001 From: Cory Schwartz Date: Tue, 19 Apr 2022 19:21:27 -0700 Subject: [PATCH] respond early --- gateway/node.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gateway/node.go b/gateway/node.go index 9d989a1aea6..e157645d493 100644 --- a/gateway/node.go +++ b/gateway/node.go @@ -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 }