Skip to content

Commit e2d7e36

Browse files
refactor(redis): share connection defaults via one helper
Extract keepAlive/connectTimeout/enableOfflineQueue + TLS SNI into a single getRedisConnectionDefaults helper. Main client and pub/sub clients both spread it; caller-specific retry/timeout policy stays per-caller (pub/sub still needs maxRetriesPerRequest: null and a different retry strategy for SUBSCRIBE).
1 parent b58fa66 commit e2d7e36

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

apps/sim/lib/core/config/redis.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import { toError } from '@sim/utils/errors'
33
import { randomFloat } from '@sim/utils/random'
4-
import Redis from 'ioredis'
4+
import Redis, { type RedisOptions } from 'ioredis'
55
import { env } from '@/lib/core/config/env'
66

77
const logger = createLogger('Redis')
@@ -16,9 +16,7 @@ const redisUrl = env.REDIS_URL
1616
*
1717
* For DNS hosts: no override needed, default verification works.
1818
*/
19-
export function resolveRedisTlsOptions(
20-
url: string | undefined
21-
): { servername: string } | undefined {
19+
function resolveRedisTlsOptions(url: string | undefined): { servername: string } | undefined {
2220
if (!url) return undefined
2321
let parsed: URL
2422
try {
@@ -39,6 +37,23 @@ export function resolveRedisTlsOptions(
3937
return { servername: env.REDIS_TLS_SERVERNAME }
4038
}
4139

40+
/**
41+
* Shared connection defaults — keepAlive, connectTimeout, enableOfflineQueue,
42+
* and TLS SNI when REDIS_URL targets an IP. Every Redis client we open should
43+
* spread this; callers add their own retry / timeout policy on top.
44+
*/
45+
export function getRedisConnectionDefaults(
46+
url: string | undefined
47+
): Pick<RedisOptions, 'keepAlive' | 'connectTimeout' | 'enableOfflineQueue' | 'tls'> {
48+
const tls = resolveRedisTlsOptions(url)
49+
return {
50+
keepAlive: 1000,
51+
connectTimeout: 10000,
52+
enableOfflineQueue: true,
53+
...(tls ? { tls } : {}),
54+
}
55+
}
56+
4257
let globalRedisClient: Redis | null = null
4358
let pingFailures = 0
4459
let pingInterval: NodeJS.Timeout | null = null
@@ -119,18 +134,15 @@ export function getRedisClient(): Redis | null {
119134
if (globalRedisClient) return globalRedisClient
120135

121136
// Outside the try/catch so config errors aren't silently swallowed.
122-
const tls = resolveRedisTlsOptions(redisUrl)
137+
const defaults = getRedisConnectionDefaults(redisUrl)
123138

124139
try {
125140
logger.info('Initializing Redis client')
126141

127142
globalRedisClient = new Redis(redisUrl, {
128-
keepAlive: 1000,
129-
connectTimeout: 10000,
143+
...defaults,
130144
commandTimeout: 5000,
131145
maxRetriesPerRequest: 5,
132-
enableOfflineQueue: true,
133-
...(tls ? { tls } : {}),
134146

135147
retryStrategy: (times) => {
136148
if (times > 10) {

apps/sim/lib/events/pubsub.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { EventEmitter } from 'events'
99
import { createLogger } from '@sim/logger'
1010
import Redis, { type RedisOptions } from 'ioredis'
1111
import { env } from '@/lib/core/config/env'
12-
import { resolveRedisTlsOptions } from '@/lib/core/config/redis'
12+
import { getRedisConnectionDefaults } from '@/lib/core/config/redis'
1313

1414
const logger = createLogger('PubSub')
1515

@@ -34,18 +34,13 @@ class RedisPubSubChannel<T> implements PubSubChannel<T> {
3434
redisUrl: string,
3535
private config: PubSubChannelConfig
3636
) {
37-
const tls = resolveRedisTlsOptions(redisUrl)
38-
3937
const commonOpts = {
40-
keepAlive: 1000,
41-
connectTimeout: 10000,
38+
...getRedisConnectionDefaults(redisUrl),
4239
maxRetriesPerRequest: null,
43-
enableOfflineQueue: true,
4440
retryStrategy: (times: number) => {
4541
if (times > 10) return 30000
4642
return Math.min(times * 500, 5000)
4743
},
48-
...(tls ? { tls } : {}),
4944
} satisfies RedisOptions
5045

5146
this.pub = new Redis(redisUrl, { ...commonOpts, connectionName: `${config.label}-pub` })

0 commit comments

Comments
 (0)