From 98f202f6a9f44f8440afaaeebba499ac9e37fe1e Mon Sep 17 00:00:00 2001 From: Ajay Date: Mon, 7 Nov 2022 22:51:26 -0500 Subject: [PATCH] Add max connection check to redis set --- src/utils/redis.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/utils/redis.ts b/src/utils/redis.ts index 49d7ed0..900d0fb 100644 --- a/src/utils/redis.ts +++ b/src/utils/redis.ts @@ -39,6 +39,7 @@ if (config.redis?.enabled) { const get = client.get.bind(client); + const set = client.set.bind(client); const getRead = readClient?.get?.bind(readClient); exportClient.get = (key) => new Promise((resolve, reject) => { if (activeRequests > config.redis.maxConnections) { @@ -66,6 +67,22 @@ if (config.redis?.enabled) { reject(err); }); }); + exportClient.set = (key, value) => new Promise((resolve, reject) => { + if (activeRequests > config.redis.maxConnections) { + reject("Too many active requests"); + return; + } + + activeRequests++; + + set(key, value).then((reply) => { + activeRequests--; + resolve(reply); + }).catch((err) => { + activeRequests--; + reject(err); + }); + }); exportClient.increment = (key) => new Promise((resolve, reject) => void client.multi() .incr(key)