Make redis readiness check recoverable

This commit is contained in:
Ajay
2024-04-19 21:20:40 -04:00
parent cfd7c3d8c4
commit 0f97ce4a49
2 changed files with 8 additions and 9 deletions

View File

@@ -1,19 +1,22 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import { Server } from "http"; import { Server } from "http";
import { config } from "../config"; import { config } from "../config";
import { getRedisActiveRequests, getRedisStats } from "../utils/redis"; import { getRedisStats } from "../utils/redis";
import { Postgres } from "../databases/Postgres"; import { Postgres } from "../databases/Postgres";
import { db } from "../databases/databases"; import { db } from "../databases/databases";
export async function getReady(req: Request, res: Response, server: Server): Promise<Response> { export async function getReady(req: Request, res: Response, server: Server): Promise<Response> {
const connections = await new Promise((resolve) => server.getConnections((_, count) => resolve(count))) as number; const connections = await new Promise((resolve) => server.getConnections((_, count) => resolve(count))) as number;
const redisStats = getRedisStats();
const postgresStats = (db as Postgres).getStats?.();
if (!connections if (!connections
|| (connections < config.maxConnections || (connections < config.maxConnections
&& (!config.redis || getRedisActiveRequests() < config.redis.maxConnections * 0.8) && (!config.redis || redisStats.activeRequests < config.redis.maxConnections * 0.8)
&& (!config.redis || getRedisStats().avgReadTime < 2000) && (!config.redis || redisStats.avgReadTime < 2000 || redisStats.activeRequests < 1)
&& (!config.postgres || (db as Postgres).getStats().activeRequests < config.postgres.maxActiveRequests * 0.8)) && (!config.postgres || postgresStats.activeRequests < config.postgres.maxActiveRequests * 0.8))
&& (!config.postgres || (db as Postgres).getStats().avgReadTime < 2000)) { && (!config.postgres || postgresStats.avgReadTime < 2000)) {
return res.sendStatus(200); return res.sendStatus(200);
} else { } else {
return res.sendStatus(500); return res.sendStatus(500);

View File

@@ -429,8 +429,4 @@ async function setupCacheClientTracking(client: RedisClientType,
await client.sendCommand(["CLIENT", "TRACKING", "ON", "REDIRECT", cacheConnectionClientId, "BCAST"]); await client.sendCommand(["CLIENT", "TRACKING", "ON", "REDIRECT", cacheConnectionClientId, "BCAST"]);
} }
export function getRedisActiveRequests() {
return activeRequests;
}
export default exportClient; export default exportClient;