From d607d8b1796e3eb2b73a93ddfba666a3658e49ed Mon Sep 17 00:00:00 2001 From: Ajay Date: Mon, 15 Jan 2024 14:07:34 -0500 Subject: [PATCH] Don't fallback to db when too many redis connections --- src/utils/queryCacher.ts | 8 ++++++-- src/utils/redis.ts | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/utils/queryCacher.ts b/src/utils/queryCacher.ts index b1fbe46..4e7685c 100644 --- a/src/utils/queryCacher.ts +++ b/src/utils/queryCacher.ts @@ -1,4 +1,4 @@ -import redis from "../utils/redis"; +import redis, { TooManyActiveConnectionsError } from "../utils/redis"; import { Logger } from "../utils/logger"; import { skipSegmentsHashKey, skipSegmentsKey, reputationKey, ratingHashKey, skipSegmentGroupsKey, userFeatureKey, videoLabelsKey, videoLabelsHashKey, brandingHashKey, brandingKey } from "./redisKeys"; import { Service, VideoID, VideoIDHash } from "../types/segments.model"; @@ -13,7 +13,11 @@ async function get(fetchFromDB: () => Promise, key: string): Promise { return JSON.parse(reply); } - } catch (e) { } //eslint-disable-line no-empty + } catch (e) { + if (e instanceof TooManyActiveConnectionsError) { + throw e; + } + } const data = await fetchFromDB(); diff --git a/src/utils/redis.ts b/src/utils/redis.ts index 17dd3f7..c884a60 100644 --- a/src/utils/redis.ts +++ b/src/utils/redis.ts @@ -46,6 +46,8 @@ const writeResponseTime: number[] = []; let lastResponseTimeLimit = 0; const maxStoredTimes = 200; +export class TooManyActiveConnectionsError extends Error {} + export let connectionPromise = Promise.resolve(); if (config.redis?.enabled) { @@ -61,7 +63,7 @@ if (config.redis?.enabled) { const getRead = readClient?.get?.bind(readClient); exportClient.get = (key) => new Promise((resolve, reject) => { if (config.redis.maxConnections && activeRequests > config.redis.maxConnections) { - reject("Too many active requests in general"); + reject(new TooManyActiveConnectionsError("Too many active requests in general")); return; }