Files
lolympus-tournament-support/apps/api/src/modules/riot/riot-rate-limiter.service.ts
T

245 lines
6.5 KiB
TypeScript

import { Injectable, Logger } from '@nestjs/common';
import type { RiotPlatformRoute, RiotRegionalRoute } from './riot.types';
import { GarnetService } from '../global/garnet/garnet.service';
const APP_LIMITS = [
{ windowMs: 1_000, max: 15 },
{ windowMs: 120_000, max: 90 },
] as const;
const APP_KEY_PREFIX = 'riot:rl:app';
const APP_WINDOW_MS = 120_000;
const TAIL_MS = 60_000;
const APP_OBSERVED_KEY = 'riot:app:rl:observed';
const APP_OBSERVED_TTL_SECONDS = 300;
const DEFAULT_APP_LIMIT_120 = 100;
interface AppRateObservation {
count120: number;
limit120: number;
observedAt: number;
windowStartAt: number;
}
const ACQUIRE_SCRIPT = `
local now = tonumber(ARGV[1])
local memberBase = ARGV[2]
local cost = tonumber(ARGV[3])
if cost == nil or cost < 1 then
cost = 1
end
local maxWait = 0
local effectiveCosts = {}
for i = 1, #KEYS do
local argIndex = 4 + ((i - 1) * 2)
local windowMs = tonumber(ARGV[argIndex])
local limit = tonumber(ARGV[argIndex + 1])
local key = KEYS[i]
local effectiveCost = cost
if effectiveCost > limit then
effectiveCost = limit
end
effectiveCosts[i] = effectiveCost
redis.call('ZREMRANGEBYSCORE', key, '-inf', now - windowMs)
local count = redis.call('ZCARD', key)
local overflow = count + effectiveCost - limit
if overflow > 0 then
local oldest = redis.call(
'ZRANGE',
key,
overflow - 1,
overflow - 1,
'WITHSCORES'
)
local waitMs = windowMs
if oldest[2] then
waitMs = windowMs - (now - tonumber(oldest[2])) + 1
end
if waitMs > maxWait then
maxWait = waitMs
end
end
end
if maxWait > 0 then
return maxWait
end
for i = 1, #KEYS do
local argIndex = 4 + ((i - 1) * 2)
local windowMs = tonumber(ARGV[argIndex])
local key = KEYS[i]
local effectiveCost = effectiveCosts[i] or cost
for c = 1, effectiveCost do
redis.call('ZADD', key, now, memberBase .. ':' .. i .. ':' .. c)
end
redis.call('PEXPIRE', key, windowMs)
end
return 0
`;
@Injectable()
export class RiotRateLimiterService {
private readonly logger = new Logger(RiotRateLimiterService.name);
constructor(private readonly garnet: GarnetService) {}
public async acquire(
route: RiotRegionalRoute | RiotPlatformRoute,
cost = 1,
): Promise<void> {
const normalizedCost = normalizeCost(cost);
for (;;) {
const now = Date.now();
const waitMs = await this.checkAndAdd(now, normalizedCost);
if (waitMs <= 0) return;
if (waitMs >= 100) {
this.logger.warn(
`Riot API throttle waiting ${waitMs}ms for route=${route} cost=${normalizedCost}.`,
);
}
await sleep(waitMs);
}
}
public async recordAppRateLimitObservation(
countHeader: string | null,
limitHeader?: string | null,
): Promise<void> {
const count120 = parseRateLimitHeader(countHeader, 120);
if (count120 === null) return;
const limit120 =
parseRateLimitHeader(limitHeader ?? null, 120) ?? DEFAULT_APP_LIMIT_120;
const now = Date.now();
const previous = await this.getAppRateObservation();
const windowStartAt =
!previous ||
now - previous.observedAt >= APP_WINDOW_MS ||
count120 < previous.count120
? now
: previous.windowStartAt;
const observation: AppRateObservation = {
count120,
limit120,
observedAt: now,
windowStartAt,
};
// Concurrent responses can race here. This observation is advisory, so
// last-writer-wins is acceptable and self-corrects on the next Riot header.
await this.garnet.setString(
APP_OBSERVED_KEY,
JSON.stringify(observation),
APP_OBSERVED_TTL_SECONDS,
);
}
public async isRefreshWindowOpen(
usageRatio: number,
now = Date.now(),
): Promise<boolean> {
const observation = await this.getAppRateObservation();
if (!observation) return true;
if (now - observation.observedAt >= APP_WINDOW_MS) return true;
const ratio = Number.isFinite(usageRatio) ? usageRatio : 0;
const headroomOk = observation.count120 < observation.limit120 * ratio;
const inTail = now >= observation.windowStartAt + APP_WINDOW_MS - TAIL_MS;
return headroomOk && inTail;
}
private async checkAndAdd(now: number, cost: number): Promise<number> {
const keys = APP_LIMITS.map(
(limit) => `${APP_KEY_PREFIX}:${limit.windowMs}`,
);
return this.checkAndAddWithLimits(keys, APP_LIMITS, now, cost);
}
private async checkAndAddWithLimits(
keys: string[],
limits: ReadonlyArray<{ windowMs: number; max: number }>,
now: number,
cost: number,
): Promise<number> {
const client = this.garnet.raw();
const member = `${process.pid}:${now}:${Math.random().toString(36).slice(2)}`;
const argumentsList = [
String(now),
member,
String(normalizeCost(cost)),
...limits.flatMap((limit) => [String(limit.windowMs), String(limit.max)]),
];
const response = await client.sendCommand([
'EVAL',
ACQUIRE_SCRIPT,
String(keys.length),
...keys,
...argumentsList,
]);
const waitMs = Number(response ?? 0);
if (!Number.isFinite(waitMs) || waitMs < 0) return 0;
return waitMs;
}
private async getAppRateObservation(): Promise<AppRateObservation | null> {
const raw = await this.garnet.getString(APP_OBSERVED_KEY);
if (!raw) return null;
try {
const parsed = JSON.parse(raw) as Partial<AppRateObservation>;
if (
typeof parsed.count120 !== 'number' ||
typeof parsed.limit120 !== 'number' ||
typeof parsed.observedAt !== 'number' ||
typeof parsed.windowStartAt !== 'number'
) {
return null;
}
return {
count120: parsed.count120,
limit120: parsed.limit120,
observedAt: parsed.observedAt,
windowStartAt: parsed.windowStartAt,
};
} catch {
return null;
}
}
}
function normalizeCost(cost: number): number {
if (!Number.isFinite(cost)) return 1;
return Math.max(1, Math.floor(cost));
}
function parseRateLimitHeader(
header: string | null,
windowSeconds: number,
): number | null {
if (!header) return null;
for (const rawEntry of header.split(',')) {
const [rawCount, rawWindow] = rawEntry.trim().split(':');
const count = Number(rawCount);
const window = Number(rawWindow);
if (window === windowSeconds && Number.isFinite(count) && count >= 0) {
return count;
}
}
return null;
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}