From 7678be1e2419cb4eae517b183a703346836c2a28 Mon Sep 17 00:00:00 2001 From: Ajay Date: Fri, 13 Sep 2024 04:06:50 -0400 Subject: [PATCH] Add max redis response time for reads --- src/config.ts | 1 + src/types/config.model.ts | 1 + src/utils/redis.ts | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/src/config.ts b/src/config.ts index ce6265e..4ab6bc1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -165,6 +165,7 @@ addDefaults(config, { commandsQueueMaxLength: 3000, stopWritingAfterResponseTime: 50, responseTimePause: 1000, + maxReadResponseTime: 500, disableHashCache: false, clientCacheSize: 2000, useCompression: false, diff --git a/src/types/config.model.ts b/src/types/config.model.ts index 43041cc..98d6946 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -10,6 +10,7 @@ interface RedisConfig extends redis.RedisClientOptions { maxWriteConnections: number; stopWritingAfterResponseTime: number; responseTimePause: number; + maxReadResponseTime: number; disableHashCache: boolean; clientCacheSize: number; useCompression: boolean; diff --git a/src/utils/redis.ts b/src/utils/redis.ts index 05c1b99..33dc54b 100644 --- a/src/utils/redis.ts +++ b/src/utils/redis.ts @@ -222,6 +222,12 @@ if (config.redis?.enabled) { return; } + if (config.redis.maxReadResponseTime && activeRequests > maxStoredTimes + && readResponseTime[readResponseTime.length - 1] > config.redis.maxReadResponseTime) { + reject(new TooManyActiveConnectionsError(`Redis response time too high in general: ${readResponseTime[readResponseTime.length - 1]}ms with ${activeRequests} connections`)); + return; + } + // For tracking if (!shouldClientCacheKey(key)) memoryCacheUncachedMisses++;