From 275686b3ada09f9b7317a8974d4facee5e975a80 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 12 Oct 2019 14:05:47 +0600 Subject: [PATCH] fix recover of short and long quota in quota manager --- .../SauceNaoAPI/utils/RequestQuotaManager.kt | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt index 43fca38..0449d44 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt @@ -8,6 +8,7 @@ import kotlinx.coroutines.channels.Channel import kotlinx.io.core.Closeable import org.joda.time.DateTime import kotlin.coroutines.suspendCoroutine +import kotlin.math.max import kotlin.math.min class RequestQuotaManager ( @@ -42,17 +43,13 @@ class RequestQuotaManager ( shortQuota = min(newShortQuota, shortMaxQuota) when { - shortQuota < 1 -> timeManager.getMostOldestInShortPeriod() ?.millis ?.plus(SHORT_TIME_RECALCULATING_MILLIS) ?: let { - shortQuota = 1 - null - } - longQuota < 1 -> timeManager.getMostOldestInLongPeriod() ?.millis ?.plus(LONG_TIME_RECALCULATING_MILLIS) ?: let { - longQuota = 1 - null - } + longQuota < 1 -> (timeManager.getMostOldestInLongPeriod() ?: DateTime.now()).millis + LONG_TIME_RECALCULATING_MILLIS + shortQuota < 1 -> (timeManager.getMostOldestInShortPeriod() ?: DateTime.now()).millis + SHORT_TIME_RECALCULATING_MILLIS else -> null - } ?.let { + } ?.also { delay(it - DateTime.now().millis) + shortQuota = max(shortQuota, 1) + longQuota = max(longQuota, 1) } Unit }