summaryrefslogtreecommitdiff
path: root/src/util/RequestThrottler.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2019-12-19 20:42:49 +0100
committerFlorian Dold <florian.dold@gmail.com>2019-12-19 20:42:49 +0100
commit0c9358c1b2bd80e25940022e86bd8daef8184ad7 (patch)
treea8c8ca0134bd886d8151633aff4c85e9513ad32c /src/util/RequestThrottler.ts
parent49e3b3e5b9bbf1ce356ef68f301d50c689ceecb9 (diff)
downloadwallet-core-0c9358c1b2bd80e25940022e86bd8daef8184ad7.tar.gz
wallet-core-0c9358c1b2bd80e25940022e86bd8daef8184ad7.tar.bz2
wallet-core-0c9358c1b2bd80e25940022e86bd8daef8184ad7.zip
new date format, replace checkable annotations with codecs
Diffstat (limited to 'src/util/RequestThrottler.ts')
-rw-r--r--src/util/RequestThrottler.ts14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/util/RequestThrottler.ts b/src/util/RequestThrottler.ts
index 01695ec37..0566306de 100644
--- a/src/util/RequestThrottler.ts
+++ b/src/util/RequestThrottler.ts
@@ -21,7 +21,7 @@
/**
* Imports.
*/
-import { getTimestampNow, Timestamp } from "../types/walletTypes";
+import { getTimestampNow, Timestamp, timestampSubtractDuraction, timestampDifference } from "../util/time";
/**
* Maximum request per second, per origin.
@@ -50,10 +50,14 @@ class OriginState {
private refill(): void {
const now = getTimestampNow();
- const d = now.t_ms - this.lastUpdate.t_ms;
- this.tokensSecond = Math.min(MAX_PER_SECOND, this.tokensSecond + (d / 1000));
- this.tokensMinute = Math.min(MAX_PER_MINUTE, this.tokensMinute + (d / 1000 * 60));
- this.tokensHour = Math.min(MAX_PER_HOUR, this.tokensHour + (d / 1000 * 60 * 60));
+ const d = timestampDifference(now, this.lastUpdate);
+ if (d.d_ms === "forever") {
+ 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.lastUpdate = now;
}