From cff2325aefc829adc52b37afc8945817e32a53c0 Mon Sep 17 00:00:00 2001 From: Ajay Date: Mon, 5 Dec 2022 17:06:00 -0500 Subject: [PATCH] use keep alive in disk cache and user counter --- src/middleware/userCounter.ts | 10 ++++++++-- src/utils/diskCache.ts | 14 +++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/middleware/userCounter.ts b/src/middleware/userCounter.ts index dccd101..dff3ab5 100644 --- a/src/middleware/userCounter.ts +++ b/src/middleware/userCounter.ts @@ -3,12 +3,18 @@ import { Logger } from "../utils/logger"; import { config } from "../config"; import { getIP } from "../utils/getIP"; import { NextFunction, Request, Response } from "express"; +import { Agent } from "http"; + +const httpAgent = new Agent({ keepAlive: true }); export function userCounter(req: Request, res: Response, next: NextFunction): void { if (req.method !== "OPTIONS") { if (Math.random() < 1 / config.userCounterRatio) { - axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req)}`) - .catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`)); + axios({ + method: "post", + url: `${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req)}`, + httpAgent + }).catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`)); } } diff --git a/src/utils/diskCache.ts b/src/utils/diskCache.ts index c03c750..798f4d9 100644 --- a/src/utils/diskCache.ts +++ b/src/utils/diskCache.ts @@ -1,15 +1,23 @@ import axios, { AxiosError } from "axios"; +import { Agent } from "http"; import { config } from "../config"; import { Logger } from "./logger"; +const httpAgent = new Agent({ keepAlive: true }); + class DiskCache { async set(key: string, value: unknown): Promise { if (!config.diskCacheURL) return false; try { - const result = await axios.post(`${config.diskCacheURL}/api/v1/item`, { - key, - value + const result = await axios({ + method: "post", + url: `${config.diskCacheURL}/api/v1/item`, + data: { + key, + value + }, + httpAgent }); return result.status === 200;