fix: unify riot refresh rate gating

This commit is contained in:
dahyeon committed 2026-06-07 11:11:12 -07:00
1 parent 25f3ccad57
commit de8bc87940
6 files changed
+359 -123

No files matched your search

@@ -7,11 +7,19 @@ const APP_LIMITS = [
{ windowMs: 120_000, max: 90 },
] as const;
const BACKGROUND_LIMITS = [
{ windowMs: 1_000, max: 6 },
{ windowMs: 120_000, max: 36 },
] as const;
const BACKGROUND_KEY_PREFIX = 'riot:bg:rl';
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])
@@ -81,85 +89,78 @@ export class RiotRateLimiterService {
public async acquire(
route: RiotRegionalRoute | RiotPlatformRoute,
cost = 1,
): Promise<void> {
for (;;) {
const now = Date.now();
const waitMs = await this.checkAndAdd(route, now, 1);
if (waitMs <= 0) return;
if (waitMs >= 100) {
this.logger.warn(
`Riot API throttle waiting ${waitMs}ms for route=${route}.`,
);
}
await sleep(waitMs);
}
}
public async acquireBackground(cost = 1): Promise<void> {
const normalizedCost = normalizeCost(cost);
for (;;) {
const now = Date.now();
const waitMs = await this.checkAndAddBackground(now, normalizedCost);
const waitMs = await this.checkAndAdd(now, normalizedCost);
if (waitMs <= 0) return;
if (waitMs >= 100) {
this.logger.warn(
`Background Riot throttle waiting ${waitMs}ms (cost=${normalizedCost}).`,
`Riot API throttle waiting ${waitMs}ms for route=${route} cost=${normalizedCost}.`,
);
}
await sleep(waitMs);
}
}
public async getBackgroundUsage(): Promise<{
used1s: number;
used2m: number;
}> {
const client = this.garnet.raw();
const now = Date.now();
const usages = await Promise.all(
BACKGROUND_LIMITS.map(async (limit) => {
const key = `${BACKGROUND_KEY_PREFIX}:${limit.windowMs}`;
await client.sendCommand([
'ZREMRANGEBYSCORE',
key,
'-inf',
String(now - limit.windowMs),
]);
const response = await client.sendCommand(['ZCARD', key]);
const count = Number(response ?? 0);
return Number.isFinite(count) && count > 0 ? count : 0;
}),
);
public async recordAppRateLimitObservation(
countHeader: string | null,
limitHeader?: string | null,
): Promise<void> {
const count120 = parseRateLimitHeader(countHeader, 120);
if (count120 === null) return;
return {
used1s: usages[0] ?? 0,
used2m: usages[1] ?? 0,
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,
);
}
private async checkAndAdd(
route: RiotRegionalRoute | RiotPlatformRoute,
now: number,
cost: number,
): Promise<number> {
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) => `riot:rl:${route}:${limit.windowMs}`,
(limit) => `${APP_KEY_PREFIX}:${limit.windowMs}`,
);
return this.checkAndAddWithLimits(keys, APP_LIMITS, now, cost);
}
private async checkAndAddBackground(
now: number,
cost: number,
): Promise<number> {
const keys = BACKGROUND_LIMITS.map(
(limit) => `${BACKGROUND_KEY_PREFIX}:${limit.windowMs}`,
);
return this.checkAndAddWithLimits(keys, BACKGROUND_LIMITS, now, cost);
}
private async checkAndAddWithLimits(
keys: string[],
limits: ReadonlyArray<{ windowMs: number; max: number }>,
@@ -187,6 +188,32 @@ export class RiotRateLimiterService {
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 {
@@ -194,6 +221,24 @@ function normalizeCost(cost: number): number {
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));
}