From 0c9358c1b2bd80e25940022e86bd8daef8184ad7 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 19 Dec 2019 20:42:49 +0100 Subject: new date format, replace checkable annotations with codecs --- src/util/RequestThrottler.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/util/RequestThrottler.ts') 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; } -- cgit v1.2.3