Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update go-pkgz/expirable-cache to version with generics #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 6 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
30 changes: 15 additions & 15 deletions limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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()))
}