feat(api): add Riot rate limiter
This commit is contained in:
1 parent
78a8f136d0
commit
c07b040467
2 files changed
+264
No files matched your search
@@ -0,0 +1,65 @@
|
||||
import { GarnetService } from '../global/garnet/garnet.service';
|
||||
import { RiotRateLimiterService } from './riot-rate-limiter.service';
|
||||
|
||||
function makeService(sendCommand = jest.fn().mockResolvedValue(0)) {
|
||||
const garnet = {
|
||||
raw: jest.fn().mockReturnValue({ sendCommand }),
|
||||
} as unknown as GarnetService;
|
||||
return {
|
||||
service: new RiotRateLimiterService(garnet),
|
||||
sendCommand,
|
||||
};
|
||||
}
|
||||
|
||||
describe('RiotRateLimiterService background limiter', () => {
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('uses the background key namespace and passes cost to the Lua script', async () => {
|
||||
jest.spyOn(Date, 'now').mockReturnValue(1_000);
|
||||
const { service, sendCommand } = makeService();
|
||||
|
||||
await service.acquireBackground(8);
|
||||
|
||||
expect(sendCommand).toHaveBeenCalledTimes(1);
|
||||
const command = sendCommand.mock.calls[0][0] as string[];
|
||||
expect(command[0]).toBe('EVAL');
|
||||
expect(command[2]).toBe('2');
|
||||
expect(command[3]).toBe('riot:bg:rl:1000');
|
||||
expect(command[4]).toBe('riot:bg:rl:120000');
|
||||
expect(command[5]).toBe('1000');
|
||||
expect(command[7]).toBe('8');
|
||||
});
|
||||
|
||||
it('reads cleaned background usage for both windows', async () => {
|
||||
jest.spyOn(Date, 'now').mockReturnValue(200_000);
|
||||
const sendCommand = jest
|
||||
.fn()
|
||||
.mockImplementation(async (command: string[]) => {
|
||||
if (command[0] === 'ZREMRANGEBYSCORE') return 0;
|
||||
if (command[1] === 'riot:bg:rl:1000') return 3;
|
||||
if (command[1] === 'riot:bg:rl:120000') return 24;
|
||||
return 0;
|
||||
});
|
||||
const { service } = makeService(sendCommand);
|
||||
|
||||
await expect(service.getBackgroundUsage()).resolves.toEqual({
|
||||
used1s: 3,
|
||||
used2m: 24,
|
||||
});
|
||||
|
||||
expect(sendCommand).toHaveBeenCalledWith([
|
||||
'ZREMRANGEBYSCORE',
|
||||
'riot:bg:rl:1000',
|
||||
'-inf',
|
||||
'199000',
|
||||
]);
|
||||
expect(sendCommand).toHaveBeenCalledWith([
|
||||
'ZREMRANGEBYSCORE',
|
||||
'riot:bg:rl:120000',
|
||||
'-inf',
|
||||
'80000',
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,199 @@
|
||||
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));
|
||||
}
|
||||
Reference in new issue
Block a user