aboutsummaryrefslogtreecommitdiff
path: root/src/util/RequestThrottler.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2020-03-30 16:09:32 +0530
committerFlorian Dold <florian.dold@gmail.com>2020-03-30 16:09:32 +0530
commitaaf950e2ad5c07d4423f9822e3a0ae9f7b8d2bdf (patch)
tree9274139660f30c4857d80044eb4ac283aac1775a /src/util/RequestThrottler.ts
parent15e18440dbad55df19977a2eb7053681259afc18 (diff)
downloadwallet-core-aaf950e2ad5c07d4423f9822e3a0ae9f7b8d2bdf.tar.xz
re-format with prettier v2, fix HTML
Diffstat (limited to 'src/util/RequestThrottler.ts')
-rw-r--r--src/util/RequestThrottler.ts29
1 files changed, 21 insertions, 8 deletions
diff --git a/src/util/RequestThrottler.ts b/src/util/RequestThrottler.ts
index 0566306de..c99c7e949 100644
--- a/src/util/RequestThrottler.ts
+++ b/src/util/RequestThrottler.ts
@@ -21,7 +21,12 @@
/**
* Imports.
*/
-import { getTimestampNow, Timestamp, timestampSubtractDuraction, timestampDifference } from "../util/time";
+import {
+ getTimestampNow,
+ Timestamp,
+ timestampSubtractDuraction,
+ timestampDifference,
+} from "../util/time";
/**
* Maximum request per second, per origin.
@@ -38,7 +43,6 @@ const MAX_PER_MINUTE = 100;
*/
const MAX_PER_HOUR = 1000;
-
/**
* Throttling state for one origin.
*/
@@ -52,12 +56,21 @@ class OriginState {
const now = getTimestampNow();
const d = timestampDifference(now, this.lastUpdate);
if (d.d_ms === "forever") {
- throw Error("assertion failed")
+ throw Error("assertion failed");
}
const d_s = d.d_ms / 1000;
- this.tokensSecond = Math.min(MAX_PER_SECOND, this.tokensSecond + (d_s / 1000));
- this.tokensMinute = Math.min(MAX_PER_MINUTE, this.tokensMinute + (d_s / 1000 * 60));
- this.tokensHour = Math.min(MAX_PER_HOUR, this.tokensHour + (d_s / 1000 * 60 * 60));
+ this.tokensSecond = Math.min(
+ MAX_PER_SECOND,
+ this.tokensSecond + d_s / 1000,
+ );
+ this.tokensMinute = Math.min(
+ MAX_PER_MINUTE,
+ this.tokensMinute + (d_s / 1000) * 60,
+ );
+ this.tokensHour = Math.min(
+ MAX_PER_HOUR,
+ this.tokensHour + (d_s / 1000) * 60 * 60,
+ );
this.lastUpdate = now;
}
@@ -104,13 +117,13 @@ export class RequestThrottler {
if (s) {
return s;
}
- const ns = this.perOriginInfo[origin] = new OriginState();
+ const ns = (this.perOriginInfo[origin] = new OriginState());
return ns;
}
/**
* Apply throttling to a request.
- *
+ *
* @returns whether the request should be throttled.
*/
applyThrottle(requestUrl: string): boolean {