hotfix for exception related to too many requests

This commit is contained in:
InsanusMokrassar 2019-10-12 13:43:28 +06:00
parent f9a3176fec
commit 9ae91b0bd4
2 changed files with 31 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package com.github.insanusmokrassar.SauceNaoAPI
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException import com.github.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException
import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer
import com.github.insanusmokrassar.SauceNaoAPI.utils.* import com.github.insanusmokrassar.SauceNaoAPI.utils.*
@ -52,6 +53,9 @@ data class SauceNaoAPI(
callback.resumeWith(Result.success(answer)) callback.resumeWith(Result.success(answer))
quotaManager.updateQuota(answer.header, timeManager) quotaManager.updateQuota(answer.header, timeManager)
} catch (e: TooManyRequestsException) {
quotaManager.happenTooManyRequests(timeManager)
requestsChannel.send(callback to requestBuilder)
} catch (e: Exception) { } catch (e: Exception) {
try { try {
callback.resumeWith(Result.failure(e)) callback.resumeWith(Result.failure(e))

View File

@ -26,14 +26,20 @@ class RequestQuotaManager (
} }
} }
suspend fun updateQuota(header: Header, timeManager: TimeManager) { private suspend fun updateQuota(
newLongQuota: Int,
newShortQuota: Int,
newMaxLongQuota: Int?,
newMaxShortQuota: Int?,
timeManager: TimeManager
) {
quotaActions.send( quotaActions.send(
suspend { suspend {
longMaxQuota = header.longLimit longMaxQuota = newMaxLongQuota ?: longMaxQuota
shortMaxQuota = header.shortLimit shortMaxQuota = newMaxShortQuota ?: shortMaxQuota
longQuota = min(header.longLimit, header.longRemaining) longQuota = min(newLongQuota, longMaxQuota)
shortQuota = min(header.shortLimit, header.shortRemaining) shortQuota = min(newShortQuota, shortMaxQuota)
when { when {
shortQuota < 1 -> timeManager.getMostOldestInShortPeriod() ?.millis ?.plus(SHORT_TIME_RECALCULATING_MILLIS) ?: let { shortQuota < 1 -> timeManager.getMostOldestInShortPeriod() ?.millis ?.plus(SHORT_TIME_RECALCULATING_MILLIS) ?: let {
@ -53,6 +59,22 @@ class RequestQuotaManager (
) )
} }
suspend fun updateQuota(header: Header, timeManager: TimeManager) = updateQuota(
header.longRemaining,
header.shortRemaining,
header.longLimit,
header.shortLimit,
timeManager
)
suspend fun happenTooManyRequests(timeManager: TimeManager) = updateQuota(
1,
0,
null,
null,
timeManager
)
suspend fun getQuota() { suspend fun getQuota() {
return suspendCoroutine { return suspendCoroutine {
lateinit var callback: suspend () -> Unit lateinit var callback: suspend () -> Unit