Skip to content

Commit 87e8d4d

Browse files
fix(backend): address review feedback on limiter and cache
1 parent 194a10e commit 87e8d4d

File tree

3 files changed

+9
-45
lines changed

3 files changed

+9
-45
lines changed

backend/src/dal/leaderboards.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ export async function get(
9494
}
9595
}
9696

97-
const CACHE_TTL_MS = 10 * 60 * 1000;
98-
const cachedCounts = new Map<string, { count: number; expiresAt: number }>();
97+
const cachedCounts = new Map<string, number>();
9998

10099
export async function getCount(
101100
mode: string,
@@ -104,21 +103,16 @@ export async function getCount(
104103
uid?: string,
105104
): Promise<number> {
106105
const key = `${language}_${mode}_${mode2}`;
107-
const cached = cachedCounts.get(key);
108-
if (
109-
uid === undefined &&
110-
cached !== undefined &&
111-
cached.expiresAt > Date.now()
112-
) {
113-
return cached.count;
106+
if (uid === undefined && cachedCounts.has(key)) {
107+
return cachedCounts.get(key) as number;
114108
} else {
115109
if (uid === undefined) {
116110
const count = await getCollection({
117111
language,
118112
mode,
119113
mode2,
120114
}).estimatedDocumentCount();
121-
cachedCounts.set(key, { count, expiresAt: Date.now() + CACHE_TTL_MS });
115+
cachedCounts.set(key, count);
122116
return count;
123117
} else {
124118
const result = await aggregateWithAcceptedConnections<{

backend/src/middlewares/rate-limit.ts

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import MonkeyError from "../utils/error";
22
import type { Response, NextFunction, Request } from "express";
3-
import {
4-
RateLimiterMemory,
5-
RateLimiterRedis,
6-
type RateLimiterAbstract,
7-
} from "rate-limiter-flexible";
3+
import { RateLimiterMemory } from "rate-limiter-flexible";
84
import {
95
rateLimit,
106
RateLimitRequestHandler,
@@ -25,7 +21,6 @@ import {
2521
TsRestRequestWithContext,
2622
} from "../api/types";
2723
import { AppRoute, AppRouter } from "@ts-rest/core";
28-
import * as RedisClient from "../init/redis";
2924

3025
export const REQUEST_MULTIPLIER = isDevEnvironment() ? 100 : 1;
3126

@@ -154,31 +149,10 @@ export const rootRateLimiter = rateLimit({
154149
});
155150

156151
// Bad Authentication Rate Limiter
157-
// Uses Redis when available for consistency across instances, falls back to memory
158-
function createBadAuthRateLimiter(): RateLimiterAbstract {
159-
const opts = {
160-
points: 30 * REQUEST_MULTIPLIER,
161-
duration: 60 * 60,
162-
};
163-
164-
const redisConnection = RedisClient.getConnection();
165-
if (redisConnection !== null) {
166-
return new RateLimiterRedis({
167-
...opts,
168-
storeClient: redisConnection,
169-
keyPrefix: "bad_auth",
170-
insuranceLimiter: new RateLimiterMemory(opts),
171-
});
172-
}
173-
174-
return new RateLimiterMemory(opts);
175-
}
176-
177-
let badAuthRateLimiter: RateLimiterAbstract = createBadAuthRateLimiter();
178-
179-
export function reinitBadAuthRateLimiter(): void {
180-
badAuthRateLimiter = createBadAuthRateLimiter();
181-
}
152+
const badAuthRateLimiter = new RateLimiterMemory({
153+
points: 30 * REQUEST_MULTIPLIER,
154+
duration: 60 * 60, //one hour seconds
155+
});
182156

183157
export async function badAuthRateLimiterHandler(
184158
req: ExpressRequestWithContext,

backend/src/server.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { createIndicies as leaderboardDbSetup } from "./dal/leaderboards";
1919
import { createIndicies as blocklistDbSetup } from "./dal/blocklist";
2020
import { createIndicies as connectionsDbSetup } from "./dal/connections";
2121
import { getErrorMessage } from "./utils/error";
22-
import { reinitBadAuthRateLimiter } from "./middlewares/rate-limit";
2322

2423
async function bootServer(port: number): Promise<Server> {
2524
try {
@@ -47,9 +46,6 @@ async function bootServer(port: number): Promise<Server> {
4746
Logger.success("Connected to redis");
4847
const connection = RedisClient.getConnection();
4948

50-
Logger.info("Upgrading bad auth rate limiter to Redis...");
51-
reinitBadAuthRateLimiter();
52-
5349
Logger.info("Initializing queues...");
5450
queues.forEach((queue) => {
5551
queue.init(connection ?? undefined);

0 commit comments

Comments
 (0)