Skip to content

Commit

Permalink
Merge pull request #41 from bringg/up-go-redis
Browse files Browse the repository at this point in the history
Upgrade go-redis
  • Loading branch information
yosiat committed Jan 24, 2024
2 parents 36d86bf + 47fa409 commit 21130cb
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 58 deletions.
29 changes: 20 additions & 9 deletions algorithm/cloudflare/redis_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cloudflare

import (
"context"
"fmt"
"strconv"
"time"

"github.com/go-redis/redis/v8"

"github.com/bringg/go_redis_ratelimit/algorithm"
)

Expand All @@ -31,17 +31,28 @@ func (s *RedisDataStore) Inc(key string, window time.Time) error {

func (s *RedisDataStore) Get(key string, previousWindow, currentWindow time.Time) (prevValue int64, currValue int64, err error) {
ctx := context.Background()
pipe := s.RDB.TxPipeline()
prevRes := pipe.Get(ctx, mapKey(key, previousWindow))
currRes := pipe.Get(ctx, mapKey(key, currentWindow))

_, err = pipe.Exec(ctx)
if err != nil && err != redis.Nil {
res, err := s.RDB.MGet(ctx, mapKey(key, previousWindow), mapKey(key, currentWindow)).Result()

if err != nil {
return
}

prevValue, _ = prevRes.Int64()
currValue, _ = currRes.Int64()
if res[0] != nil {
prevValue, err = strconv.ParseInt(res[0].(string), 10, 64)
if err != nil {
err = fmt.Errorf("failed parsing previous value: %q", res[0])
return
}
}

if res[1] != nil {
currValue, err = strconv.ParseInt(res[1].(string), 10, 64)
if err != nil {
err = fmt.Errorf("failed parsing current value: %q", res[0])
return
}
}

return prevValue, currValue, nil
}
Expand Down
2 changes: 1 addition & 1 deletion algorithm/gcra/gcra.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package gcra
import (
"context"

"github.com/go-redis/redis_rate/v9"
"github.com/go-redis/redis_rate/v10"

"github.com/bringg/go_redis_ratelimit/algorithm"
)
Expand Down
8 changes: 6 additions & 2 deletions algorithm/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
)

var Registry []*RegInfo
Expand All @@ -25,13 +25,17 @@ type (
Del(ctx context.Context, keys ...string) *redis.IntCmd
ScriptLoad(ctx context.Context, script string) *redis.StringCmd
ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd
ZAdd(ctx context.Context, key string, members ...*redis.Z) *redis.IntCmd
ZAdd(ctx context.Context, key string, members ...redis.Z) *redis.IntCmd
Expire(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd
ZRemRangeByScore(ctx context.Context, key string, min string, max string) *redis.IntCmd
Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) ([]redis.Cmder, error)
ZRangeByScoreWithScores(ctx context.Context, key string, opt *redis.ZRangeBy) *redis.ZSliceCmd
MGet(ctx context.Context, keys ...string) *redis.SliceCmd

EvalRO(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
EvalShaRO(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
}

Algorithm interface {
Expand Down
2 changes: 1 addition & 1 deletion algorithm/sliding_window/sliding_window_lua.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sliding_window

import "github.com/go-redis/redis/v8"
import "github.com/redis/go-redis/v9"

var script = redis.NewScript(`
-- this script has side-effects, so it requires replicate commands mode
Expand Down
4 changes: 2 additions & 2 deletions algorithm/sliding_window/v2/sliding_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"time"

"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"

"github.com/bringg/go_redis_ratelimit/algorithm"
)
Expand Down Expand Up @@ -57,7 +57,7 @@ func (c *SlidingWindow) Allow() (r *algorithm.Result, err error) {
if err := pipe.ZAdd(
ctx,
c.key,
&redis.Z{
redis.Z{
Member: nowNanos,
Score: float64(nowNanos),
},
Expand Down
2 changes: 1 addition & 1 deletion examples/cloudflare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"time"

"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"

limiter "github.com/bringg/go_redis_ratelimit"
"github.com/bringg/go_redis_ratelimit/algorithm/cloudflare"
Expand Down
2 changes: 1 addition & 1 deletion examples/gcra/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"time"

"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"

"github.com/bringg/go_redis_ratelimit"
"github.com/bringg/go_redis_ratelimit/algorithm/gcra"
Expand Down
2 changes: 1 addition & 1 deletion examples/sliding_window/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"time"

"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"

"github.com/bringg/go_redis_ratelimit"
"github.com/bringg/go_redis_ratelimit/algorithm/sliding_window"
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.21
require (
github.com/alicebob/miniredis/v2 v2.31.1
github.com/coinpaprika/ratelimiter v0.2.1
github.com/go-redis/redis/v8 v8.11.4
github.com/go-redis/redis_rate/v9 v9.1.2
github.com/go-redis/redis_rate/v10 v10.0.1
github.com/golangci/golangci-lint v1.55.2
github.com/redis/go-redis/v9 v9.4.0
github.com/stretchr/testify v1.8.4
gotest.tools/gotestsum v1.11.0
)
Expand Down Expand Up @@ -43,7 +43,7 @@ require (
github.com/butuzov/mirror v1.1.0 // indirect
github.com/catenacyber/perfsprint v0.2.0 // indirect
github.com/ccojocar/zxcvbn-go v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charithe/durationcheck v0.0.10 // indirect
github.com/chavacava/garif v0.1.0 // indirect
github.com/curioswitch/go-reassign v0.2.0 // indirect
Expand Down
Loading

0 comments on commit 21130cb

Please sign in to comment.