diff --git a/backend/go.mod b/backend/go.mod index d0134dc..4264f23 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -14,6 +14,11 @@ require ( 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 ( filippo.io/edwards25519 v1.1.0 // indirect github.com/bytedance/sonic v1.14.0 // indirect diff --git a/backend/go.sum b/backend/go.sum index 8063d6f..9f07738 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -1,5 +1,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= 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/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= 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/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/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/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU= diff --git a/backend/internal/middleware/redis/redis.go b/backend/internal/middleware/redis/redis.go index fa3bbc5..074f7de 100644 --- a/backend/internal/middleware/redis/redis.go +++ b/backend/internal/middleware/redis/redis.go @@ -70,6 +70,14 @@ else 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 { if c == nil || c.rdb == nil { return nil @@ -82,15 +90,10 @@ func (c *Client) IncrementWithExpire(ctx context.Context, key string, expire tim if c == nil || c.rdb == nil { return 0, nil } - count, err := c.rdb.Incr(ctx, key).Result() - if err != nil { - return 0, err - } - if count == 1 { - err = c.rdb.Expire(ctx, key, expire).Err() - if err != nil { - return 0, err - } - } - return count, nil -} \ No newline at end of file + return incrementWithExpireScript.Run( + ctx, + c.rdb, + []string{key}, + expire.Milliseconds(), + ).Int64() +} diff --git a/backend/internal/middleware/redis/redis_test.go b/backend/internal/middleware/redis/redis_test.go new file mode 100644 index 0000000..0387c45 --- /dev/null +++ b/backend/internal/middleware/redis/redis_test.go @@ -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) + } +}