Skip to content

Commit cff2325

Browse files
committed
use keep alive in disk cache and user counter
1 parent d7d5618 commit cff2325

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/middleware/userCounter.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ import { Logger } from "../utils/logger";
33
import { config } from "../config";
44
import { getIP } from "../utils/getIP";
55
import { NextFunction, Request, Response } from "express";
6+
import { Agent } from "http";
7+
8+
const httpAgent = new Agent({ keepAlive: true });
69

710
export function userCounter(req: Request, res: Response, next: NextFunction): void {
811
if (req.method !== "OPTIONS") {
912
if (Math.random() < 1 / config.userCounterRatio) {
10-
axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req)}`)
11-
.catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
13+
axios({
14+
method: "post",
15+
url: `${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req)}`,
16+
httpAgent
17+
}).catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
1218
}
1319
}
1420

src/utils/diskCache.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import axios, { AxiosError } from "axios";
2+
import { Agent } from "http";
23
import { config } from "../config";
34
import { Logger } from "./logger";
45

6+
const httpAgent = new Agent({ keepAlive: true });
7+
58
class DiskCache {
69
async set(key: string, value: unknown): Promise<boolean> {
710
if (!config.diskCacheURL) return false;
811

912
try {
10-
const result = await axios.post(`${config.diskCacheURL}/api/v1/item`, {
11-
key,
12-
value
13+
const result = await axios({
14+
method: "post",
15+
url: `${config.diskCacheURL}/api/v1/item`,
16+
data: {
17+
key,
18+
value
19+
},
20+
httpAgent
1321
});
1422

1523
return result.status === 200;

0 commit comments

Comments
 (0)