15
15
package llmtokenratelimit
16
16
17
17
import (
18
+ "context"
18
19
"fmt"
19
20
"sync"
20
21
"time"
21
22
22
- redis "github.com/go-redis/redis/v7 "
23
+ redis "github.com/go-redis/redis/v8 "
23
24
)
24
25
25
26
type SafeRedisClient struct {
@@ -33,6 +34,21 @@ func NewGlobalRedisClient() *SafeRedisClient {
33
34
return & SafeRedisClient {}
34
35
}
35
36
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
+
36
52
func (c * SafeRedisClient ) Init (cfg * Redis ) error {
37
53
if c == nil {
38
54
return fmt .Errorf ("safe redis client is nil" )
@@ -103,12 +119,12 @@ func (c *SafeRedisClient) Init(cfg *Redis) error {
103
119
return fmt .Errorf ("new redis client is nil" )
104
120
}
105
121
106
- if _ , err := newClient .Ping ().Result (); err != nil {
122
+ if _ , err := newClient .Ping (context . TODO () ).Result (); err != nil {
107
123
return fmt .Errorf ("failed to connect to redis cluster: %v" , err )
108
124
}
109
125
// Perform lock replacement only after the new client successfully connects;
110
126
// otherwise, a deadlock will occur if the connection fails
111
- return c .updateClient (newClient )
127
+ return c .SetRedisClient (newClient )
112
128
}
113
129
114
130
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{}
122
138
return nil , fmt .Errorf ("redis client is not initialized" )
123
139
}
124
140
125
- return c .client .Eval (script , keys , args ... ).Result ()
141
+ return c .client .Eval (context . TODO (), script , keys , args ... ).Result ()
126
142
}
127
143
128
144
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
136
152
return fmt .Errorf ("redis client is not initialized" )
137
153
}
138
154
139
- return c .client .Set (key , value , expiration ).Err ()
155
+ return c .client .Set (context . TODO (), key , value , expiration ).Err ()
140
156
}
141
157
142
158
func (c * SafeRedisClient ) Get (key string ) (* redis.StringCmd , error ) {
@@ -150,7 +166,7 @@ func (c *SafeRedisClient) Get(key string) (*redis.StringCmd, error) {
150
166
return nil , fmt .Errorf ("redis client is not initialized" )
151
167
}
152
168
153
- return c .client .Get (key ), nil
169
+ return c .client .Get (context . TODO (), key ), nil
154
170
}
155
171
156
172
func (c * SafeRedisClient ) Close () error {
@@ -165,18 +181,3 @@ func (c *SafeRedisClient) Close() error {
165
181
}
166
182
return nil
167
183
}
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
- }
0 commit comments