Add active connection limit to setEx as well

This commit is contained in:
Ajay 2022-11-08 22:09:03 -05:00
parent 0ead3892ba
commit 90e68caaf7

View file

@ -51,7 +51,6 @@ 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) {
@ -83,7 +82,9 @@ if (config.redis?.enabled) {
reject(err);
});
});
exportClient.set = (key, value) => new Promise((resolve, reject) => {
const setFun = <T extends Array<any>>(func: (...args: T) => Promise<string> , params: T): Promise<string> =>
new Promise((resolve, reject) => {
if (activeRequests > config.redis.maxWriteConnections) {
reject("Too many active requests");
return;
@ -93,7 +94,7 @@ if (config.redis?.enabled) {
activeRequests++;
writeRequests++;
set(key, value).then((reply) => {
func(...params).then((reply) => {
activeRequests--;
writeRequests--;
resolve(reply);
@ -106,6 +107,9 @@ if (config.redis?.enabled) {
reject(err);
});
});
exportClient.set = (key, value) => setFun(client.set.bind(client), [key, value]);
exportClient.setEx = (key, seconds, value) => setFun(client.setEx.bind(client), [key, seconds, value]);
exportClient.increment = (key) => new Promise((resolve, reject) =>
void client.multi()
.incr(key)