Stop writing if response time exceeds value

This commit is contained in:
Ajay 2022-11-19 23:58:55 -05:00
parent 9bd6e9df4f
commit 238ccc83d9
3 changed files with 14 additions and 2 deletions

View file

@ -141,6 +141,8 @@ addDefaults(config, {
getTimeout: 40, getTimeout: 40,
maxConnections: 15000, maxConnections: 15000,
maxWriteConnections: 1000, maxWriteConnections: 1000,
stopWritingAfterResponseTime: 20,
responseTimePause: 1000,
disableHashCache: false disableHashCache: false
}, },
redisRead: { redisRead: {

View file

@ -7,6 +7,8 @@ interface RedisConfig extends redis.RedisClientOptions {
getTimeout: number; getTimeout: number;
maxConnections: number; maxConnections: number;
maxWriteConnections: number; maxWriteConnections: number;
stopWritingAfterResponseTime: number;
responseTimePause: number;
disableHashCache: boolean; disableHashCache: boolean;
} }

View file

@ -39,6 +39,7 @@ let writeRequests = 0;
const readResponseTime: number[] = []; const readResponseTime: number[] = [];
const writeResponseTime: number[] = []; const writeResponseTime: number[] = [];
let lastResponseTimeLimit = 0;
const maxStoredTimes = 200; const maxStoredTimes = 200;
if (config.redis?.enabled) { if (config.redis?.enabled) {
@ -69,8 +70,13 @@ if (config.redis?.enabled) {
activeRequests--; activeRequests--;
resolve(reply); resolve(reply);
readResponseTime.push(Date.now() - start); const responseTime = Date.now() - start;
readResponseTime.push(responseTime);
if (readResponseTime.length > maxStoredTimes) readResponseTime.shift(); if (readResponseTime.length > maxStoredTimes) readResponseTime.shift();
if (config.redis.stopWritingAfterResponseTime
&& responseTime > config.redis.stopWritingAfterResponseTime) {
lastResponseTimeLimit = Date.now();
}
}).catch((err) => { }).catch((err) => {
if (chosenGet === get) { if (chosenGet === get) {
lastClientFail = Date.now(); lastClientFail = Date.now();
@ -85,7 +91,9 @@ if (config.redis?.enabled) {
const setFun = <T extends Array<any>>(func: (...args: T) => Promise<string>, params: T): Promise<string> => const setFun = <T extends Array<any>>(func: (...args: T) => Promise<string>, params: T): Promise<string> =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
if (config.redis.maxWriteConnections && activeRequests > config.redis.maxWriteConnections) { if ((config.redis.maxWriteConnections && activeRequests > config.redis.maxWriteConnections)
|| (config.redis.responseTimePause
&& Date.now() - lastResponseTimeLimit < config.redis.responseTimePause)) {
reject("Too many active requests to write"); reject("Too many active requests to write");
return; return;
} }