1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

Update ExceptionsOnlyLimiter.kt

This commit is contained in:
InsanusMokrassar 2022-12-27 22:25:26 +06:00 committed by GitHub
parent f686be0271
commit f9a9f958ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,34 +27,6 @@ import kotlinx.coroutines.sync.withLock
class ExceptionsOnlyLimiter( class ExceptionsOnlyLimiter(
private val defaultTooManyRequestsDelay: MilliSeconds = 1000L, private val defaultTooManyRequestsDelay: MilliSeconds = 1000L,
) : RequestLimiter { ) : RequestLimiter {
/**
* Should be used for all [mutexesMap] changes
*/
private val lockMutex = Mutex()
/**
* Contains [Mutex]es for [Any] keys. If [Mutex] is presented it means that [lock] function has been called and
* that mutex should be locked for some time
*/
private val mutexesMap = mutableMapOf<Any, Mutex>()
private suspend fun lock(
key: Any,
timeMillis: MilliSeconds
) {
val mutex = Mutex()
mutex.withLock {
safely {
lockMutex.withLock {
mutexesMap[key] = mutex
}
delay(timeMillis)
lockMutex.withLock {
mutexesMap.remove(key)
}
}
}
}
/** /**
* Just call [block] * Just call [block]
*/ */