refactor: improve maintainability by reducing LOC/reuse
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
export function createRateLimiter(windowMs = 60_000, cleanupIntervalMs = 5 * 60_000) {
|
||||
const map = new Map<string, { count: number; resetAt: number }>();
|
||||
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const [key, entry] of map) {
|
||||
if (now > entry.resetAt) map.delete(key);
|
||||
}
|
||||
}, cleanupIntervalMs);
|
||||
|
||||
return function check(key: string, max: number): boolean {
|
||||
const now = Date.now();
|
||||
const entry = map.get(key);
|
||||
if (!entry || now > entry.resetAt) {
|
||||
map.set(key, { count: 1, resetAt: now + windowMs });
|
||||
return true;
|
||||
}
|
||||
entry.count++;
|
||||
return entry.count <= max;
|
||||
};
|
||||
}
|
||||
@@ -12,10 +12,7 @@ export function sparkline(values: number[], width = 120, height = 32, color = '#
|
||||
return `<svg width="${width}" height="${height}" class="inline-block" data-vals="${values.join(',')}" data-region="${region}"><polyline points="${points}" fill="none" stroke="${color}" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
|
||||
}
|
||||
|
||||
const REGION_COLORS: Record<string, string> = {
|
||||
'eu-central': '#3b82f6',
|
||||
'us-west': '#f59e0b',
|
||||
};
|
||||
import { REGION_COLORS } from "../../../shared/plans";
|
||||
|
||||
// Pick the best region: the one with the lowest avg latency across its last 3 pings.
|
||||
// Only considers regions that have at least one ping in the most recent 3 pings overall,
|
||||
|
||||
Reference in New Issue
Block a user