From 674f8ddd415ff4faf100ff35cf2511f84f60fea9 Mon Sep 17 00:00:00 2001 From: kontrollanten <6680299+kontrollanten@users.noreply.github.com> Date: Wed, 10 May 2023 09:52:50 +0200 Subject: [PATCH] feat(server): add redis sentinel support (#5593) * feat(server): add redis sentinel support closes #5141 * Styling --------- Co-authored-by: Chocobozzz --- config/default.yaml | 9 +++++++- config/production.yaml.example | 9 +++++++- server/initializers/checker-before-init.ts | 3 ++- server/initializers/config.ts | 8 +++++++- server/lib/redis.ts | 24 ++++++++++++++++++---- 5 files changed, 45 insertions(+), 8 deletions(-) diff --git a/config/default.yaml b/config/default.yaml index 14bb8d060..5d0eab4f5 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -65,8 +65,15 @@ database: redis: hostname: 'localhost' port: 6379 - auth: null + auth: null # Used by both standalone and sentinel db: 0 + sentinel: + enabled: false + enable_tls: false + master_name: '' + sentinels: + - hostname: '' + port: 26379 # SMTP server to send emails smtp: diff --git a/config/production.yaml.example b/config/production.yaml.example index db9c18cb8..5514f1af6 100644 --- a/config/production.yaml.example +++ b/config/production.yaml.example @@ -63,8 +63,15 @@ database: redis: hostname: 'localhost' port: 6379 - auth: null + auth: null # Used by both standalone and sentinel db: 0 + sentinel: + enabled: false + enable_tls: false + master_name: '' + sentinels: + - hostname: '' + port: 26379 # SMTP server to send emails smtp: diff --git a/server/initializers/checker-before-init.ts b/server/initializers/checker-before-init.ts index 2f5a274e4..0a315ea70 100644 --- a/server/initializers/checker-before-init.ts +++ b/server/initializers/checker-before-init.ts @@ -84,7 +84,8 @@ function checkMissedConfig () { const requiredAlternatives = [ [ // set [ 'redis.hostname', 'redis.port' ], // alternative - [ 'redis.socket' ] + [ 'redis.socket' ], + [ 'redis.sentinel.master_name', 'redis.sentinel.sentinels[0].hostname', 'redis.sentinel.sentinels[0].port' ] ] ] const miss: string[] = [] diff --git a/server/initializers/config.ts b/server/initializers/config.ts index 9c2705689..51ac5d0ce 100644 --- a/server/initializers/config.ts +++ b/server/initializers/config.ts @@ -39,7 +39,13 @@ const CONFIG = { PORT: config.has('redis.port') ? config.get('redis.port') : null, SOCKET: config.has('redis.socket') ? config.get('redis.socket') : null, AUTH: config.has('redis.auth') ? config.get('redis.auth') : null, - DB: config.has('redis.db') ? config.get('redis.db') : null + DB: config.has('redis.db') ? config.get('redis.db') : null, + SENTINEL: { + ENABLED: config.has('redis.sentinel.enabled') ? config.get('redis.sentinel.enabled') : false, + ENABLE_TLS: config.has('redis.sentinel.enable_tls') ? config.get('redis.sentinel.enable_tls') : false, + SENTINELS: config.has('redis.sentinel.sentinels') ? config.get<{ hostname: string, port: number }[]>('redis.sentinel.sentinels') : [], + MASTER_NAME: config.has('redis.sentinel.master_name') ? config.get('redis.sentinel.master_name') : null + } }, SMTP: { TRANSPORT: config.has('smtp.transport') ? config.get('smtp.transport') : 'smtp', diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 3706d2228..8430b2227 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -32,7 +32,8 @@ class Redis { if (this.initialized === true) return this.initialized = true - logger.info('Connecting to redis...') + const redisMode = CONFIG.REDIS.SENTINEL.ENABLED ? 'sentinel' : 'standalone' + logger.info('Connecting to redis ' + redisMode + '...') this.client = new IoRedis(Redis.getRedisClientOptions('', { enableAutoPipelining: true })) this.client.on('error', err => logger.error('Redis failed to connect', { err })) @@ -56,10 +57,25 @@ class Redis { this.prefix = 'redis-' + WEBSERVER.HOST + '-' } - static getRedisClientOptions (connectionName?: string, options: RedisOptions = {}): RedisOptions { + static getRedisClientOptions (name?: string, options: RedisOptions = {}): RedisOptions { + const connectionName = [ 'PeerTube', name ].join('') + const connectTimeout = 20000 // Could be slow since node use sync call to compile PeerTube + + if (CONFIG.REDIS.SENTINEL.ENABLED) { + return { + connectionName, + connectTimeout, + enableTLSForSentinelMode: CONFIG.REDIS.SENTINEL.ENABLE_TLS, + sentinelPassword: CONFIG.REDIS.AUTH, + sentinels: CONFIG.REDIS.SENTINEL.SENTINELS, + name: CONFIG.REDIS.SENTINEL.MASTER_NAME, + ...options + } + } + return { - connectionName: [ 'PeerTube', connectionName ].join(''), - connectTimeout: 20000, // Could be slow since node use sync call to compile PeerTube + connectionName, + connectTimeout, password: CONFIG.REDIS.AUTH, db: CONFIG.REDIS.DB, host: CONFIG.REDIS.HOSTNAME, -- 2.41.0