Skip to content

Commit

Permalink
feat: enable redis tls (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
abretonc7s authored Mar 6, 2024
1 parent f19ef09 commit e9dcf5a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/sdk-socket-server-next/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SEGMENT_API_KEY_DEBUG=123456789
# Example REDIS_NODES format: "redis://host1:6379,redis://host2:6379"
REDIS_NODES=redis://localhost:6380,redis://localhost:6381,redis://localhost:6382
REDIS_PASSWORD=redis_password
REDIS_TLS=false
RATE_LIMITER=false
RATE_LIMITER_HTTP_WINDOW_MINUTE=1
RATE_LIMITER_HTTP_LIMIT=100000
Expand Down
58 changes: 50 additions & 8 deletions packages/sdk-socket-server-next/src/api-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cors from 'cors';
import express from 'express';
import { rateLimit } from 'express-rate-limit';
import helmet from 'helmet';
import { Cluster, ClusterOptions, Redis } from 'ioredis';
import { Cluster, ClusterOptions, Redis, RedisOptions } from 'ioredis';
import { logger } from './logger';
import { isDevelopment, isDevelopmentServer } from '.';

Expand Down Expand Up @@ -37,19 +37,61 @@ if (redisNodes.length === 0) {
}

const redisCluster = process.env.REDIS_CLUSTER === 'true';
const redisTLS = process.env.REDIS_TLS === 'true';

let redisClient: Cluster | Redis | undefined;
const redisClusterOptions: ClusterOptions = {
// slotsRefreshTimeout: 2000,
redisOptions: {
// tls: {}, // WARN: enabling tls would fail the client if not setup with correct params
password: process.env.REDIS_PASSWORD,
},

export const getRedisOptions = (
isTls: boolean,
password: string | undefined,
): RedisOptions => {
const tlsOptions = {
tls: {
checkServerIdentity: (/* host, cert*/) => {
return undefined;
},
},
};

return {
...(password && { password }),
...(isTls && tlsOptions),
connectTimeout: 30000,
maxRetriesPerRequest: 4,
retryStrategy: (times) => Math.min(times * 30, 1000),
reconnectOnError: (error) => {
// eslint-disable-next-line require-unicode-regexp
const targetErrors = [/READONLY/, /ETIMEDOUT/];
return targetErrors.some((targetError) =>
targetError.test(error.message),
);
},
};
};

export const getRedisClient = () => {
if (!redisClient) {
if (redisCluster) {
logger.info('Connecting to Redis Cluster');
logger.info('Connecting to Redis Cluster...');

const redisOptions = getRedisOptions(
redisTLS,
process.env.REDIS_PASSWORD,
);
const redisClusterOptions: ClusterOptions = {
dnsLookup: (address, callback) => callback(null, address),
slotsRefreshTimeout: 2000,
slotsRefreshInterval: 4000,
clusterRetryStrategy: (times) => Math.min(times * 30, 1000),
enableAutoPipelining: true,
redisOptions,
};

logger.debug(
'Redis Cluster options:',
JSON.stringify(redisClusterOptions, null, 2),
);

redisClient = new Cluster(redisNodes, redisClusterOptions);
} else {
logger.info('Connecting to single Redis node');
Expand Down

0 comments on commit e9dcf5a

Please sign in to comment.