2021-05-03 19:29:22 +02:00
|
|
|
import {TooManyRequestsHttpError} from "./HttpError.js";
|
|
|
|
import {logger} from "./Logger.js";
|
2020-04-24 10:54:21 +02:00
|
|
|
|
|
|
|
export default class Throttler {
|
2020-09-25 23:42:15 +02:00
|
|
|
private static readonly throttles: Record<string, Throttle | undefined> = {};
|
2020-04-24 10:54:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Throttle function; will throw a TooManyRequestsHttpError when the threshold is reached.
|
|
|
|
*
|
|
|
|
* This throttle is adaptive: it will slowly decrease (linear) until it reaches 0 after {@param resetPeriod} ms.
|
|
|
|
* Threshold will hold for {@param holdPeriod} ms.
|
|
|
|
*
|
|
|
|
* @param action a unique action name (can be used multiple times, but it'll account for a single action).
|
|
|
|
* @param max how many times this action can be triggered per id.
|
|
|
|
* @param resetPeriod after how much time in ms the throttle will reach 0.
|
|
|
|
* @param id an identifier of who triggered the action.
|
|
|
|
* @param holdPeriod time in ms after each call before the threshold begins to decrease.
|
|
|
|
* @param jailPeriod time in ms for which the throttle will throw when it is triggered.
|
|
|
|
*/
|
2020-09-25 23:42:15 +02:00
|
|
|
public static throttle(
|
|
|
|
action: string,
|
|
|
|
max: number,
|
|
|
|
resetPeriod: number,
|
|
|
|
id: string,
|
|
|
|
holdPeriod: number = 100,
|
|
|
|
jailPeriod: number = 30 * 1000,
|
|
|
|
): void {
|
2020-04-24 10:54:21 +02:00
|
|
|
let throttle = this.throttles[action];
|
2020-11-15 15:16:18 +01:00
|
|
|
if (!throttle)
|
|
|
|
throttle = this.throttles[action] = new Throttle(action, max, resetPeriod, holdPeriod, jailPeriod);
|
2020-04-24 10:54:21 +02:00
|
|
|
throttle.trigger(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private constructor() {
|
2020-09-25 23:42:15 +02:00
|
|
|
// Disable constructor
|
2020-04-24 10:54:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Throttle {
|
2020-11-15 15:16:18 +01:00
|
|
|
private readonly jailName: string;
|
2020-04-24 10:54:21 +02:00
|
|
|
private readonly max: number;
|
|
|
|
private readonly resetPeriod: number;
|
|
|
|
private readonly holdPeriod: number;
|
|
|
|
private readonly jailPeriod: number;
|
2020-09-25 23:42:15 +02:00
|
|
|
private readonly triggers: Record<string, {
|
|
|
|
count: number,
|
|
|
|
lastTrigger?: number,
|
|
|
|
jailed?: number;
|
|
|
|
} | undefined> = {};
|
2020-04-24 10:54:21 +02:00
|
|
|
|
2020-11-15 15:16:18 +01:00
|
|
|
public constructor(jailName: string, max: number, resetPeriod: number, holdPeriod: number, jailPeriod: number) {
|
|
|
|
this.jailName = jailName;
|
2020-04-24 10:54:21 +02:00
|
|
|
this.max = max;
|
|
|
|
this.resetPeriod = resetPeriod;
|
|
|
|
this.holdPeriod = holdPeriod;
|
|
|
|
this.jailPeriod = jailPeriod;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public trigger(id: string) {
|
|
|
|
let trigger = this.triggers[id];
|
|
|
|
if (!trigger) trigger = this.triggers[id] = {count: 0};
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
const currentDate = new Date().getTime();
|
2020-04-24 10:54:21 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
if (trigger.jailed && currentDate - trigger.jailed < this.jailPeriod)
|
|
|
|
return this.throw(trigger.jailed + this.jailPeriod - currentDate);
|
2020-04-24 10:54:21 +02:00
|
|
|
|
|
|
|
if (trigger.lastTrigger) {
|
|
|
|
let timeDiff = currentDate - trigger.lastTrigger;
|
|
|
|
if (timeDiff > this.holdPeriod) {
|
|
|
|
timeDiff -= this.holdPeriod;
|
2020-07-28 11:17:28 +02:00
|
|
|
trigger.count = Math.floor(Math.min(trigger.count, (this.max + 1) * (1 - timeDiff / this.resetPeriod)));
|
2020-04-24 10:54:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trigger.count++;
|
|
|
|
trigger.lastTrigger = currentDate;
|
|
|
|
|
|
|
|
if (trigger.count > this.max) {
|
|
|
|
trigger.jailed = currentDate;
|
2020-11-15 15:16:18 +01:00
|
|
|
|
|
|
|
const unjailedIn = trigger.jailed + this.jailPeriod - currentDate;
|
2021-01-22 15:54:26 +01:00
|
|
|
logger.info(`Jail ${this.jailName} triggered by ${id} and will be unjailed in ${unjailedIn}ms.`);
|
2020-11-15 15:16:18 +01:00
|
|
|
|
|
|
|
return this.throw(unjailedIn);
|
2020-04-24 10:54:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:17:28 +02:00
|
|
|
protected throw(unjailedIn: number) {
|
2020-11-15 15:16:18 +01:00
|
|
|
throw new TooManyRequestsHttpError(unjailedIn, this.jailName);
|
2020-04-24 10:54:21 +02:00
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|