From f686be0271f2eb8d389ae7763db9b58de1012690 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 27 Dec 2022 22:22:03 +0600 Subject: [PATCH] Update ExceptionsOnlyLimiter.kt --- .../limiters/ExceptionsOnlyLimiter.kt | 43 +++---------------- 1 file changed, 6 insertions(+), 37 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/ExceptionsOnlyLimiter.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/ExceptionsOnlyLimiter.kt index 0ad5e2a14b..4547cb0f53 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/ExceptionsOnlyLimiter.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/ExceptionsOnlyLimiter.kt @@ -26,7 +26,6 @@ import kotlinx.coroutines.sync.withLock */ class ExceptionsOnlyLimiter( private val defaultTooManyRequestsDelay: MilliSeconds = 1000L, - private val requestKeyFactory: suspend (Request<*>) -> Any = { it::class } ) : RequestLimiter { /** * Should be used for all [mutexesMap] changes @@ -59,42 +58,12 @@ class ExceptionsOnlyLimiter( /** * Just call [block] */ - override suspend fun limit(block: suspend () -> T): T = block() - - /** - * Will take a key for [request] using [requestKeyFactory] and try to retrieve [Mutex] by that key. In case if [Mutex] - * presented it will wait while [Mutex] is locked. After that operations completed, method will call - * [limit] with [block] inside of [safely] and in case of exception will call internal [lock] method - */ - override suspend fun limit(request: Request, block: suspend () -> T): T { - val key = requestKeyFactory(request) - while (true) { - // do nothing, just wait for unlock in case when mutex is presented in mutexesMap - lockMutex.withLock { mutexesMap[key] } ?.takeIf { it.isLocked } ?.withLock { } - var throwable: Throwable? = null - val result = safely({ - throwable = when (it) { - is TooMuchRequestsException -> { - lock(key, it.retryAfter.leftToRetry) - it - } - is ClientRequestException -> { - if (it.response.status == HttpStatusCode.TooManyRequests) { - lock(key, defaultTooManyRequestsDelay) - } else { - throw it - } - it - } - else -> throw it - } - null - }) { - limit(block) - } - if (throwable == null) { - return result!! - } + override suspend fun limit(block: suspend () -> T): T { + try { + block() + } catch (e: TooMuchRequestsException) { + delay(e.retryAfter.leftToRetry) + limit(block) } } }