mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2025-10-11 17:50:09 +00:00
improvements and actualization
This commit is contained in:
@@ -9,13 +9,12 @@ import dev.inmo.saucenaoapi.models.LimitsState
|
||||
import com.soywiz.klock.DateTime
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
internal class RequestQuotaManager (
|
||||
scope: CoroutineScope
|
||||
) : SauceCloseable {
|
||||
) {
|
||||
private var longQuota = 1
|
||||
private var shortQuota = 1
|
||||
private var longMaxQuota = 1
|
||||
@@ -35,6 +34,10 @@ internal class RequestQuotaManager (
|
||||
for (callback in quotaActions) {
|
||||
callback()
|
||||
}
|
||||
}.also {
|
||||
it.invokeOnCompletion {
|
||||
quotaActions.close(it)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateQuota(
|
||||
@@ -83,21 +86,16 @@ internal class RequestQuotaManager (
|
||||
)
|
||||
|
||||
suspend fun getQuota() {
|
||||
return suspendCoroutine {
|
||||
lateinit var callback: suspend () -> Unit
|
||||
callback = suspend {
|
||||
if (longQuota > 0 && shortQuota > 0) {
|
||||
it.resumeWith(Result.success(Unit))
|
||||
} else {
|
||||
quotaActions.send(callback)
|
||||
}
|
||||
val job = Job()
|
||||
lateinit var callback: suspend () -> Unit
|
||||
callback = suspend {
|
||||
if (longQuota > 0 && shortQuota > 0) {
|
||||
job.complete()
|
||||
} else {
|
||||
quotaActions.send(callback)
|
||||
}
|
||||
quotaActions.trySend(callback)
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
quotaJob.cancel()
|
||||
quotaActions.close()
|
||||
quotaActions.trySend(callback)
|
||||
return job.join()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user