Commit 577ff7fe authored by ThinhNC's avatar ThinhNC

feat(infra): support REDIS_URL connection string and TLS for cloud Redis

parent 97360cba
......@@ -13,6 +13,7 @@ JWT_REFRESH_SECRET=change_me_refresh_secret
JWT_ACCESS_EXPIRES_IN=1d
JWT_REFRESH_EXPIRES_IN=7d
REDIS_URL=redis://red-d2xxxxxxxxxxxxxxxxxx:6379
REDIS_HOST=localhost
REDIS_PORT=7379
REDIS_PASSWORD=
......
......@@ -28,23 +28,33 @@ export class CacheService {
private initRedis() {
try {
this.redis = new Redis({
host: envConfig.redis.host,
port: envConfig.redis.port,
password: envConfig.redis.password,
lazyConnect: true,
maxRetriesPerRequest: 3,
retryStrategy: (times) => {
const retryStrategy = (times: number) => {
if (times > 3) {
this.logger.warn('Failed to connect to Redis. Falling back to in-memory cache.');
this.isRedisConnected = false;
this.redis?.disconnect();
this.initMemoryCleanup();
return null; // Stop retrying
return null;
}
return Math.min(times * 100, 2000);
},
};
if (envConfig.redis.url) {
this.redis = new Redis(envConfig.redis.url, {
lazyConnect: true,
maxRetriesPerRequest: 3,
retryStrategy,
});
} else {
this.redis = new Redis({
host: envConfig.redis.host,
port: envConfig.redis.port,
password: envConfig.redis.password,
lazyConnect: true,
maxRetriesPerRequest: 3,
retryStrategy,
});
}
this.redis.on('connect', () => {
this.logger.info('Successfully connected to Redis.');
......
......@@ -150,9 +150,41 @@ export const envConfig = {
},
},
redis: {
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379', 10),
password: process.env.REDIS_PASSWORD || undefined,
url: process.env.REDIS_URL || '',
host: (() => {
if (process.env.REDIS_HOST) return process.env.REDIS_HOST;
if (process.env.REDIS_URL) {
try {
return new URL(process.env.REDIS_URL).hostname || 'localhost';
} catch {
return 'localhost';
}
}
return 'localhost';
})(),
port: (() => {
if (process.env.REDIS_PORT) return parseInt(process.env.REDIS_PORT, 10);
if (process.env.REDIS_URL) {
try {
const p = new URL(process.env.REDIS_URL).port;
return p ? parseInt(p, 10) : 6379;
} catch {
return 6379;
}
}
return 6379;
})(),
password: (() => {
if (process.env.REDIS_PASSWORD) return process.env.REDIS_PASSWORD;
if (process.env.REDIS_URL) {
try {
return new URL(process.env.REDIS_URL).password || undefined;
} catch {
return undefined;
}
}
return undefined;
})(),
enabled: process.env.REDIS_ENABLED !== 'false',
},
rateLimit: {
......
......@@ -38,10 +38,12 @@ export class WebhookQueueService {
return;
}
const isTls = envConfig.redis.url.startsWith('rediss://');
const connection = {
host: redisHost,
port: redisPort,
password: redisPassword,
tls: isTls ? {} : undefined,
maxRetriesPerRequest: null,
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment