fix: make rate limit increment atomic
This commit is contained in:
@@ -14,6 +14,11 @@ require (
|
|||||||
gorm.io/gorm v1.31.1
|
gorm.io/gorm v1.31.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/alicebob/miniredis/v2 v2.37.0 // indirect
|
||||||
|
github.com/yuin/gopher-lua v1.1.1 // indirect
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
filippo.io/edwards25519 v1.1.0 // indirect
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
github.com/bytedance/sonic v1.14.0 // indirect
|
github.com/bytedance/sonic v1.14.0 // indirect
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
|
github.com/alicebob/miniredis/v2 v2.37.0 h1:RheObYW32G1aiJIj81XVt78ZHJpHonHLHW7OLIshq68=
|
||||||
|
github.com/alicebob/miniredis/v2 v2.37.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM=
|
||||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
@@ -85,6 +87,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
|
|||||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||||
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
|
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
|
||||||
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||||
|
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||||
|
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
|
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
|
||||||
|
|||||||
@@ -70,6 +70,14 @@ else
|
|||||||
end
|
end
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
var incrementWithExpireScript = redis.NewScript(`
|
||||||
|
local count = redis.call("INCR", KEYS[1])
|
||||||
|
if count == 1 then
|
||||||
|
redis.call("PEXPIRE", KEYS[1], ARGV[1])
|
||||||
|
end
|
||||||
|
return count
|
||||||
|
`)
|
||||||
|
|
||||||
func (c *Client) Unlock(ctx context.Context, key string, token string) error {
|
func (c *Client) Unlock(ctx context.Context, key string, token string) error {
|
||||||
if c == nil || c.rdb == nil {
|
if c == nil || c.rdb == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -82,15 +90,10 @@ func (c *Client) IncrementWithExpire(ctx context.Context, key string, expire tim
|
|||||||
if c == nil || c.rdb == nil {
|
if c == nil || c.rdb == nil {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
count, err := c.rdb.Incr(ctx, key).Result()
|
return incrementWithExpireScript.Run(
|
||||||
if err != nil {
|
ctx,
|
||||||
return 0, err
|
c.rdb,
|
||||||
}
|
[]string{key},
|
||||||
if count == 1 {
|
expire.Milliseconds(),
|
||||||
err = c.rdb.Expire(ctx, key, expire).Err()
|
).Int64()
|
||||||
if err != nil {
|
}
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count, nil
|
|
||||||
}
|
|
||||||
|
|||||||
56
backend/internal/middleware/redis/redis_test.go
Normal file
56
backend/internal/middleware/redis/redis_test.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
miniredis "github.com/alicebob/miniredis/v2"
|
||||||
|
goredis "github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIncrementWithExpireSetsTTLWithoutExtendingWindow(t *testing.T) {
|
||||||
|
mr, err := miniredis.Run()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("start miniredis: %v", err)
|
||||||
|
}
|
||||||
|
defer mr.Close()
|
||||||
|
|
||||||
|
client := &Client{
|
||||||
|
rdb: goredis.NewClient(&goredis.Options{Addr: mr.Addr()}),
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
key := "feedsystem:ratelimit:test"
|
||||||
|
expire := 30 * time.Second
|
||||||
|
|
||||||
|
count, err := client.IncrementWithExpire(ctx, key, expire)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("first increment: %v", err)
|
||||||
|
}
|
||||||
|
if count != 1 {
|
||||||
|
t.Fatalf("expected count 1, got %d", count)
|
||||||
|
}
|
||||||
|
|
||||||
|
firstTTL := mr.TTL(key)
|
||||||
|
if firstTTL <= 0 || firstTTL > expire {
|
||||||
|
t.Fatalf("expected ttl in (0, %s], got %s", expire, firstTTL)
|
||||||
|
}
|
||||||
|
|
||||||
|
mr.FastForward(5 * time.Second)
|
||||||
|
ttlBeforeSecond := mr.TTL(key)
|
||||||
|
|
||||||
|
count, err = client.IncrementWithExpire(ctx, key, expire)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("second increment: %v", err)
|
||||||
|
}
|
||||||
|
if count != 2 {
|
||||||
|
t.Fatalf("expected count 2, got %d", count)
|
||||||
|
}
|
||||||
|
|
||||||
|
ttlAfterSecond := mr.TTL(key)
|
||||||
|
if ttlAfterSecond != ttlBeforeSecond {
|
||||||
|
t.Fatalf("expected ttl to stay at %s, got %s", ttlBeforeSecond, ttlAfterSecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user