From 323514519ab623b1cb875d9734088066287440f8 Mon Sep 17 00:00:00 2001 From: Shindou Mihou Date: Mon, 5 Aug 2024 09:56:57 +0800 Subject: [PATCH] feat(latte): remove semaphore wrapper --- .../beemo/latte/util/SuspendingRatelimit.kt | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/latte/src/main/java/gg/beemo/latte/util/SuspendingRatelimit.kt b/latte/src/main/java/gg/beemo/latte/util/SuspendingRatelimit.kt index 673a687..c521fe4 100644 --- a/latte/src/main/java/gg/beemo/latte/util/SuspendingRatelimit.kt +++ b/latte/src/main/java/gg/beemo/latte/util/SuspendingRatelimit.kt @@ -46,16 +46,15 @@ class SuspendingRatelimit(private val burst: Int, private val duration: Duration } } - suspend fun tryRequestQuota(): Pair = - quotaRequestSem.withPermit { - if (remainingQuota <= 0) { - val waitTime = calculateWaitTime() - return@withPermit false to waitTime - } - tryResetQuota() - - check(remainingQuota > 0) - remainingQuota-- - return@withPermit true to null + fun tryRequestQuota(): Pair { + if (remainingQuota <= 0) { + val waitTime = calculateWaitTime() + return false to waitTime } + tryResetQuota() + + check(remainingQuota > 0) + remainingQuota-- + return true to null + } }