From 8c49e60dec73fbc1d3cc18d9ee85e2eca6831c41 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 13 Dec 2019 22:45:10 +0600 Subject: [PATCH] fixes --- .../github/insanusmokrassar/SauceNaoAPI/additional/Utils.kt | 6 ++++-- .../SauceNaoAPI/utils/RequestQuotaManager.kt | 6 +++--- .../insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt | 4 ++-- .../insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/Utils.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/Utils.kt index 60060b1..7873582 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/Utils.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/Utils.kt @@ -1,9 +1,11 @@ package com.github.insanusmokrassar.SauceNaoAPI.additional +import com.soywiz.klock.TimeSpan + typealias AccountType = Int const val defaultAccountType: AccountType = 1 // "basic" typealias UserId = Int -const val SHORT_TIME_RECALCULATING_MILLIS = 30.0 * 1000 -const val LONG_TIME_RECALCULATING_MILLIS = 24.0 * 60 * 60 * 1000 +val SHORT_TIME_RECALCULATING_MILLIS = TimeSpan(30.0 * 1000) +val LONG_TIME_RECALCULATING_MILLIS = TimeSpan(24.0 * 60 * 60 * 1000) 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 8eeb60d..40641ed 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt @@ -43,11 +43,11 @@ class RequestQuotaManager ( shortQuota = min(newShortQuota, shortMaxQuota) when { - longQuota < 1 -> (timeManager.getMostOldestInLongPeriod() ?: DateTime.now()).unixMillisLong + LONG_TIME_RECALCULATING_MILLIS.toLong() - shortQuota < 1 -> (timeManager.getMostOldestInShortPeriod() ?: DateTime.now()).unixMillisLong + SHORT_TIME_RECALCULATING_MILLIS.toLong() + longQuota < 1 -> (timeManager.getMostOldestInLongPeriod() ?: DateTime.now()) + LONG_TIME_RECALCULATING_MILLIS + shortQuota < 1 -> (timeManager.getMostOldestInShortPeriod() ?: DateTime.now()) + SHORT_TIME_RECALCULATING_MILLIS else -> null } ?.also { - delay((it - DateTime.now().unixMillisLong)) + delay((it - DateTime.now()).millisecondsLong) shortQuota = max(shortQuota, 1) longQuota = max(longQuota, 1) } diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt index 10ecb48..88a1462 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt @@ -12,8 +12,8 @@ internal suspend fun calculateSleepTime( mostOldestInLongPeriodGetter: suspend () -> DateTime? ): DateTime? { return when { - header.longRemaining < 1 -> mostOldestInLongPeriodGetter() ?.plus(TimeSpan(LONG_TIME_RECALCULATING_MILLIS)) - header.shortRemaining < 1 -> mostOldestInShortPeriodGetter() ?.plus(TimeSpan(SHORT_TIME_RECALCULATING_MILLIS)) + header.longRemaining < 1 -> mostOldestInLongPeriodGetter() ?.plus(LONG_TIME_RECALCULATING_MILLIS) + header.shortRemaining < 1 -> mostOldestInShortPeriodGetter() ?.plus(SHORT_TIME_RECALCULATING_MILLIS) else -> null } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt index bb72d59..a3aa6e6 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt @@ -12,7 +12,7 @@ import kotlin.coroutines.Continuation import kotlin.coroutines.suspendCoroutine private fun MutableList.clearTooOldTimes(relatedTo: DateTime = DateTime.now()) { - val limitValue = relatedTo.minus(TimeSpan(LONG_TIME_RECALCULATING_MILLIS.toDouble())) + val limitValue = relatedTo - LONG_TIME_RECALCULATING_MILLIS removeAll { it < limitValue @@ -56,7 +56,7 @@ private data class TimeManagerMostOldestInShortGetter( val now = DateTime.now() - val limitTime = now.minus(TimeSpan(SHORT_TIME_RECALCULATING_MILLIS.toDouble())) + val limitTime = now - SHORT_TIME_RECALCULATING_MILLIS continuation.resumeWith( Result.success(