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 | |
parent | ddf9171c5becb3bb1aebdd3e1a298644f62ed090 (diff) | |
download | wallet-core-421e613f92b80c81c856d6b074aa160e80e38e3d.tar.xz |
throttling diagnostics and request timeouts
Diffstat (limited to 'packages/taler-wallet-core/src/util')
-rw-r--r-- | packages/taler-wallet-core/src/util/RequestThrottler.ts | 31 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/util/http.ts | 2 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/util/time.ts | 10 |
3 files changed, 37 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, + } + } } diff --git a/packages/taler-wallet-core/src/util/http.ts b/packages/taler-wallet-core/src/util/http.ts index 22566daac..44c01a4e5 100644 --- a/packages/taler-wallet-core/src/util/http.ts +++ b/packages/taler-wallet-core/src/util/http.ts @@ -26,6 +26,7 @@ import { Codec } from "./codec"; import { OperationFailedError, makeErrorDetails } from "../operations/errors"; import { TalerErrorCode } from "../TalerErrorCode"; import { Logger } from "./logging"; +import { Duration } from "./time"; const logger = new Logger("http.ts"); @@ -43,6 +44,7 @@ export interface HttpResponse { export interface HttpRequestOptions { headers?: { [name: string]: string }; + timeout?: Duration, } export enum HttpResponseStatus { diff --git a/packages/taler-wallet-core/src/util/time.ts b/packages/taler-wallet-core/src/util/time.ts index 5c2f49d12..ccd75e14b 100644 --- a/packages/taler-wallet-core/src/util/time.ts +++ b/packages/taler-wallet-core/src/util/time.ts @@ -95,6 +95,16 @@ export function durationMin(d1: Duration, d2: Duration): Duration { return { d_ms: Math.min(d1.d_ms, d2.d_ms) }; } +export function durationMax(d1: Duration, d2: Duration): Duration { + if (d1.d_ms === "forever") { + return { d_ms: "forever" }; + } + if (d2.d_ms === "forever") { + return { d_ms: "forever" }; + } + return { d_ms: Math.max(d1.d_ms, d2.d_ms) }; +} + export function timestampCmp(t1: Timestamp, t2: Timestamp): number { if (t1.t_ms === "never") { if (t2.t_ms === "never") { |