Skip to content

Commit bc6187e

Browse files
committed
fix: add TLS Redis support by using REDIS_TLS_URL/REDIS_URL and fallback to legacy config
1 parent c618ac1 commit bc6187e

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/core/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export const config = {
133133
port: toNumber(process.env.REDIS_PORT, 6379),
134134
password: process.env.REDIS_PASSWORD,
135135
db: toNumber(process.env.REDIS_DB, 0),
136+
tlsUrl: process.env.REDIS_TLS_URL || process.env.REDIS_URL,
136137
},
137138
} as const;
138139

src/core/utils/connections.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { logger } from 'kv-logger';
12
import { createClient } from 'redis';
23
import { Sequelize } from 'sequelize';
34
import { config } from '../config';
@@ -9,19 +10,44 @@ export const sequelize = new Sequelize(
910
config.db,
1011
);
1112

12-
export const redisClient = createClient({
13-
socket: {
14-
host: config.redis.host,
15-
port: config.redis.port,
16-
reconnectStrategy: (retries) => {
17-
if (retries > 10) {
18-
return new Error('Retry count exhausted');
19-
}
13+
const redisTlsUrl = config.redis.tlsUrl; // REDIS_URL 환경변수 우선 사용 (TLS 포함)
14+
const isTlsSupported = redisTlsUrl?.startsWith?.('rediss://');
2015

21-
return retries * 100;
22-
},
23-
},
24-
password: config.redis.password,
25-
database: config.redis.db,
16+
export const redisClient = redisTlsUrl
17+
? createClient({
18+
url: redisTlsUrl,
19+
socket: {
20+
tls: isTlsSupported,
21+
reconnectStrategy: (retries: number) => {
22+
if (retries > 10) {
23+
return new Error('Retry count exhausted');
24+
}
25+
return retries * 100;
26+
},
27+
},
28+
})
29+
: createClient({
30+
socket: {
31+
host: config.redis.host,
32+
port: config.redis.port,
33+
reconnectStrategy: (retries: number) => {
34+
if (retries > 10) {
35+
return new Error('Retry count exhausted');
36+
}
37+
38+
return retries * 100;
39+
},
40+
},
41+
password: config.redis.password,
42+
database: config.redis.db,
43+
});
44+
45+
// 에러 로깅 (Unhandled 'error' 로 인한 앱크래시 방지)
46+
redisClient.on('error', (err) => {
47+
logger.error('Redis Client Error', err?.message || JSON.stringify(err));
48+
});
49+
50+
// connect 시도 (커넥션 실패시 앱크래시 방지)
51+
redisClient.connect().catch((err) => {
52+
logger.error('Redis connect error', err);
2653
});
27-
redisClient.connect();

0 commit comments

Comments
 (0)