From 6d04b4add30524756c8d42c13b10c7ef0dafe120 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 19 Sep 2017 15:28:03 -0600 Subject: [PATCH] Redis Cluster Support - Deprecated unused `redis` options in favour of options from `ioredis`. - Added support for ioredis Cluster. - Enabled more extended redis client configuration. --- config.js | 37 ++++++++++++++++---- docs/_docs/02-01-configuration.md | 19 +++++----- services/redis.js | 58 ++++++++++++------------------- 3 files changed, 64 insertions(+), 50 deletions(-) diff --git a/config.js b/config.js index 1a54ece5a..3d8b6213c 100644 --- a/config.js +++ b/config.js @@ -86,14 +86,17 @@ const CONFIG = { MONGO_URL: process.env.TALK_MONGO_URL, REDIS_URL: process.env.TALK_REDIS_URL, - // REDIS_RECONNECTION_MAX_ATTEMPTS is the amount of attempts that a redis - // connection will attempt to reconnect before aborting with an error. - REDIS_RECONNECTION_MAX_ATTEMPTS: parseInt(process.env.TALK_REDIS_RECONNECTION_MAX_ATTEMPTS || '100'), + // REDIS_CLIENT_CONFIG is the optional configuration that is merged with the + // function config to provide deep control of the redis connection beheviour. + REDIS_CLIENT_CONFIG: process.env.TALK_REDIS_CLIENT_CONFIGURATION || '{}', - // REDIS_RECONNECTION_MAX_RETRY_TIME is the time in string format for the - // maximum amount of time that a client can be considered "connecting" before - // attempts at reconnection are aborted with an error. - REDIS_RECONNECTION_MAX_RETRY_TIME: ms(process.env.TALK_REDIS_RECONNECTION_MAX_RETRY_TIME || '1 min'), + // REDIS_CLUSTER_MODE allows configuration on the type of cluster mode enabled + // on the redis client. Can be either `NONE` or `CLUSTER`. + REDIS_CLUSTER_MODE: process.env.TALK_REDIS_CLUSTER_MODE || 'NONE', + + // REDIS_CLUSTER_CONFIGURATION contains the json string for the redis cluster + // configuration. + REDIS_CLUSTER_CONFIGURATION: process.env.TALK_REDIS_CLUSTER_CONFIGURATION || '[]', // REDIS_RECONNECTION_BACKOFF_FACTOR is the factor that will be multiplied // against the current attempt count inbetween attempts to connect to redis. @@ -238,6 +241,26 @@ if (process.env.NODE_ENV === 'test' && !CONFIG.REDIS_URL) { CONFIG.REDIS_URL = 'redis://localhost/1'; } +// REDIS_CLUSTER_CONFIGURATION should be parsed when the cluster mode !== none. +if (CONFIG.REDIS_CLUSTER_MODE === 'CLUSTER') { + try { + CONFIG.REDIS_CLUSTER_CONFIGURATION = JSON.parse(CONFIG.REDIS_CLUSTER_CONFIGURATION); + } catch (err) { + throw new Error('TALK_REDIS_CLUSTER_CONFIGURATION is not valid JSON, see https://github.com/luin/ioredis#cluster for valid syntax of the list of cluster nodes'); + } + + if (!Array.isArray(CONFIG.REDIS_CLUSTER_CONFIGURATION)) { + throw new Error('TALK_REDIS_CLUSTER_MODE is CLUSTER, but the TALK_REDIS_CLUSTER_CONFIGURATION is invalid, see https://github.com/luin/ioredis#cluster for valid syntax of the list of cluster nodes'); + } + + if (CONFIG.REDIS_CLUSTER_CONFIGURATION.length === 0) { + throw new Error('TALK_REDIS_CLUSTER_CONFIGURATION must have at least one node specified in the cluster, see https://github.com/luin/ioredis#cluster for valid syntax of the list of cluster nodes'); + } +} + +// Client config is a JSON encoded string, defaulting to `{}`. +CONFIG.REDIS_CLIENT_CONFIG = JSON.parse(CONFIG.REDIS_CLIENT_CONFIG); + //------------------------------------------------------------------------------ // Recaptcha configuration //------------------------------------------------------------------------------ diff --git a/docs/_docs/02-01-configuration.md b/docs/_docs/02-01-configuration.md index 021735fb9..b3e637e27 100644 --- a/docs/_docs/02-01-configuration.md +++ b/docs/_docs/02-01-configuration.md @@ -55,18 +55,21 @@ These are only used during the webpack build. #### Advanced {:.no_toc} -- `TALK_REDIS_RECONNECTION_MAX_ATTEMPTS` (_optional_) - the amount of attempts - that a redis connection will attempt to reconnect before aborting with an - error. (Default `100`) -- `TALK_REDIS_RECONNECTION_MAX_RETRY_TIME` (_optional_) - the time in string - format for the maximum amount of time that a client can be considered - "connecting" before attempts at reconnection are aborted with an error. - (Default `1 min`) +- `TALK_REDIS_CLIENT_CONFIG` (_optional_) - configuration overrides for the + redis client configuration in a JSON encoded string. Configuration is + overridden as the second parameter to the redis client constructor, and is + merged with default configuration. (Default `{}`) - `TALK_REDIS_RECONNECTION_BACKOFF_FACTOR` (_optional_) - the time factor that will be multiplied against the current attempt count inbetween attempts to connect to redis. (Default `500 ms`) - `TALK_REDIS_RECONNECTION_BACKOFF_MINIMUM_TIME` (_optional_) - the minimum time used to delay before attempting to reconnect to redis. (Default `1 sec`) +- `TALK_REDIS_CLUSTER_MODE` (_optional_) - the cluster mode of the redis client. + Can be either `NONE` or `CLUSTER`. (Default `NONE`) +- `TALK_REDIS_CLUSTER_CONFIGURATION` (_optional_) - the json serialized form of + the cluster nodes. Only required when `TALK_REDIS_CLUSTER_MODE=CLUSTER`. See + https://github.com/luin/ioredis#cluster for configuration details. + (Default `[]`) ### Server @@ -131,7 +134,7 @@ is not needed in most situations. use to set a cookie containing a JWT that was issued by Talk. (Default `process.env.TALK_JWT_COOKIE_NAME`) - `TALK_JWT_COOKIE_NAMES` (_optional_) - the different cookie names to check for - a JWT token in, seperated by `,`. By default, we always use the + a JWT token in, separated by `,`. By default, we always use the `process.env.TALK_JWT_COOKIE_NAME` and `process.env.TALK_JWT_SIGNING_COOKIE_NAME` for this value. Any additional cookie names specified here will be appended to the list of cookie names to inspect. diff --git a/services/redis.js b/services/redis.js index 5fd1b78d3..9b00b19db 100644 --- a/services/redis.js +++ b/services/redis.js @@ -1,12 +1,14 @@ const Redis = require('ioredis'); +const merge = require('lodash/merge'); const debug = require('debug')('talk:services:redis'); const enabled = require('debug').enabled('talk:services:redis'); const { REDIS_URL, - REDIS_RECONNECTION_MAX_ATTEMPTS, - REDIS_RECONNECTION_MAX_RETRY_TIME, REDIS_RECONNECTION_BACKOFF_FACTOR, REDIS_RECONNECTION_BACKOFF_MINIMUM_TIME, + REDIS_CLIENT_CONFIG, + REDIS_CLUSTER_MODE, + REDIS_CLUSTER_CONFIGURATION, } = require('../config'); const attachMonitors = (client) => { @@ -16,8 +18,8 @@ const attachMonitors = (client) => { if (enabled) { client.on('connect', () => debug('client connected')); client.on('ready', () => debug('client ready')); - client.on('reconnecting', () => debug('client connection lost, attempting to reconnect')); client.on('close', () => debug('client closed the connection')); + client.on('reconnecting', () => debug('client connection lost, attempting to reconnect')); client.on('end', () => debug('client ended')); } @@ -27,44 +29,30 @@ const attachMonitors = (client) => { console.error('Error connecting to redis:', err); } }); + client.on('node error', (err) => debug('node error', err)); }; -const connectionOptions = { - retry_strategy: function(options) { - if (options.error && options.error.code !== 'ECONNREFUSED') { +function retryStrategy(times) { + const delay = Math.max(times * REDIS_RECONNECTION_BACKOFF_FACTOR, REDIS_RECONNECTION_BACKOFF_MINIMUM_TIME); - debug('retry strategy: none, an error occured'); + debug(`retry strategy: try to reconnect ${delay} ms from now`); - // End reconnecting on a specific error and flush all commands with a individual error - return options.error; - } - if (options.total_retry_time > REDIS_RECONNECTION_MAX_RETRY_TIME) { - - debug('retry strategy: none, exhausted retry time'); - - // End reconnecting after a specific timeout and flush all commands with a individual error - return new Error('Retry time exhausted'); - } - - if (options.attempt > REDIS_RECONNECTION_MAX_ATTEMPTS) { - - debug('retry strategy: none, exhausted retry attempts'); - - // End reconnecting with built in error - return undefined; - } - - // reconnect after - const delay = Math.max(options.attempt * REDIS_RECONNECTION_BACKOFF_FACTOR, REDIS_RECONNECTION_BACKOFF_MINIMUM_TIME); - - debug(`retry strategy: try to reconnect ${delay} ms from now`); - - return delay; - } -}; + return delay; +} const createClient = () => { - let client = new Redis(REDIS_URL, connectionOptions); + let client; + if (REDIS_CLUSTER_MODE === 'NONE') { + client = new Redis(REDIS_URL, merge({}, REDIS_CLIENT_CONFIG, { + retryStrategy, + })); + } else if (REDIS_CLUSTER_MODE === 'CLUSTER') { + client = new Redis.Cluster(REDIS_CLUSTER_CONFIGURATION, merge({ + scaleReads: 'slave', + }, REDIS_CLIENT_CONFIG, { + clusterRetryStrategy: retryStrategy, + })); + } // Attach the monitors that will print debug messages to the console. attachMonitors(client);