From 9e4328c526eb70f7c8245b6886951ee0a150bb12 Mon Sep 17 00:00:00 2001 From: Evan Thomas Date: Tue, 30 Apr 2024 13:10:54 -0400 Subject: [PATCH] fix: Use correct type when connecting to Redis Cluster Connecting to a redis cluster using Redis runs into a variety of issues. For example, it will not follow MOVED errors. In order to get desired behavior, use the Redis.Cluster type. This requires a reworking of how we configure the server to connect to Redis because of differences in constructing a Redis instance vs a Redis.Cluster instance. --- src/state.ts | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/state.ts b/src/state.ts index d64717c..da774e3 100644 --- a/src/state.ts +++ b/src/state.ts @@ -1,12 +1,35 @@ // store state of auth -import Redis from 'ioredis'; +import Redis, {Cluster} from 'ioredis'; import { STATE_LIFETIME } from './constants'; -let redis: Redis | undefined; +const redisConfig = { + host: process.env.REDIS_HOST, + port: +(process.env.REDIS_PORT || 6379), + options: { + username: process.env.REDIS_USERNAME, + password: process.env.REDIS_PASSWORD, + ...(process.env.REDIS_ENABLE_TLS === 'true') && {tls: { + rejectUnauthorized: process.env.REDIS_CLUSTER !== 'true', // cert validation throwing error in cluster mode + }} + }, + isCluster: process.env.REDIS_CLUSTER === 'true', +}; -if (process.env.REDIS_URL) { - redis = new Redis(process.env.REDIS_URL); +let redis: Redis | Cluster | undefined; + + +if (process.env.REDIS_HOST) { + if (redisConfig.isCluster) { + redis = new Redis.Cluster([{host: redisConfig.host, port: redisConfig.port}], {redisOptions: redisConfig.options}); + } else { + redis = new Redis({ + host: redisConfig.host, + port: redisConfig.port, + ...redisConfig.options + } + ); + } } type State = {