Skip to content

Commit 177056f

Browse files
committed
deps: update Redis to v8
1 parent 70d0d66 commit 177056f

File tree

23 files changed

+430
-451
lines changed

23 files changed

+430
-451
lines changed

core/llm_token_ratelimit/identifier_checker.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@ func (f *HeaderChecker) Check(ctx *Context, infos *RequestInfos, identifier Iden
4848
if f == nil {
4949
return false
5050
}
51-
if infos == nil || infos.Headers == nil {
51+
if infos == nil {
5252
logging.Warn("[LLMTokenRateLimit] requestInfos is nil",
5353
"requestID", ctx.Get(KeyRequestID),
5454
)
5555
return true // allow nil for global rate limit
5656
}
57+
if infos.Headers == nil {
58+
logging.Warn("[LLMTokenRateLimit] requestInfos's Headers is nil",
59+
"requestID", ctx.Get(KeyRequestID),
60+
)
61+
return true // allow nil for global rate limit
62+
}
5763
for key, values := range infos.Headers {
5864
if len(values) == 0 {
5965
values = []string{pattern}

core/llm_token_ratelimit/redis_client.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
package llmtokenratelimit
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"sync"
2021
"time"
2122

22-
redis "github.com/go-redis/redis/v7"
23+
redis "github.com/go-redis/redis/v8"
2324
)
2425

2526
type SafeRedisClient struct {
@@ -33,6 +34,21 @@ func NewGlobalRedisClient() *SafeRedisClient {
3334
return &SafeRedisClient{}
3435
}
3536

37+
func (c *SafeRedisClient) SetRedisClient(client *redis.ClusterClient) error {
38+
if c == nil {
39+
return fmt.Errorf("safe redis client is nil")
40+
}
41+
c.mu.Lock()
42+
defer c.mu.Unlock()
43+
44+
if c.client != nil {
45+
c.client.Close()
46+
}
47+
48+
c.client = client
49+
return nil
50+
}
51+
3652
func (c *SafeRedisClient) Init(cfg *Redis) error {
3753
if c == nil {
3854
return fmt.Errorf("safe redis client is nil")
@@ -103,12 +119,12 @@ func (c *SafeRedisClient) Init(cfg *Redis) error {
103119
return fmt.Errorf("new redis client is nil")
104120
}
105121

106-
if _, err := newClient.Ping().Result(); err != nil {
122+
if _, err := newClient.Ping(context.TODO()).Result(); err != nil {
107123
return fmt.Errorf("failed to connect to redis cluster: %v", err)
108124
}
109125
// Perform lock replacement only after the new client successfully connects;
110126
// otherwise, a deadlock will occur if the connection fails
111-
return c.updateClient(newClient)
127+
return c.SetRedisClient(newClient)
112128
}
113129

114130
func (c *SafeRedisClient) Eval(script string, keys []string, args ...interface{}) (interface{}, error) {
@@ -122,7 +138,7 @@ func (c *SafeRedisClient) Eval(script string, keys []string, args ...interface{}
122138
return nil, fmt.Errorf("redis client is not initialized")
123139
}
124140

125-
return c.client.Eval(script, keys, args...).Result()
141+
return c.client.Eval(context.TODO(), script, keys, args...).Result()
126142
}
127143

128144
func (c *SafeRedisClient) Set(key string, value interface{}, expiration time.Duration) error {
@@ -136,7 +152,7 @@ func (c *SafeRedisClient) Set(key string, value interface{}, expiration time.Dur
136152
return fmt.Errorf("redis client is not initialized")
137153
}
138154

139-
return c.client.Set(key, value, expiration).Err()
155+
return c.client.Set(context.TODO(), key, value, expiration).Err()
140156
}
141157

142158
func (c *SafeRedisClient) Get(key string) (*redis.StringCmd, error) {
@@ -150,7 +166,7 @@ func (c *SafeRedisClient) Get(key string) (*redis.StringCmd, error) {
150166
return nil, fmt.Errorf("redis client is not initialized")
151167
}
152168

153-
return c.client.Get(key), nil
169+
return c.client.Get(context.TODO(), key), nil
154170
}
155171

156172
func (c *SafeRedisClient) Close() error {
@@ -165,18 +181,3 @@ func (c *SafeRedisClient) Close() error {
165181
}
166182
return nil
167183
}
168-
169-
func (c *SafeRedisClient) updateClient(newClient *redis.ClusterClient) error {
170-
if c == nil {
171-
return fmt.Errorf("safe redis client is nil")
172-
}
173-
c.mu.Lock()
174-
defer c.mu.Unlock()
175-
176-
if c.client != nil {
177-
c.client.Close()
178-
}
179-
180-
c.client = newClient
181-
return nil
182-
}

core/llm_token_ratelimit/script/token_encoder/update.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local expiration = tonumber(ARGV[2])
2121

2222
local ttl = redis.call('TTL', key)
2323
if ttl < 0 then
24-
redis.call('SET', key, difference, 'EX', expiration)
24+
redis.call('SET', key, difference, 'EX', expiration + 5)
2525
return {difference, expiration}
2626
end
2727
return {tonumber(redis.call('INCRBY', key, difference)), ttl}

core/llm_token_ratelimit/token_encoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"sync"
2222

2323
"github.com/alibaba/sentinel-golang/logging"
24-
"github.com/go-redis/redis/v7"
24+
"github.com/go-redis/redis/v8"
2525
"github.com/pkoukk/tiktoken-go"
2626
)
2727

example/llm_token_ratelimit/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ require (
1717
github.com/cespare/xxhash/v2 v2.2.0 // indirect
1818
github.com/cloudwego/base64x v0.1.4 // indirect
1919
github.com/cloudwego/iasm v0.2.0 // indirect
20+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
2021
github.com/dlclark/regexp2 v1.10.0 // indirect
2122
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
2223
github.com/gin-contrib/sse v0.1.0 // indirect
2324
github.com/go-ole/go-ole v1.2.6 // indirect
2425
github.com/go-playground/locales v0.14.1 // indirect
2526
github.com/go-playground/universal-translator v0.18.1 // indirect
2627
github.com/go-playground/validator/v10 v10.20.0 // indirect
27-
github.com/go-redis/redis/v7 v7.4.1 // indirect
28+
github.com/go-redis/redis/v8 v8.11.0 // indirect
2829
github.com/goccy/go-json v0.10.2 // indirect
2930
github.com/golang/protobuf v1.5.4 // indirect
3031
github.com/google/uuid v1.6.0 // indirect

0 commit comments

Comments
 (0)