mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2025-01-09 13:09:53 +00:00
commit
d5d3de9559
@ -12,6 +12,10 @@
|
|||||||
* Now `SauceNaoAPI` working with synchronous queue
|
* Now `SauceNaoAPI` working with synchronous queue
|
||||||
* `SauceNaoAPI` now will wait for some time when one of limits will be achieved
|
* `SauceNaoAPI` now will wait for some time when one of limits will be achieved
|
||||||
|
|
||||||
|
### 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
|
### 0.4.1 Managers experiments and row format in answer
|
||||||
|
|
||||||
* Add `TimeManager` - it will manage work with requests times
|
* Add `TimeManager` - it will manage work with requests times
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
project.version = "0.4.1"
|
project.version = "0.4.2"
|
||||||
project.group = "com.github.insanusmokrassar"
|
project.group = "com.github.insanusmokrassar"
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
|
@ -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))
|
||||||
|
@ -8,6 +8,7 @@ import kotlinx.coroutines.channels.Channel
|
|||||||
import kotlinx.io.core.Closeable
|
import kotlinx.io.core.Closeable
|
||||||
import org.joda.time.DateTime
|
import org.joda.time.DateTime
|
||||||
import kotlin.coroutines.suspendCoroutine
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
class RequestQuotaManager (
|
class RequestQuotaManager (
|
||||||
@ -26,33 +27,51 @@ 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 {
|
longQuota < 1 -> (timeManager.getMostOldestInLongPeriod() ?: DateTime.now()).millis + LONG_TIME_RECALCULATING_MILLIS
|
||||||
shortQuota = 1
|
shortQuota < 1 -> (timeManager.getMostOldestInShortPeriod() ?: DateTime.now()).millis + SHORT_TIME_RECALCULATING_MILLIS
|
||||||
null
|
|
||||||
}
|
|
||||||
longQuota < 1 -> timeManager.getMostOldestInLongPeriod() ?.millis ?.plus(LONG_TIME_RECALCULATING_MILLIS) ?: let {
|
|
||||||
longQuota = 1
|
|
||||||
null
|
|
||||||
}
|
|
||||||
else -> null
|
else -> null
|
||||||
} ?.let {
|
} ?.also {
|
||||||
delay(it - DateTime.now().millis)
|
delay(it - DateTime.now().millis)
|
||||||
|
shortQuota = max(shortQuota, 1)
|
||||||
|
longQuota = max(longQuota, 1)
|
||||||
}
|
}
|
||||||
Unit
|
Unit
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user