.
This commit is contained in:
1 parent
5df3ea67e7
commit
3b3c564075
13 files changed
+821
-27
No files matched your search
@@ -7,21 +7,44 @@ 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 ACQUIRE_SCRIPT = `
|
||||
local now = tonumber(ARGV[1])
|
||||
local member = ARGV[2]
|
||||
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 = 3 + ((i - 1) * 2)
|
||||
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)
|
||||
if count >= limit then
|
||||
local oldest = redis.call('ZRANGE', key, 0, 0, 'WITHSCORES')
|
||||
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
|
||||
@@ -37,10 +60,13 @@ if maxWait > 0 then
|
||||
end
|
||||
|
||||
for i = 1, #KEYS do
|
||||
local argIndex = 3 + ((i - 1) * 2)
|
||||
local argIndex = 4 + ((i - 1) * 2)
|
||||
local windowMs = tonumber(ARGV[argIndex])
|
||||
local key = KEYS[i]
|
||||
redis.call('ZADD', key, now, member)
|
||||
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
|
||||
|
||||
@@ -58,7 +84,7 @@ export class RiotRateLimiterService {
|
||||
): Promise<void> {
|
||||
for (;;) {
|
||||
const now = Date.now();
|
||||
const waitMs = await this.checkAndAdd(route, now);
|
||||
const waitMs = await this.checkAndAdd(route, now, 1);
|
||||
if (waitMs <= 0) return;
|
||||
|
||||
if (waitMs >= 100) {
|
||||
@@ -70,22 +96,83 @@ export class RiotRateLimiterService {
|
||||
}
|
||||
}
|
||||
|
||||
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 client = this.garnet.raw();
|
||||
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,
|
||||
...APP_LIMITS.flatMap((limit) => [
|
||||
String(limit.windowMs),
|
||||
String(limit.max),
|
||||
]),
|
||||
String(normalizeCost(cost)),
|
||||
...limits.flatMap((limit) => [String(limit.windowMs), String(limit.max)]),
|
||||
];
|
||||
|
||||
const response = await client.sendCommand([
|
||||
@@ -102,6 +189,11 @@ export class RiotRateLimiterService {
|
||||
}
|
||||
}
|
||||
|
||||
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