-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split interface Limiter and port default Counter from k1LoW/rlutil
- Loading branch information
Showing
11 changed files
with
239 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package counter | ||
|
||
import ( | ||
"fmt" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/jellydator/ttlcache/v3" | ||
) | ||
|
||
// Counter is a sliding window counter implemented with a TTL cache | ||
type Counter struct { | ||
cache *ttlcache.Cache[string, *uint64] | ||
// capacity is the maximum number of items to store in the cache | ||
capacity uint64 | ||
// disableAutoDeleteExpired disables the automatic deletion of expired items | ||
disableAutoDeleteExpired bool | ||
} | ||
|
||
type Option func(*Counter) | ||
|
||
// WithCapacity sets the maximum number of items to store in the cache | ||
func WithCapacity(capacity uint64) Option { | ||
return func(c *Counter) { | ||
c.capacity = capacity | ||
} | ||
} | ||
|
||
// DisableAutoDeleteExpired disables the automatic deletion of expired items | ||
func DisableAutoDeleteExpired() Option { | ||
return func(c *Counter) { | ||
c.disableAutoDeleteExpired = true | ||
} | ||
} | ||
|
||
// NewCounter creates a new Counter | ||
func New(windowLen time.Duration, opts ...Option) *Counter { | ||
c := &Counter{} | ||
for _, opt := range opts { | ||
opt(c) | ||
} | ||
ttlOpts := []ttlcache.Option[string, *uint64]{ | ||
ttlcache.WithTTL[string, *uint64](windowLen * 2), | ||
} | ||
if c.capacity > 0 { | ||
ttlOpts = append(ttlOpts, ttlcache.WithCapacity[string, *uint64](c.capacity)) | ||
} | ||
cache := ttlcache.New[string, *uint64](ttlOpts...) | ||
c.cache = cache | ||
if !c.disableAutoDeleteExpired { | ||
go cache.Start() | ||
} | ||
return c | ||
} | ||
|
||
// Get returns the count for the given key and window | ||
func (c *Counter) Get(key string, window time.Time) (int, error) { //nostyle:getters | ||
key = generateKey(key, window) | ||
i := c.cache.Get(key) | ||
if i == nil { | ||
return 0, nil | ||
} | ||
return int(*i.Value()), nil | ||
} | ||
|
||
// Increment increments the count for the given key and window | ||
func (c *Counter) Increment(key string, currWindow time.Time) error { | ||
key = generateKey(key, currWindow) | ||
zero := uint64(0) | ||
i, _ := c.cache.GetOrSet(key, &zero) | ||
atomic.AddUint64(i.Value(), 1) | ||
return nil | ||
} | ||
|
||
func generateKey(key string, window time.Time) string { | ||
return fmt.Sprintf("%s-%d", key, window.Unix()) | ||
} | ||
|
||
// DeleteExpired deletes expired items from the cache | ||
func (c *Counter) DeleteExpired() { | ||
c.cache.DeleteExpired() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package counter | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/jellydator/ttlcache/v3" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
windowLen := 1 * time.Second | ||
window := time.Now().Truncate(windowLen) | ||
key := "test" | ||
c := New(windowLen) | ||
t.Run("Zero value", func(t *testing.T) { | ||
for i := 0; i < 2; i++ { | ||
want := 0 | ||
got, err := c.Get(key, window) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if got != want { | ||
t.Errorf("Get() = %v, want %v", got, want) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Get value", func(t *testing.T) { | ||
want := 1 | ||
v := uint64(want) | ||
c.cache.Set(generateKey(key, window), &v, ttlcache.DefaultTTL) | ||
got, err := c.Get(key, window) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if got != want { | ||
t.Errorf("Get() = %v, want %v", got, want) | ||
} | ||
}) | ||
} | ||
|
||
func TestIncrement(t *testing.T) { | ||
windowLen := 1 * time.Millisecond | ||
window := time.Now().Truncate(windowLen) | ||
key := "test" | ||
t.Run("Increment simply", func(t *testing.T) { | ||
c := New(windowLen) | ||
for i := 0; i < 5; i++ { | ||
want := i + 1 | ||
if err := c.Increment(key, window); err != nil { | ||
t.Error(err) | ||
} | ||
got, err := c.Get(key, window) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if got != want { | ||
t.Errorf("Get() = %v, want %v", got, want) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Increment in parallel", func(t *testing.T) { | ||
c := New(windowLen) | ||
want := 1000 | ||
eg := new(errgroup.Group) | ||
for i := 0; i < want; i++ { | ||
eg.Go(func() error { | ||
return c.Increment(key, window) | ||
}) | ||
} | ||
if err := eg.Wait(); err != nil { | ||
t.Error(err) | ||
} | ||
got, err := c.Get(key, window) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if got != want { | ||
t.Errorf("Get() = %v, want %v", got, want) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= | ||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= | ||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-chi/httprate v0.8.0 h1:CyKng28yhGnlGXH9EDGC/Qizj29afJQSNW15W/yj34o= | ||
github.com/go-chi/httprate v0.8.0/go.mod h1:6GOYBSwnpra4CQfAKXu8sQZg+nZ0M1g9QnyFvxrAB8A= | ||
github.com/jellydator/ttlcache/v3 v3.1.1 h1:RCgYJqo3jgvhl+fEWvjNW8thxGWsgxi+TPhRir1Y9y8= | ||
github.com/jellydator/ttlcache/v3 v3.1.1/go.mod h1:hi7MGFdMAwZna5n2tuvh63DvFLzVKySzCVW6+0gA2n4= | ||
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/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= | ||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= | ||
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= | ||
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= | ||
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.