mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-12 22:47:12 +03:00
Add avg read and write for redis and write requests
This commit is contained in:
@@ -2,7 +2,7 @@ import { db } from "../databases/databases";
|
|||||||
import { Logger } from "../utils/logger";
|
import { Logger } from "../utils/logger";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
import redis, { getRedisActiveRequests } from "../utils/redis";
|
import redis, { getRedisStats } from "../utils/redis";
|
||||||
import { promiseOrTimeout } from "../utils/promise";
|
import { promiseOrTimeout } from "../utils/promise";
|
||||||
import { Postgres } from "../databases/Postgres";
|
import { Postgres } from "../databases/Postgres";
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ export async function getStatus(req: Request, res: Response): Promise<Response>
|
|||||||
statusRequests,
|
statusRequests,
|
||||||
hostname: os.hostname(),
|
hostname: os.hostname(),
|
||||||
activePostgresRequests: (db as Postgres)?.activePostgresRequests,
|
activePostgresRequests: (db as Postgres)?.activePostgresRequests,
|
||||||
activeRedisRequests: getRedisActiveRequests(),
|
redisStats: getRedisStats(),
|
||||||
};
|
};
|
||||||
return value ? res.send(JSON.stringify(statusValues[value])) : res.send(statusValues);
|
return value ? res.send(JSON.stringify(statusValues[value])) : res.send(statusValues);
|
||||||
} catch (err) /* istanbul ignore next */ {
|
} catch (err) /* istanbul ignore next */ {
|
||||||
|
|||||||
@@ -5,6 +5,13 @@ import { RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply } fro
|
|||||||
import { RedisClientOptions } from "@redis/client/dist/lib/client";
|
import { RedisClientOptions } from "@redis/client/dist/lib/client";
|
||||||
import { RedisReply } from "rate-limit-redis";
|
import { RedisReply } from "rate-limit-redis";
|
||||||
|
|
||||||
|
export interface RedisStats {
|
||||||
|
activeRequests: number;
|
||||||
|
writeRequests: number;
|
||||||
|
avgReadTime: number;
|
||||||
|
avgWriteTime: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface RedisSB {
|
interface RedisSB {
|
||||||
get(key: RedisCommandArgument): Promise<string>;
|
get(key: RedisCommandArgument): Promise<string>;
|
||||||
set(key: RedisCommandArgument, value: RedisCommandArgument): Promise<string>;
|
set(key: RedisCommandArgument, value: RedisCommandArgument): Promise<string>;
|
||||||
@@ -28,6 +35,11 @@ let exportClient: RedisSB = {
|
|||||||
let lastClientFail = 0;
|
let lastClientFail = 0;
|
||||||
let lastReadFail = 0;
|
let lastReadFail = 0;
|
||||||
let activeRequests = 0;
|
let activeRequests = 0;
|
||||||
|
let writeRequests = 0;
|
||||||
|
|
||||||
|
const readResponseTime: number[] = [];
|
||||||
|
const writeResponseTime: number[] = [];
|
||||||
|
const maxStoredTimes = 200;
|
||||||
|
|
||||||
if (config.redis?.enabled) {
|
if (config.redis?.enabled) {
|
||||||
Logger.info("Connected to redis");
|
Logger.info("Connected to redis");
|
||||||
@@ -47,6 +59,7 @@ if (config.redis?.enabled) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const start = Date.now();
|
||||||
activeRequests++;
|
activeRequests++;
|
||||||
|
|
||||||
const timeout = config.redis.getTimeout ? setTimeout(() => reject(), config.redis.getTimeout) : null;
|
const timeout = config.redis.getTimeout ? setTimeout(() => reject(), config.redis.getTimeout) : null;
|
||||||
@@ -56,6 +69,9 @@ if (config.redis?.enabled) {
|
|||||||
|
|
||||||
activeRequests--;
|
activeRequests--;
|
||||||
resolve(reply);
|
resolve(reply);
|
||||||
|
|
||||||
|
readResponseTime.push(Date.now() - start);
|
||||||
|
if (readResponseTime.length > maxStoredTimes) readResponseTime.shift();
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
if (chosenGet === get) {
|
if (chosenGet === get) {
|
||||||
lastClientFail = Date.now();
|
lastClientFail = Date.now();
|
||||||
@@ -73,13 +89,20 @@ if (config.redis?.enabled) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const start = Date.now();
|
||||||
activeRequests++;
|
activeRequests++;
|
||||||
|
writeRequests++;
|
||||||
|
|
||||||
set(key, value).then((reply) => {
|
set(key, value).then((reply) => {
|
||||||
activeRequests--;
|
activeRequests--;
|
||||||
|
writeRequests--;
|
||||||
resolve(reply);
|
resolve(reply);
|
||||||
|
|
||||||
|
readResponseTime.push(Date.now() - start);
|
||||||
|
if (readResponseTime.length > maxStoredTimes) readResponseTime.shift();
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
activeRequests--;
|
activeRequests--;
|
||||||
|
writeRequests--;
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -119,8 +142,13 @@ function pickChoice<T>(client: T, readClient: T): T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRedisActiveRequests(): number {
|
export function getRedisStats(): RedisStats {
|
||||||
return activeRequests;
|
return {
|
||||||
|
activeRequests,
|
||||||
|
writeRequests,
|
||||||
|
avgReadTime: readResponseTime.length > 0 ? readResponseTime.reduce((a, b) => a + b, 0) / readResponseTime.length : 0,
|
||||||
|
avgWriteTime: writeResponseTime.length > 0 ? writeResponseTime.reduce((a, b) => a + b, 0) / writeResponseTime.length : 0,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default exportClient;
|
export default exportClient;
|
||||||
|
|||||||
Reference in New Issue
Block a user