Skip to content

Commit

Permalink
fixed ratelimiter bucket ttl; improve for 0 delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jun 12, 2020
1 parent 66b2085 commit 73cec76
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/middlewares/ratelimiter/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down

0 comments on commit 73cec76

Please sign in to comment.