Improve ready check

This commit is contained in:
Ajay 2024-04-19 20:05:52 -04:00
parent 7c51586664
commit af7d8428ab
2 changed files with 12 additions and 1 deletions

View file

@ -1,11 +1,18 @@
import { Request, Response } from "express";
import { Server } from "http";
import { config } from "../config";
import { getRedisActiveRequests } from "../utils/redis";
import { Postgres } from "../databases/Postgres";
import { db } from "../databases/databases";
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;
if (!connections || connections < config.maxConnections) {
if (!connections
|| (connections < config.maxConnections
&& (!config.redis || getRedisActiveRequests() < config.redis.maxConnections * 0.8)
&& (!config.postgres || (db as Postgres).getStats().activeRequests < config.postgres.maxActiveRequests * 0.8))
&& (!config.postgres || (db as Postgres).getStats().avgReadTime < 5000)) {
return res.sendStatus(200);
} else {
return res.sendStatus(500);

View file

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