aboutsummaryrefslogtreecommitdiff
path: root/src/util/RequestThrottler.ts
diff options
context:
space:
mode:
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;
}