From 4e3753d32ceac6045ee6bb4cf77a44430bc33345 Mon Sep 17 00:00:00 2001 From: Ajay Date: Mon, 5 Dec 2022 16:47:11 -0500 Subject: [PATCH] Only call user counter some of the time --- src/config.ts | 1 + src/middleware/userCounter.ts | 6 ++++-- src/types/config.model.ts | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index cb0bf34..e8ae606 100644 --- a/src/config.ts +++ b/src/config.ts @@ -65,6 +65,7 @@ addDefaults(config, { } }, userCounterURL: null, + userCounterRatio: 10, newLeafURLs: null, maxRewardTimePerSegmentInSeconds: 600, poiMinimumStartTime: 2, diff --git a/src/middleware/userCounter.ts b/src/middleware/userCounter.ts index 433af8e..dccd101 100644 --- a/src/middleware/userCounter.ts +++ b/src/middleware/userCounter.ts @@ -6,8 +6,10 @@ import { NextFunction, Request, Response } from "express"; export function userCounter(req: Request, res: Response, next: NextFunction): void { if (req.method !== "OPTIONS") { - axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req)}`) - .catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`)); + 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}`)); + } } next(); diff --git a/src/types/config.model.ts b/src/types/config.model.ts index 4adaa51..3f7e1ae 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -52,6 +52,7 @@ export interface SBSConfig { minReputationToSubmitChapter: number; minReputationToSubmitFiller: number; userCounterURL?: string; + userCounterRatio: number; proxySubmission?: string; behindProxy: string | boolean; db: string;