improvements and actualization

This commit is contained in:
2022-05-14 13:43:28 +06:00
parent 8543917da7
commit 0b79f27b38
12 changed files with 198 additions and 127 deletions

View File

@@ -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()
}
}