mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 01:02:30 +01:00
Add active connection limit to setEx as well
This commit is contained in:
parent
0ead3892ba
commit
90e68caaf7
1 changed files with 24 additions and 20 deletions
|
@ -51,7 +51,6 @@ if (config.redis?.enabled) {
|
||||||
|
|
||||||
|
|
||||||
const get = client.get.bind(client);
|
const get = client.get.bind(client);
|
||||||
const set = client.set.bind(client);
|
|
||||||
const getRead = readClient?.get?.bind(readClient);
|
const getRead = readClient?.get?.bind(readClient);
|
||||||
exportClient.get = (key) => new Promise((resolve, reject) => {
|
exportClient.get = (key) => new Promise((resolve, reject) => {
|
||||||
if (activeRequests > config.redis.maxConnections) {
|
if (activeRequests > config.redis.maxConnections) {
|
||||||
|
@ -83,29 +82,34 @@ if (config.redis?.enabled) {
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
exportClient.set = (key, value) => new Promise((resolve, reject) => {
|
|
||||||
if (activeRequests > config.redis.maxWriteConnections) {
|
|
||||||
reject("Too many active requests");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const start = Date.now();
|
const setFun = <T extends Array<any>>(func: (...args: T) => Promise<string> , params: T): Promise<string> =>
|
||||||
activeRequests++;
|
new Promise((resolve, reject) => {
|
||||||
writeRequests++;
|
if (activeRequests > config.redis.maxWriteConnections) {
|
||||||
|
reject("Too many active requests");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
set(key, value).then((reply) => {
|
const start = Date.now();
|
||||||
activeRequests--;
|
activeRequests++;
|
||||||
writeRequests--;
|
writeRequests++;
|
||||||
resolve(reply);
|
|
||||||
|
|
||||||
writeResponseTime.push(Date.now() - start);
|
func(...params).then((reply) => {
|
||||||
if (writeResponseTime.length > maxStoredTimes) writeResponseTime.shift();
|
activeRequests--;
|
||||||
}).catch((err) => {
|
writeRequests--;
|
||||||
activeRequests--;
|
resolve(reply);
|
||||||
writeRequests--;
|
|
||||||
reject(err);
|
writeResponseTime.push(Date.now() - start);
|
||||||
|
if (writeResponseTime.length > maxStoredTimes) writeResponseTime.shift();
|
||||||
|
}).catch((err) => {
|
||||||
|
activeRequests--;
|
||||||
|
writeRequests--;
|
||||||
|
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) =>
|
exportClient.increment = (key) => new Promise((resolve, reject) =>
|
||||||
void client.multi()
|
void client.multi()
|
||||||
.incr(key)
|
.incr(key)
|
||||||
|
|
Loading…
Reference in a new issue