diff --git a/go.mod b/go.mod index c889a20..5998196 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/didip/tollbooth/v7 go 1.19 -require github.com/go-pkgz/expirable-cache v0.1.0 +require github.com/go-pkgz/expirable-cache/v3 v3.0.0 diff --git a/go.sum b/go.sum index e6d03c3..98c711e 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,7 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-pkgz/expirable-cache v0.1.0 h1:3bw0m8vlTK8qlwz5KXuygNBTkiKRTPrAGXU0Ej2AC1g= -github.com/go-pkgz/expirable-cache v0.1.0/go.mod h1:GTrEl0X+q0mPNqN6dtcQXksACnzCBQ5k/k1SwXJsZKs= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/go-pkgz/expirable-cache/v3 v3.0.0 h1:u3/gcu3sabLYiTCevoRKv+WzjIn5oo7P8XtiXBeRDLw= +github.com/go-pkgz/expirable-cache/v3 v3.0.0/go.mod h1:2OQiDyEGQalYecLWmXprm3maPXeVb5/6/X7yRPYTzec= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/limiter/limiter.go b/limiter/limiter.go index 5561e6c..0153dc8 100644 --- a/limiter/limiter.go +++ b/limiter/limiter.go @@ -6,7 +6,7 @@ import ( "sync" "time" - cache "github.com/go-pkgz/expirable-cache" + cache "github.com/go-pkgz/expirable-cache/v3" "github.com/didip/tollbooth/v7/internal/time/rate" ) @@ -36,9 +36,9 @@ func New(generalExpirableOptions *ExpirableOptions) *Limiter { lmt.generalExpirableOptions.DefaultExpirationTTL = 87600 * time.Hour } - lmt.tokenBuckets, _ = cache.NewCache(cache.TTL(lmt.generalExpirableOptions.DefaultExpirationTTL)) + lmt.tokenBuckets = cache.NewCache[string, *rate.Limiter]().WithTTL(lmt.generalExpirableOptions.DefaultExpirationTTL) - lmt.basicAuthUsers, _ = cache.NewCache(cache.TTL(lmt.generalExpirableOptions.DefaultExpirationTTL)) + lmt.basicAuthUsers = cache.NewCache[string, bool]().WithTTL(lmt.generalExpirableOptions.DefaultExpirationTTL) return lmt } @@ -81,17 +81,17 @@ type Limiter struct { generalExpirableOptions *ExpirableOptions // List of basic auth usernames to limit. - basicAuthUsers cache.Cache + basicAuthUsers cache.Cache[string, bool] // Map of HTTP headers to limit. // Empty means skip headers checking. - headers map[string]cache.Cache + headers map[string]cache.Cache[string, bool] // Map of Context values to limit. - contextValues map[string]cache.Cache + contextValues map[string]cache.Cache[string, bool] // Map of limiters with TTL - tokenBuckets cache.Cache + tokenBuckets cache.Cache[string, *rate.Limiter] // Ignore URL on the rate limiter keys ignoreURL bool @@ -383,7 +383,7 @@ func (l *Limiter) DeleteExpiredTokenBuckets() { // SetHeaders is thread-safe way of setting map of HTTP headers to limit. func (l *Limiter) SetHeaders(headers map[string][]string) *Limiter { if l.headers == nil { - l.headers = make(map[string]cache.Cache) + l.headers = make(map[string]cache.Cache[string, bool]) } for header, entries := range headers { @@ -419,7 +419,7 @@ func (l *Limiter) SetHeader(header string, entries []string) *Limiter { } if !found { - existing, _ = cache.NewCache(cache.TTL(ttl)) + existing = cache.NewCache[string, bool]().WithTTL(ttl) } for _, entry := range entries { @@ -450,7 +450,7 @@ func (l *Limiter) RemoveHeader(header string) *Limiter { } l.Lock() - l.headers[header], _ = cache.NewCache(cache.TTL(ttl)) + l.headers[header] = cache.NewCache[string, bool]().WithTTL(ttl) l.Unlock() return l @@ -476,7 +476,7 @@ func (l *Limiter) RemoveHeaderEntries(header string, entriesForRemoval []string) // SetContextValues is thread-safe way of setting map of HTTP headers to limit. func (l *Limiter) SetContextValues(contextValues map[string][]string) *Limiter { if l.contextValues == nil { - l.contextValues = make(map[string]cache.Cache) + l.contextValues = make(map[string]cache.Cache[string, bool]) } for contextValue, entries := range contextValues { @@ -512,7 +512,7 @@ func (l *Limiter) SetContextValue(contextValue string, entries []string) *Limite } if !found { - existing, _ = cache.NewCache(cache.TTL(ttl)) + existing = cache.NewCache[string, bool]().WithTTL(ttl) } for _, entry := range entries { @@ -543,7 +543,7 @@ func (l *Limiter) RemoveContextValue(contextValue string) *Limiter { } l.Lock() - l.contextValues[contextValue], _ = cache.NewCache(cache.TTL(ttl)) + l.contextValues[contextValue] = cache.NewCache[string, bool]().WithTTL(ttl) l.Unlock() return l @@ -585,7 +585,7 @@ func (l *Limiter) limitReachedWithTokenBucketTTL(key string, tokenBucketTTL time return false } - return !expiringMap.(*rate.Limiter).Allow() + return !expiringMap.Allow() } // LimitReached returns a bool indicating if the Bucket identified by key ran out of tokens. @@ -606,5 +606,5 @@ func (l *Limiter) Tokens(key string) int { return 0 } - return int(expiringMap.(*rate.Limiter).TokensAt(time.Now())) + return int(expiringMap.TokensAt(time.Now())) }