mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-24 00:18:23 +03:00
use keep alive in disk cache and user counter
This commit is contained in:
@@ -3,12 +3,18 @@ import { Logger } from "../utils/logger";
|
|||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
import { getIP } from "../utils/getIP";
|
import { getIP } from "../utils/getIP";
|
||||||
import { NextFunction, Request, Response } from "express";
|
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 {
|
export function userCounter(req: Request, res: Response, next: NextFunction): void {
|
||||||
if (req.method !== "OPTIONS") {
|
if (req.method !== "OPTIONS") {
|
||||||
if (Math.random() < 1 / config.userCounterRatio) {
|
if (Math.random() < 1 / config.userCounterRatio) {
|
||||||
axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req)}`)
|
axios({
|
||||||
.catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
|
method: "post",
|
||||||
|
url: `${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req)}`,
|
||||||
|
httpAgent
|
||||||
|
}).catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
import axios, { AxiosError } from "axios";
|
import axios, { AxiosError } from "axios";
|
||||||
|
import { Agent } from "http";
|
||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
import { Logger } from "./logger";
|
import { Logger } from "./logger";
|
||||||
|
|
||||||
|
const httpAgent = new Agent({ keepAlive: true });
|
||||||
|
|
||||||
class DiskCache {
|
class DiskCache {
|
||||||
async set(key: string, value: unknown): Promise<boolean> {
|
async set(key: string, value: unknown): Promise<boolean> {
|
||||||
if (!config.diskCacheURL) return false;
|
if (!config.diskCacheURL) return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await axios.post(`${config.diskCacheURL}/api/v1/item`, {
|
const result = await axios({
|
||||||
key,
|
method: "post",
|
||||||
value
|
url: `${config.diskCacheURL}/api/v1/item`,
|
||||||
|
data: {
|
||||||
|
key,
|
||||||
|
value
|
||||||
|
},
|
||||||
|
httpAgent
|
||||||
});
|
});
|
||||||
|
|
||||||
return result.status === 200;
|
return result.status === 200;
|
||||||
|
|||||||
Reference in New Issue
Block a user