This commit is contained in:
InsanusMokrassar 2019-12-13 22:45:10 +06:00
parent 51957eb369
commit 8c49e60dec
4 changed files with 11 additions and 9 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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
}
}

View File

@ -12,7 +12,7 @@ import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine
private fun MutableList<DateTime>.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(