From 9ae91b0bd4743120865e7550dd46e0d4a3b18045 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 12 Oct 2019 13:43:28 +0600 Subject: [PATCH 1/4] hotfix for exception related to too many requests --- .../SauceNaoAPI/SauceNaoAPI.kt | 4 +++ .../SauceNaoAPI/utils/RequestQuotaManager.kt | 32 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt index e8e6dd6..b414b13 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt @@ -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.SHORT_TIME_RECALCULATING_MILLIS +import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException import com.github.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer import com.github.insanusmokrassar.SauceNaoAPI.utils.* @@ -52,6 +53,9 @@ data class SauceNaoAPI( callback.resumeWith(Result.success(answer)) quotaManager.updateQuota(answer.header, timeManager) + } catch (e: TooManyRequestsException) { + quotaManager.happenTooManyRequests(timeManager) + requestsChannel.send(callback to requestBuilder) } catch (e: Exception) { try { callback.resumeWith(Result.failure(e)) 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 a94740b..43fca38 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt @@ -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( suspend { - longMaxQuota = header.longLimit - shortMaxQuota = header.shortLimit + longMaxQuota = newMaxLongQuota ?: longMaxQuota + shortMaxQuota = newMaxShortQuota ?: shortMaxQuota - longQuota = min(header.longLimit, header.longRemaining) - shortQuota = min(header.shortLimit, header.shortRemaining) + longQuota = min(newLongQuota, longMaxQuota) + shortQuota = min(newShortQuota, shortMaxQuota) when { 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() { return suspendCoroutine { lateinit var callback: suspend () -> Unit From bb20fd8d7c068bd4e06d049fab0cd8c4262f1046 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 12 Oct 2019 13:47:00 +0600 Subject: [PATCH 2/4] update build script and changelog --- CHANGELOG.md | 2 ++ build.gradle | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bf6b5c..f622c0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ * Now `SauceNaoAPI` working with synchronous queue * `SauceNaoAPI` now will wait for some time when one of limits will be achieved +### 0.4.2 + ### 0.4.1 Managers experiments and row format in answer * Add `TimeManager` - it will manage work with requests times diff --git a/build.gradle b/build.gradle index 0446708..0e78261 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -project.version = "0.4.1" +project.version = "0.4.2" project.group = "com.github.insanusmokrassar" buildscript { From 275686b3ada09f9b7317a8974d4facee5e975a80 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 12 Oct 2019 14:05:47 +0600 Subject: [PATCH 3/4] 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 } From 65bedf11b2402aaa653d10cd58c1e709946e69b3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 12 Oct 2019 14:07:48 +0600 Subject: [PATCH 4/4] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f622c0f..cc564ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ ### 0.4.2 +Hotfix for autostop for some time when there is no remaining quotas for requests + ### 0.4.1 Managers experiments and row format in answer * Add `TimeManager` - it will manage work with requests times