diff options
author | Florian Dold <florian.dold@gmail.com> | 2020-08-20 16:27:20 +0530 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2020-08-20 16:27:20 +0530 |
commit | 421e613f92b80c81c856d6b074aa160e80e38e3d (patch) | |
tree | 7284e8b856986de3ee4ca1362fa9b52ce1e22de1 /packages/taler-wallet-core/src/util/RequestThrottler.ts | |
parent | ddf9171c5becb3bb1aebdd3e1a298644f62ed090 (diff) | |
download | wallet-core-421e613f92b80c81c856d6b074aa160e80e38e3d.tar.xz |
throttling diagnostics and request timeouts
Diffstat (limited to 'packages/taler-wallet-core/src/util/RequestThrottler.ts')
-rw-r--r-- | packages/taler-wallet-core/src/util/RequestThrottler.ts | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/packages/taler-wallet-core/src/util/RequestThrottler.ts b/packages/taler-wallet-core/src/util/RequestThrottler.ts index 3b8f22f58..b56f7476a 100644 --- a/packages/taler-wallet-core/src/util/RequestThrottler.ts +++ b/packages/taler-wallet-core/src/util/RequestThrottler.ts @@ -30,25 +30,25 @@ const logger = new Logger("RequestThrottler.ts"); /** * Maximum request per second, per origin. */ -const MAX_PER_SECOND = 50; +const MAX_PER_SECOND = 100; /** * Maximum request per minute, per origin. */ -const MAX_PER_MINUTE = 100; +const MAX_PER_MINUTE = 500; /** * Maximum request per hour, per origin. */ -const MAX_PER_HOUR = 1000; +const MAX_PER_HOUR = 2000; /** * Throttling state for one origin. */ class OriginState { - private tokensSecond: number = MAX_PER_SECOND; - private tokensMinute: number = MAX_PER_MINUTE; - private tokensHour: number = MAX_PER_HOUR; + tokensSecond: number = MAX_PER_SECOND; + tokensMinute: number = MAX_PER_MINUTE; + tokensHour: number = MAX_PER_HOUR; private lastUpdate = getTimestampNow(); private refill(): void { @@ -57,6 +57,9 @@ class OriginState { if (d.d_ms === "forever") { throw Error("assertion failed"); } + if (d.d_ms < 0) { + return; + } const d_s = d.d_ms / 1000; this.tokensSecond = Math.min( MAX_PER_SECOND, @@ -129,4 +132,20 @@ export class RequestThrottler { const origin = new URL(requestUrl).origin; return this.getState(origin).applyThrottle(); } + + /** + * 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, + } + } } |