Files
shields/core/token-pooling/redis-token-persistence.js
Paul Melnikow ec0264dfb7 Fix Redis prod issue for Node 9 (#3707)
Encountered while testing #3332
2019-07-12 19:51:05 -04:00

44 lines
1014 B
JavaScript

'use strict'
const { URL } = require('url')
const Redis = require('ioredis')
const log = require('../server/log')
const TokenPersistence = require('./token-persistence')
module.exports = class RedisTokenPersistence extends TokenPersistence {
constructor({ url, key }) {
super()
this.url = url
this.key = key
}
async initialize() {
const options =
this.url && this.url.startsWith('rediss:')
? {
// https://www.compose.com/articles/ssl-connections-arrive-for-redis-on-compose/
tls: { servername: new URL(this.url).hostname },
}
: undefined
this.redis = new Redis(this.url, options)
this.redis.on('error', e => {
log.error(e)
})
const tokens = await this.redis.smembers(this.key)
return tokens
}
async stop() {
await this.redis.quit()
}
async onTokenAdded(token) {
await this.redis.sadd(this.key, token)
}
async onTokenRemoved(token) {
await this.redis.srem(this.key, token)
}
}