From 919d3cc160f7106c73514d27aa337d9e446eff63 Mon Sep 17 00:00:00 2001 From: Diego Szychowski Date: Fri, 19 Mar 2021 14:57:14 -0300 Subject: [PATCH] fix: rate limit defined by config (#200) --- internal/serv/iplimiter.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/serv/iplimiter.go b/internal/serv/iplimiter.go index 93fcdf1b..425fe7a1 100644 --- a/internal/serv/iplimiter.go +++ b/internal/serv/iplimiter.go @@ -17,10 +17,10 @@ func init() { ipCache, _ = cache.NewCache(cache.MaxKeys(10), cache.TTL(time.Minute*5)) } -func getIPLimiter(ip string) *rate.Limiter { +func getIPLimiter(ip string, limit float64, bucket int) *rate.Limiter { v, exists := ipCache.Get(ip) if !exists { - limiter := rate.NewLimiter(1, 3) + limiter := rate.NewLimiter(rate.Limit(limit), bucket) ipCache.Set(ip, limiter, 0) return limiter } @@ -48,7 +48,7 @@ func rateLimiter(sc *ServConfig, h http.Handler) http.Handler { return } - if !getIPLimiter(ip).Allow() { + if !getIPLimiter(ip, sc.conf.RateLimiter.Rate, sc.conf.RateLimiter.Bucket).Allow() { http.Error(w, "429 Too Many Requests", http.StatusTooManyRequests) return }