@@ -12,6 +12,7 @@ import (
1212 auth "github.com/bricks-cloud/bricksllm/internal/authenticator"
1313 "github.com/bricks-cloud/bricksllm/internal/cache"
1414 "github.com/bricks-cloud/bricksllm/internal/config"
15+ "github.com/bricks-cloud/bricksllm/internal/encryptor"
1516 "github.com/bricks-cloud/bricksllm/internal/logger/zap"
1617 "github.com/bricks-cloud/bricksllm/internal/manager"
1718 "github.com/bricks-cloud/bricksllm/internal/message"
@@ -173,137 +174,112 @@ func main() {
173174 }
174175 rMemStore .Listen ()
175176
176- rateLimitRedisCache := redis .NewClient (& redis.Options {
177- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
178- Password : cfg .RedisPassword ,
179- DB : 0 ,
180- })
177+ defaultRedisOption := func (cfg * config.Config , dbIndex int ) * redis.Options {
178+
179+ options := & redis.Options {
180+ Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
181+ Password : cfg .RedisPassword ,
182+ DB : cfg .RedisDBStartIndex + dbIndex ,
183+ }
184+
185+ return options
186+ }
187+
188+ rateLimitRedisCache := redis .NewClient (defaultRedisOption (cfg , 0 ))
181189 ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
182190 defer cancel ()
183191 if err := rateLimitRedisCache .Ping (ctx ).Err (); err != nil {
184192 log .Sugar ().Fatalf ("error connecting to rate limit redis cache: %v" , err )
185193 }
186194
187- costLimitRedisCache := redis .NewClient (& redis.Options {
188- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
189- Password : cfg .RedisPassword ,
190- DB : 1 ,
191- })
195+ costLimitRedisCache := redis .NewClient (defaultRedisOption (cfg , 1 ))
192196
193197 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
194198 defer cancel ()
195199 if err := costLimitRedisCache .Ping (ctx ).Err (); err != nil {
196200 log .Sugar ().Fatalf ("error connecting to cost limit redis cache: %v" , err )
197201 }
198202
199- costRedisStorage := redis .NewClient (& redis.Options {
200- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
201- Password : cfg .RedisPassword ,
202- DB : 2 ,
203- })
203+ costRedisStorage := redis .NewClient (defaultRedisOption (cfg , 2 ))
204204
205205 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
206206 defer cancel ()
207207 if err := costRedisStorage .Ping (ctx ).Err (); err != nil {
208208 log .Sugar ().Fatalf ("error connecting to cost limit redis storage: %v" , err )
209209 }
210210
211- apiRedisCache := redis .NewClient (& redis.Options {
212- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
213- Password : cfg .RedisPassword ,
214- DB : 3 ,
215- })
211+ apiRedisCache := redis .NewClient (defaultRedisOption (cfg , 3 ))
216212
217213 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
218214 defer cancel ()
219215 if err := apiRedisCache .Ping (ctx ).Err (); err != nil {
220216 log .Sugar ().Fatalf ("error connecting to api redis cache: %v" , err )
221217 }
222218
223- accessRedisCache := redis .NewClient (& redis.Options {
224- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
225- Password : cfg .RedisPassword ,
226- DB : 4 ,
227- })
219+ accessRedisCache := redis .NewClient (defaultRedisOption (cfg , 4 ))
228220
229221 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
230222 defer cancel ()
231223 if err := accessRedisCache .Ping (ctx ).Err (); err != nil {
232224 log .Sugar ().Fatalf ("error connecting to api redis cache: %v" , err )
233225 }
234226
235- userRateLimitRedisCache := redis .NewClient (& redis.Options {
236- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
237- Password : cfg .RedisPassword ,
238- DB : 5 ,
239- })
227+ userRateLimitRedisCache := redis .NewClient (defaultRedisOption (cfg , 5 ))
240228
241229 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
242230 defer cancel ()
243231 if err := userRateLimitRedisCache .Ping (ctx ).Err (); err != nil {
244232 log .Sugar ().Fatalf ("error connecting to user rate limit redis cache: %v" , err )
245233 }
246234
247- userCostLimitRedisCache := redis .NewClient (& redis.Options {
248- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
249- Password : cfg .RedisPassword ,
250- DB : 6 ,
251- })
235+ userCostLimitRedisCache := redis .NewClient (defaultRedisOption (cfg , 6 ))
252236
253237 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
254238 defer cancel ()
255239 if err := userCostLimitRedisCache .Ping (ctx ).Err (); err != nil {
256240 log .Sugar ().Fatalf ("error connecting to user cost limit redis cache: %v" , err )
257241 }
258242
259- userCostRedisStorage := redis .NewClient (& redis.Options {
260- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
261- Password : cfg .RedisPassword ,
262- DB : 7 ,
263- })
243+ userCostRedisStorage := redis .NewClient (defaultRedisOption (cfg , 7 ))
264244
265245 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
266246 defer cancel ()
267247 if err := userCostRedisStorage .Ping (ctx ).Err (); err != nil {
268248 log .Sugar ().Fatalf ("error connecting to user cost redis cache: %v" , err )
269249 }
270250
271- userAccessRedisCache := redis .NewClient (& redis.Options {
272- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
273- Password : cfg .RedisPassword ,
274- DB : 8 ,
275- })
251+ userAccessRedisCache := redis .NewClient (defaultRedisOption (cfg , 8 ))
276252
277253 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
278254 defer cancel ()
279255 if err := userAccessRedisCache .Ping (ctx ).Err (); err != nil {
280256 log .Sugar ().Fatalf ("error connecting to user access redis storage: %v" , err )
281257 }
282258
283- providerSettingsRedisCache := redis .NewClient (& redis.Options {
284- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
285- Password : cfg .RedisPassword ,
286- DB : 9 ,
287- })
259+ providerSettingsRedisCache := redis .NewClient (defaultRedisOption (cfg , 9 ))
288260
289261 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
290262 defer cancel ()
291263 if err := providerSettingsRedisCache .Ping (ctx ).Err (); err != nil {
292264 log .Sugar ().Fatalf ("error connecting to provider settings redis storage: %v" , err )
293265 }
294266
295- keysRedisCache := redis .NewClient (& redis.Options {
296- Addr : fmt .Sprintf ("%s:%s" , cfg .RedisHosts , cfg .RedisPort ),
297- Password : cfg .RedisPassword ,
298- DB : 10 ,
299- })
267+ keysRedisCache := redis .NewClient (defaultRedisOption (cfg , 10 ))
300268
301269 ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
302270 defer cancel ()
303271 if err := keysRedisCache .Ping (ctx ).Err (); err != nil {
304272 log .Sugar ().Fatalf ("error connecting to keys redis storage: %v" , err )
305273 }
306274
275+ requestsLimitRedisStorage := redis .NewClient (defaultRedisOption (cfg , 11 ))
276+
277+ ctx , cancel = context .WithTimeout (context .Background (), 2 * time .Second )
278+ defer cancel ()
279+ if err := requestsLimitRedisStorage .Ping (ctx ).Err (); err != nil {
280+ log .Sugar ().Fatalf ("error connecting to requests limit redis storage: %v" , err )
281+ }
282+
307283 rateLimitCache := redisStorage .NewCache (rateLimitRedisCache , cfg .RedisWriteTimeout , cfg .RedisReadTimeout )
308284 costLimitCache := redisStorage .NewCache (costLimitRedisCache , cfg .RedisWriteTimeout , cfg .RedisReadTimeout )
309285 costStorage := redisStorage .NewStore (costRedisStorage , cfg .RedisWriteTimeout , cfg .RedisReadTimeout )
@@ -317,12 +293,17 @@ func main() {
317293
318294 psCache := redisStorage .NewProviderSettingsCache (providerSettingsRedisCache , cfg .RedisWriteTimeout , cfg .RedisReadTimeout )
319295 keysCache := redisStorage .NewKeysCache (keysRedisCache , cfg .RedisWriteTimeout , cfg .RedisReadTimeout )
296+ requestsLimitStorage := redisStorage .NewStore (requestsLimitRedisStorage , cfg .RedisWriteTimeout , cfg .RedisReadTimeout )
320297
321- v := validator .NewValidator (costLimitCache , rateLimitCache , costStorage )
298+ encryptor , err := encryptor .NewEncryptor (cfg .DecryptionEndpoint , cfg .EncryptionEndpoint , cfg .EnableEncrytion , cfg .EncryptionTimeout , cfg .Audience )
299+ if cfg .EnableEncrytion && err != nil {
300+ log .Sugar ().Fatalf ("error creating encryption client: %v" , err )
301+ }
302+ v := validator .NewValidator (costLimitCache , rateLimitCache , costStorage , requestsLimitStorage )
322303
323- m := manager .NewManager (store , costLimitCache , rateLimitCache , accessCache , keysCache )
304+ m := manager .NewManager (store , costLimitCache , rateLimitCache , accessCache , keysCache , requestsLimitStorage )
324305 krm := manager .NewReportingManager (costStorage , store , store , v )
325- psm := manager .NewProviderSettingsManager (store , psCache )
306+ psm := manager .NewProviderSettingsManager (store , psCache , encryptor )
326307 cpm := manager .NewCustomProvidersManager (store , cpMemStore )
327308 rm := manager .NewRouteManager (store , store , rMemStore , psm )
328309 pm := manager .NewPolicyManager (store , rMemStore )
@@ -357,9 +338,9 @@ func main() {
357338
358339 uv := validator .NewUserValidator (userCostLimitCache , userRateLimitCache , userCostStorage )
359340
360- rec := recorder .NewRecorder (costStorage , userCostStorage , costLimitCache , userCostLimitCache , ce , store )
341+ rec := recorder .NewRecorder (costStorage , userCostStorage , costLimitCache , userCostLimitCache , ce , store , requestsLimitStorage )
361342 rlm := manager .NewRateLimitManager (rateLimitCache , userRateLimitCache )
362- a := auth .NewAuthenticator (psm , m , rm , store )
343+ a := auth .NewAuthenticator (psm , m , rm , store , encryptor )
363344
364345 c := cache .NewCache (apiCache )
365346
0 commit comments