mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2025-12-11 23:45:44 +00:00
improvements and actualization
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package dev.inmo.saucenaoapi.utils
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.utils.io.core.Input
|
||||
|
||||
expect class MPPFile
|
||||
|
||||
expect val MPPFile.input: Input
|
||||
expect val MPPFile.contentType: ContentType
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,13 @@ interface SauceCloseable {
|
||||
fun close()
|
||||
}
|
||||
|
||||
fun <T> SauceCloseable.use(block: (SauceCloseable) -> T): T = try {
|
||||
inline fun <T> SauceCloseable.use(block: (SauceCloseable) -> T): T = try {
|
||||
block(this)
|
||||
} finally {
|
||||
close()
|
||||
}
|
||||
|
||||
@Deprecated("Useless")
|
||||
suspend fun <T> SauceCloseable.useSafe(block: suspend (SauceCloseable) -> T): T = try {
|
||||
supervisorScope {
|
||||
block(this@useSafe)
|
||||
|
||||
@@ -3,11 +3,8 @@ package dev.inmo.saucenaoapi.utils
|
||||
import dev.inmo.saucenaoapi.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import dev.inmo.saucenaoapi.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import com.soywiz.klock.DateTime
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
private fun MutableList<DateTime>.clearTooOldTimes(relatedTo: DateTime = DateTime.now()) {
|
||||
val limitValue = relatedTo - LONG_TIME_RECALCULATING_MILLIS
|
||||
@@ -38,16 +35,16 @@ private data class TimeManagerTimeAdder(
|
||||
}
|
||||
|
||||
private data class TimeManagerMostOldestInLongGetter(
|
||||
private val continuation: Continuation<DateTime?>
|
||||
private val deferred: CompletableDeferred<DateTime?>
|
||||
) : TimeManagerAction {
|
||||
override suspend fun makeChangeWith(times: MutableList<DateTime>) {
|
||||
times.clearTooOldTimes()
|
||||
continuation.resumeWith(Result.success(times.minOrNull()))
|
||||
deferred.complete(times.minOrNull())
|
||||
}
|
||||
}
|
||||
|
||||
private data class TimeManagerMostOldestInShortGetter(
|
||||
private val continuation: Continuation<DateTime?>
|
||||
private val deferred: CompletableDeferred<DateTime?>
|
||||
) : TimeManagerAction {
|
||||
override suspend fun makeChangeWith(times: MutableList<DateTime>) {
|
||||
times.clearTooOldTimes()
|
||||
@@ -56,19 +53,17 @@ private data class TimeManagerMostOldestInShortGetter(
|
||||
|
||||
val limitTime = now - SHORT_TIME_RECALCULATING_MILLIS
|
||||
|
||||
continuation.resumeWith(
|
||||
Result.success(
|
||||
times.asSequence().filter {
|
||||
limitTime < it
|
||||
}.minOrNull()
|
||||
)
|
||||
deferred.complete(
|
||||
times.asSequence().filter {
|
||||
limitTime < it
|
||||
}.minOrNull()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class TimeManager(
|
||||
scope: CoroutineScope
|
||||
) : SauceCloseable {
|
||||
) {
|
||||
private val actionsChannel = Channel<TimeManagerAction>(Channel.UNLIMITED)
|
||||
|
||||
private val timeUpdateJob = scope.launch {
|
||||
@@ -76,6 +71,10 @@ internal class TimeManager(
|
||||
for (action in actionsChannel) {
|
||||
action(times)
|
||||
}
|
||||
}.also {
|
||||
it.invokeOnCompletion {
|
||||
actionsChannel.close(it)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addTimeAndClear() {
|
||||
@@ -83,21 +82,20 @@ internal class TimeManager(
|
||||
}
|
||||
|
||||
suspend fun getMostOldestInLongPeriod(): DateTime? {
|
||||
return suspendCoroutine {
|
||||
actionsChannel.trySend(
|
||||
TimeManagerMostOldestInLongGetter(it)
|
||||
)
|
||||
val deferred = CompletableDeferred<DateTime?>()
|
||||
return if (actionsChannel.trySend(TimeManagerMostOldestInLongGetter(deferred)).isSuccess) {
|
||||
deferred.await()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getMostOldestInShortPeriod(): DateTime? {
|
||||
return suspendCoroutine {
|
||||
actionsChannel.trySend(TimeManagerMostOldestInShortGetter(it))
|
||||
val deferred = CompletableDeferred<DateTime?>()
|
||||
return if (actionsChannel.trySend(TimeManagerMostOldestInShortGetter(deferred)).isSuccess) {
|
||||
deferred.await()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
actionsChannel.close()
|
||||
timeUpdateJob.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user