From 73cec76a494c45b51f50f497b93a3c41bb657fd7 Mon Sep 17 00:00:00 2001 From: zensh Date: Fri, 12 Jun 2020 14:06:15 +0800 Subject: [PATCH] fixed ratelimiter bucket ttl; improve for 0 delay. --- pkg/middlewares/ratelimiter/rate_limiter.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/middlewares/ratelimiter/rate_limiter.go b/pkg/middlewares/ratelimiter/rate_limiter.go index 4bcf345502..1a28208c0f 100644 --- a/pkg/middlewares/ratelimiter/rate_limiter.go +++ b/pkg/middlewares/ratelimiter/rate_limiter.go @@ -18,8 +18,9 @@ import ( ) const ( - typeName = "RateLimiterType" - maxSources = 65536 + typeName = "RateLimiterType" + maxSources = 65536 + bucketTTLSeconds = int(60) * 10 // 10 minutes ) // rateLimiter implements rate limiting and traffic shaping with a set of token buckets; @@ -121,7 +122,7 @@ func (rl *rateLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request) { bucket = rlSource.(*rate.Limiter) } else { bucket = rate.NewLimiter(rl.rate, int(rl.burst)) - if err := rl.buckets.Set(source, bucket, int(rl.maxDelay/time.Second)*10+1); err != nil { + if err := rl.buckets.Set(source, bucket, bucketTTLSeconds); err != nil { logger.Errorf("could not insert bucket: %v", err) http.Error(w, "could not insert bucket", http.StatusInternalServerError) return @@ -141,7 +142,9 @@ func (rl *rateLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - time.Sleep(delay) + if delay > 0 { + time.Sleep(delay) + } rl.next.ServeHTTP(w, r) }