From a509a52993e546751af4a5ed2f9cd311e55e5d9c Mon Sep 17 00:00:00 2001 From: Mononaut Date: Wed, 2 Aug 2023 20:36:56 +0900 Subject: [PATCH] Avoid initializing redis in worker threads --- backend/src/api/redis-cache.ts | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/backend/src/api/redis-cache.ts b/backend/src/api/redis-cache.ts index fcde8013a..133a43a35 100644 --- a/backend/src/api/redis-cache.ts +++ b/backend/src/api/redis-cache.ts @@ -23,24 +23,21 @@ class RedisCache { private cacheQueue: MempoolTransactionExtended[] = []; private txFlushLimit: number = 10000; - constructor() { - if (config.REDIS.ENABLED) { - const redisConfig = { - socket: { - path: config.REDIS.UNIX_SOCKET_PATH - }, - database: NetworkDB[config.MEMPOOL.NETWORK], - }; - this.client = createClient(redisConfig); - this.client.on('error', (e) => { - logger.err(`Error in Redis client: ${e instanceof Error ? e.message : e}`); - }); - this.$ensureConnected(); - } - } - private async $ensureConnected(): Promise { if (!this.connected && config.REDIS.ENABLED) { + if (!this.client) { + const redisConfig = { + socket: { + path: config.REDIS.UNIX_SOCKET_PATH + }, + database: NetworkDB[config.MEMPOOL.NETWORK], + }; + this.client = createClient(redisConfig); + this.client.on('error', (e) => { + logger.err(`Error in Redis client: ${e instanceof Error ? e.message : e}`); + }); + } + return this.client.connect().then(async () => { this.connected = true; logger.info(`Redis client connected`);