2024-01-15 09:22:00 +00:00
|
|
|
import Redis, { RedisOptions } from 'ioredis'
|
|
|
|
|
import { env } from './env'
|
|
|
|
|
|
|
|
|
|
export type RedisDataSourceOptions = {
|
|
|
|
|
REDIS_URL?: string
|
|
|
|
|
REDIS_CERT?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class RedisDataSource {
|
|
|
|
|
options: RedisDataSourceOptions
|
|
|
|
|
isInitialized: Boolean
|
|
|
|
|
|
|
|
|
|
ioRedisClient: Redis | undefined = undefined
|
|
|
|
|
|
|
|
|
|
constructor(options: RedisDataSourceOptions) {
|
|
|
|
|
this.options = options
|
|
|
|
|
this.isInitialized = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async initialize(): Promise<this> {
|
|
|
|
|
if (this.isInitialized) throw 'Error already initialized'
|
|
|
|
|
|
|
|
|
|
this.ioRedisClient = createIORedisClient(this.options)
|
|
|
|
|
this.isInitialized = true
|
|
|
|
|
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setOptions(options: RedisDataSourceOptions): void {
|
|
|
|
|
this.options = options
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async shutdown(): Promise<void> {
|
|
|
|
|
if (this.ioRedisClient && this.ioRedisClient.status == 'ready') {
|
|
|
|
|
this.ioRedisClient.quit()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const createIORedisClient = (
|
|
|
|
|
options: RedisDataSourceOptions
|
|
|
|
|
): Redis | undefined => {
|
|
|
|
|
let redisURL = options.REDIS_URL
|
|
|
|
|
if (!redisURL) {
|
|
|
|
|
throw 'Error: no redisURL supplied'
|
|
|
|
|
}
|
2024-01-16 14:04:19 +00:00
|
|
|
const tls =
|
|
|
|
|
redisURL.startsWith('rediss://') && options.REDIS_CERT
|
|
|
|
|
? {
|
2024-01-15 09:22:00 +00:00
|
|
|
ca: options.REDIS_CERT,
|
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
|
}
|
2024-01-16 14:04:19 +00:00
|
|
|
: undefined
|
|
|
|
|
|
|
|
|
|
const redisOptions = {
|
|
|
|
|
tls,
|
|
|
|
|
connectTimeout: 10000,
|
|
|
|
|
maxRetriesPerRequest: null,
|
2024-01-16 15:02:46 +00:00
|
|
|
// reconnectOnError: (err: Error) => {
|
|
|
|
|
// const targetErrors = [/READONLY/, /ETIMEDOUT/]
|
2024-01-15 09:22:00 +00:00
|
|
|
|
2024-01-16 15:02:46 +00:00
|
|
|
// targetErrors.forEach((targetError) => {
|
|
|
|
|
// if (targetError.test(err.message)) {
|
|
|
|
|
// // Only reconnect when the error contains the keyword
|
|
|
|
|
// return true
|
|
|
|
|
// }
|
|
|
|
|
// })
|
2024-01-16 14:04:19 +00:00
|
|
|
|
2024-01-16 15:02:46 +00:00
|
|
|
// return false
|
|
|
|
|
// },
|
2024-01-16 14:04:19 +00:00
|
|
|
retryStrategy: (times: number) => {
|
2024-01-16 15:02:46 +00:00
|
|
|
// if (times > 10) {
|
|
|
|
|
// // End reconnecting after a specific number of tries and flush all commands with a individual error
|
|
|
|
|
// return null
|
|
|
|
|
// }
|
2024-01-16 14:04:19 +00:00
|
|
|
|
2024-01-16 15:02:46 +00:00
|
|
|
// // reconnect after
|
|
|
|
|
// return Math.min(times * 50, 2000)
|
|
|
|
|
return 10
|
2024-01-16 14:04:19 +00:00
|
|
|
},
|
|
|
|
|
}
|
2024-01-16 14:43:06 +00:00
|
|
|
console.log('connecting to redis: ', { redisURL, redisOptions })
|
2024-01-16 14:04:19 +00:00
|
|
|
return new Redis(redisURL, redisOptions)
|
2024-01-15 09:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const redisDataSource = new RedisDataSource({
|
|
|
|
|
REDIS_URL: env.redis.url,
|
|
|
|
|
REDIS_CERT: env.redis.cert,
|
|
|
|
|
})
|