@@ -6,6 +6,7 @@ import {ConsumePointsError, RateLimiterProvider} from "@services"
66import * as TE from "fp-ts/lib/TaskEither"
77import { pipe } from "fp-ts/lib/function"
88import { TaskEither } from "fp-ts/lib/TaskEither"
9+ import { waitForRedisConnection } from "../redis/redis.utils"
910
1011export { RateLimiterRes } from "rate-limiter-flexible"
1112
@@ -26,19 +27,7 @@ export class RedisRateLimiterProvider implements RateLimiterProvider, OnModuleIn
2627 enableOfflineQueue : false
2728 } )
2829
29- // Wait for the Redis TCP connection to be established before accepting requests.
30- // This is required because enableOfflineQueue is false, which means commands sent
31- // before the connection is ready will fail immediately instead of being queued.
32- // The "error" listener ensures that connection failures (e.g. wrong host/port)
33- // surface at startup rather than silently falling through to the fail-open path.
34- try {
35- await new Promise < void > ( ( resolve , reject ) => {
36- this . getRedisClient ( ) . once ( "ready" , resolve )
37- this . getRedisClient ( ) . once ( "error" , reject )
38- } )
39- } catch {
40- throw new Error ( `Unable to connect to Redis at ${ config . host } :${ config . port } ` )
41- }
30+ await waitForRedisConnection ( this . getRedisClient ( ) , "rate-limiter-redis-client" )
4231
4332 this . rateLimiter = new RateLimiterRedis ( {
4433 storeClient : this . redisClient ,
@@ -71,9 +60,7 @@ export class RedisRateLimiterProvider implements RateLimiterProvider, OnModuleIn
7160 this . getRateLimiter ( )
7261 . consume ( key , points )
7362 . catch ( error => {
74- if ( error instanceof RateLimiterRes ) {
75- return error
76- }
63+ if ( error instanceof RateLimiterRes ) return error
7764 throw error
7865 } )
7966 ) ,
0 commit comments