200 lines
5.1 KiB
TypeScript
200 lines
5.1 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 BACKGROUND_LIMITS = [
|
|
{ windowMs: 1_000, max: 6 },
|
|
{ windowMs: 120_000, max: 36 },
|
|
] as const;
|
|
const BACKGROUND_KEY_PREFIX = 'riot:bg:rl';
|
|
|
|
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,
|
|
): 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);
|
|
if (waitMs <= 0) return;
|
|
|
|
if (waitMs >= 100) {
|
|
this.logger.warn(
|
|
`Background Riot throttle waiting ${waitMs}ms (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;
|
|
}),
|
|
);
|
|
|
|
return {
|
|
used1s: usages[0] ?? 0,
|
|
used2m: usages[1] ?? 0,
|
|
};
|
|
}
|
|
|
|
private async checkAndAdd(
|
|
route: RiotRegionalRoute | RiotPlatformRoute,
|
|
now: number,
|
|
cost: number,
|
|
): Promise<number> {
|
|
const keys = APP_LIMITS.map(
|
|
(limit) => `riot:rl:${route}:${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 }>,
|
|
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;
|
|
}
|
|
}
|
|
|
|
function normalizeCost(cost: number): number {
|
|
if (!Number.isFinite(cost)) return 1;
|
|
return Math.max(1, Math.floor(cost));
|
|
}
|
|
|
|
function sleep(ms: number): Promise<void> {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|