2019-12-09 13:29:42 +01:00
|
|
|
/*
|
|
|
|
This file is part of GNU Taler
|
|
|
|
(C) 2019 GNUnet e.V.
|
|
|
|
|
|
|
|
GNU Taler is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
2022-03-08 19:19:29 +01:00
|
|
|
import { Logger } from "./logging.js";
|
2022-03-18 15:32:41 +01:00
|
|
|
import { AbsoluteTime } from "./time.js";
|
2022-03-08 19:19:29 +01:00
|
|
|
|
2019-12-09 13:29:42 +01:00
|
|
|
/**
|
|
|
|
* Implementation of token bucket throttling.
|
|
|
|
*/
|
|
|
|
|
2020-08-14 12:23:50 +02:00
|
|
|
const logger = new Logger("RequestThrottler.ts");
|
2019-12-09 13:29:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum request per second, per origin.
|
|
|
|
*/
|
2020-08-20 12:57:20 +02:00
|
|
|
const MAX_PER_SECOND = 100;
|
2019-12-09 13:29:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum request per minute, per origin.
|
|
|
|
*/
|
2020-08-20 12:57:20 +02:00
|
|
|
const MAX_PER_MINUTE = 500;
|
2019-12-09 13:29:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum request per hour, per origin.
|
|
|
|
*/
|
2020-08-20 12:57:20 +02:00
|
|
|
const MAX_PER_HOUR = 2000;
|
2019-12-09 13:29:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Throttling state for one origin.
|
|
|
|
*/
|
|
|
|
class OriginState {
|
2020-08-20 12:57:20 +02:00
|
|
|
tokensSecond: number = MAX_PER_SECOND;
|
|
|
|
tokensMinute: number = MAX_PER_MINUTE;
|
|
|
|
tokensHour: number = MAX_PER_HOUR;
|
2022-03-18 15:32:41 +01:00
|
|
|
private lastUpdate = AbsoluteTime.now();
|
2019-12-09 13:29:42 +01:00
|
|
|
|
|
|
|
private refill(): void {
|
2022-03-18 15:32:41 +01:00
|
|
|
const now = AbsoluteTime.now();
|
|
|
|
if (AbsoluteTime.cmp(now, this.lastUpdate) < 0) {
|
2020-09-04 12:47:32 +02:00
|
|
|
// Did the system time change?
|
|
|
|
this.lastUpdate = now;
|
|
|
|
return;
|
|
|
|
}
|
2022-03-18 15:32:41 +01:00
|
|
|
const d = AbsoluteTime.difference(now, this.lastUpdate);
|
2019-12-19 20:42:49 +01:00
|
|
|
if (d.d_ms === "forever") {
|
2020-03-30 12:39:32 +02:00
|
|
|
throw Error("assertion failed");
|
2019-12-19 20:42:49 +01:00
|
|
|
}
|
2020-03-30 12:39:32 +02:00
|
|
|
this.tokensSecond = Math.min(
|
|
|
|
MAX_PER_SECOND,
|
2020-09-04 12:47:32 +02:00
|
|
|
this.tokensSecond + d.d_ms / 1000,
|
2020-03-30 12:39:32 +02:00
|
|
|
);
|
|
|
|
this.tokensMinute = Math.min(
|
|
|
|
MAX_PER_MINUTE,
|
2020-09-04 12:47:32 +02:00
|
|
|
this.tokensMinute + d.d_ms / 1000 / 60,
|
2020-03-30 12:39:32 +02:00
|
|
|
);
|
|
|
|
this.tokensHour = Math.min(
|
|
|
|
MAX_PER_HOUR,
|
2020-09-04 12:47:32 +02:00
|
|
|
this.tokensHour + d.d_ms / 1000 / 60 / 60,
|
2020-03-30 12:39:32 +02:00
|
|
|
);
|
2019-12-09 13:29:42 +01:00
|
|
|
this.lastUpdate = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the request for this origin should be throttled.
|
|
|
|
* Otherwise, take a token out of the respective buckets.
|
|
|
|
*/
|
|
|
|
applyThrottle(): boolean {
|
|
|
|
this.refill();
|
|
|
|
if (this.tokensSecond < 1) {
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.warn("request throttled (per second limit exceeded)");
|
2019-12-09 13:29:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (this.tokensMinute < 1) {
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.warn("request throttled (per minute limit exceeded)");
|
2019-12-09 13:29:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (this.tokensHour < 1) {
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.warn("request throttled (per hour limit exceeded)");
|
2019-12-09 13:29:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
this.tokensSecond--;
|
|
|
|
this.tokensMinute--;
|
|
|
|
this.tokensHour--;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request throttler, used as a "last layer of defense" when some
|
|
|
|
* other part of the re-try logic is broken and we're sending too
|
|
|
|
* many requests to the same exchange/bank/merchant.
|
|
|
|
*/
|
|
|
|
export class RequestThrottler {
|
|
|
|
private perOriginInfo: { [origin: string]: OriginState } = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the throttling state for an origin, or
|
|
|
|
* initialize if no state is associated with the
|
|
|
|
* origin yet.
|
|
|
|
*/
|
|
|
|
private getState(origin: string): OriginState {
|
|
|
|
const s = this.perOriginInfo[origin];
|
|
|
|
if (s) {
|
|
|
|
return s;
|
|
|
|
}
|
2020-03-30 12:39:32 +02:00
|
|
|
const ns = (this.perOriginInfo[origin] = new OriginState());
|
2019-12-09 13:29:42 +01:00
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply throttling to a request.
|
2020-03-30 12:39:32 +02:00
|
|
|
*
|
2019-12-09 13:29:42 +01:00
|
|
|
* @returns whether the request should be throttled.
|
|
|
|
*/
|
|
|
|
applyThrottle(requestUrl: string): boolean {
|
|
|
|
const origin = new URL(requestUrl).origin;
|
|
|
|
return this.getState(origin).applyThrottle();
|
|
|
|
}
|
2020-08-20 12:57:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the throttle statistics for a particular URL.
|
|
|
|
*/
|
|
|
|
getThrottleStats(requestUrl: string): Record<string, unknown> {
|
|
|
|
const origin = new URL(requestUrl).origin;
|
|
|
|
const state = this.getState(origin);
|
|
|
|
return {
|
|
|
|
tokensHour: state.tokensHour,
|
|
|
|
tokensMinute: state.tokensMinute,
|
|
|
|
tokensSecond: state.tokensSecond,
|
|
|
|
maxTokensHour: MAX_PER_HOUR,
|
|
|
|
maxTokensMinute: MAX_PER_MINUTE,
|
|
|
|
maxTokensSecond: MAX_PER_SECOND,
|
2020-09-01 15:37:14 +02:00
|
|
|
};
|
2020-08-20 12:57:20 +02:00
|
|
|
}
|
2019-12-09 13:29:42 +01:00
|
|
|
}
|