LimitsState

This commit is contained in:
InsanusMokrassar 2019-12-14 00:29:12 +06:00
parent 0c2c7e9e50
commit 541134d8a7
5 changed files with 30 additions and 7 deletions

View File

@ -17,6 +17,8 @@
* Uploading of file
* Updates of versions
* Now `SauceNaoAPI` do not require api key
* `SauceNaoAPI` instances now can return `limitsState` object, which will contains `LimitsState` with currently known
state of limits
### 0.4.3

View File

@ -2,11 +2,9 @@ package com.github.insanusmokrassar.SauceNaoAPI
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.models.SauceNaoAnswerSerializer
import com.github.insanusmokrassar.SauceNaoAPI.models.*
import com.github.insanusmokrassar.SauceNaoAPI.utils.*
import io.ktor.client.HttpClient
import io.ktor.client.call.UnsupportedContentTypeException
import io.ktor.client.call.call
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.features.ClientRequestException
@ -14,12 +12,12 @@ import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.client.response.readText
import io.ktor.http.*
import io.ktor.http.content.OutgoingContent
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.io.core.*
import kotlinx.serialization.json.Json
import java.util.logging.Logger
import kotlin.Result
import kotlin.coroutines.*
private const val API_TOKEN_FIELD = "api_key"
@ -48,6 +46,9 @@ data class SauceNaoAPI(
private val timeManager = TimeManager(scope)
private val quotaManager = RequestQuotaManager(scope)
val limitsState: LimitsState
get() = quotaManager.limitsState
private val requestsJob = scope.launch {
for ((callback, requestBuilder) in requestsChannel) {
quotaManager.getQuota()

View File

@ -0,0 +1,11 @@
package com.github.insanusmokrassar.SauceNaoAPI.models
import kotlinx.serialization.Serializable
@Serializable
data class LimitsState(
val maxShortQuota: Int,
val maxLongQuota: Int,
val knownShortQuota: Int,
val knownLongQuota: Int
)

View File

@ -5,6 +5,7 @@ import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATI
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsLongException
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
import com.github.insanusmokrassar.SauceNaoAPI.models.LimitsState
import com.soywiz.klock.DateTime
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
@ -13,14 +14,22 @@ import kotlin.coroutines.suspendCoroutine
import kotlin.math.max
import kotlin.math.min
class RequestQuotaManager (
private val scope: CoroutineScope
internal class RequestQuotaManager (
scope: CoroutineScope
) : Closeable {
private var longQuota = 1
private var shortQuota = 1
private var longMaxQuota = 1
private var shortMaxQuota = 1
val limitsState: LimitsState
get() = LimitsState(
shortMaxQuota,
longMaxQuota,
shortQuota,
longQuota
)
private val quotaActions = Channel<suspend () -> Unit>(Channel.UNLIMITED)
private val quotaJob = scope.launch {

View File

@ -68,7 +68,7 @@ private data class TimeManagerMostOldestInShortGetter(
}
}
class TimeManager(
internal class TimeManager(
scope: CoroutineScope
) : Closeable {
private val actionsChannel = Channel<TimeManagerAction>(Channel.UNLIMITED)