feat(server): add redis sentinel support (#5593)

* feat(server): add redis sentinel support

closes #5141

* Styling

---------

Co-authored-by: Chocobozzz <me@florianbigard.com>
This commit is contained in:
kontrollanten 2023-05-10 09:52:50 +02:00 committed by GitHub
parent 5170f492b9
commit 674f8ddd41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 8 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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[] = []

View File

@ -39,7 +39,13 @@ const CONFIG = {
PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
DB: config.has('redis.db') ? config.get<number>('redis.db') : null
DB: config.has('redis.db') ? config.get<number>('redis.db') : null,
SENTINEL: {
ENABLED: config.has('redis.sentinel.enabled') ? config.get<boolean>('redis.sentinel.enabled') : false,
ENABLE_TLS: config.has('redis.sentinel.enable_tls') ? config.get<boolean>('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<string>('redis.sentinel.master_name') : null
}
},
SMTP: {
TRANSPORT: config.has('smtp.transport') ? config.get<string>('smtp.transport') : 'smtp',

View File

@ -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,