Skip to content

Commit

Permalink
fix: rate limit defined by config (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosz authored Mar 19, 2021
1 parent 03429de commit 919d3cc
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/serv/iplimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 919d3cc

Please sign in to comment.