mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 16:47:13 +00:00
PowLimiter and CommonLimiter rewriting (#210)
This commit is contained in:
parent
f8cccc3e17
commit
0d19952ba7
@ -7,6 +7,7 @@
|
||||
* `MicroUtils`: `0.4.0` -> `0.4.1`
|
||||
* `Core`:
|
||||
* `TelegramAPIUrlsKeeper` will fix ending of host url since this version
|
||||
* New mechanisms in`PowLimiter` and `CommonLimiter` has been added
|
||||
|
||||
## 0.30.6
|
||||
|
||||
|
@ -1,67 +1,43 @@
|
||||
package dev.inmo.tgbotapi.bot.settings.limiters
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import dev.inmo.micro_utils.coroutines.*
|
||||
import dev.inmo.tgbotapi.types.MilliSeconds
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.sync.Semaphore
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.math.roundToLong
|
||||
|
||||
private fun now(): Long = DateTime.nowUnixLong()
|
||||
|
||||
@Serializable
|
||||
class CommonLimiter(
|
||||
private val lockCount: Int = 10,
|
||||
private val regenTime: Long = 20 * 1000L // 20 seconds for full regen of opportunity to send message
|
||||
private val regenTime: MilliSeconds = 15 * 1000, // 15 seconds for full regen of opportunity to send message
|
||||
@Transient
|
||||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
) : RequestLimiter {
|
||||
private var doLimit: Boolean = false
|
||||
|
||||
private val counterChannel = Channel<Unit>(Channel.UNLIMITED)
|
||||
private val scope = CoroutineScope(Dispatchers.Default)
|
||||
private val counterJob = scope.launch {
|
||||
var wasLastSecond = 0
|
||||
var lastCountTime = now()
|
||||
var limitManagementJob: Job? = null
|
||||
var removeLimitTime: Long = lastCountTime
|
||||
for (counter in counterChannel) {
|
||||
val now = now()
|
||||
if (now - lastCountTime > 1000) {
|
||||
lastCountTime = now
|
||||
wasLastSecond = 1
|
||||
} else {
|
||||
wasLastSecond++
|
||||
}
|
||||
if (wasLastSecond >= lockCount) {
|
||||
removeLimitTime = now + regenTime
|
||||
if (limitManagementJob == null) {
|
||||
limitManagementJob = launch {
|
||||
doLimit = true
|
||||
var internalNow = now()
|
||||
while (internalNow < removeLimitTime) {
|
||||
delay(removeLimitTime - internalNow)
|
||||
internalNow = now()
|
||||
}
|
||||
doLimit = false
|
||||
}
|
||||
private val quotaSemaphore = Semaphore(lockCount)
|
||||
private val counterRegeneratorJob = scope.launch {
|
||||
val regenDelay: MilliSeconds = (regenTime.toDouble() / lockCount).roundToLong()
|
||||
while (isActive) {
|
||||
delay(regenDelay)
|
||||
if (quotaSemaphore.availablePermits < lockCount) {
|
||||
try {
|
||||
quotaSemaphore.release()
|
||||
} catch (_: IllegalStateException) {
|
||||
// Skip IllegalStateException due to the fact that this exception may happens in release method
|
||||
}
|
||||
}
|
||||
if (now > removeLimitTime) {
|
||||
limitManagementJob = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val quoterChannel = Channel<Unit>(Channel.CONFLATED)
|
||||
private val tickerJob = scope.launch {
|
||||
while (isActive) {
|
||||
quoterChannel.send(Unit)
|
||||
delay(1000L)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun <T> limit(block: suspend () -> T): T {
|
||||
counterChannel.send(Unit)
|
||||
return if (!doLimit) {
|
||||
block()
|
||||
} else {
|
||||
quoterChannel.receive()
|
||||
block()
|
||||
}
|
||||
quotaSemaphore.acquire()
|
||||
return block()
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package dev.inmo.tgbotapi.bot.settings.limiters
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.*
|
||||
import dev.inmo.tgbotapi.types.MilliSeconds
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.serialization.Serializable
|
||||
@ -9,62 +11,60 @@ import kotlin.math.pow
|
||||
|
||||
private sealed class RequestEvent
|
||||
private class AddRequest(
|
||||
val continuation: Continuation<Long>
|
||||
val continuation: Continuation<MilliSeconds>
|
||||
) : RequestEvent()
|
||||
private object CompleteRequest : RequestEvent()
|
||||
|
||||
@Serializable
|
||||
data class PowLimiter(
|
||||
private val minAwaitTime: Long = 0L,
|
||||
private val maxAwaitTime: Long = 10000L,
|
||||
private val minAwaitTime: MilliSeconds = 0L,
|
||||
private val maxAwaitTime: MilliSeconds = 10000L,
|
||||
private val powValue: Double = 4.0,
|
||||
private val powK: Double = 0.0016
|
||||
private val powK: Double = 1.6,
|
||||
@Transient
|
||||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
) : RequestLimiter {
|
||||
@Transient
|
||||
private val scope = CoroutineScope(Dispatchers.Default)
|
||||
@Transient
|
||||
private val eventsChannel = Channel<RequestEvent>(Channel.UNLIMITED)
|
||||
@Transient
|
||||
private val awaitTimeRange = minAwaitTime .. maxAwaitTime
|
||||
@Transient
|
||||
private val eventsChannel = let {
|
||||
var requestsInWork = 0.0
|
||||
scope.actor<RequestEvent> {
|
||||
when (it) {
|
||||
is AddRequest -> {
|
||||
val awaitTime = (requestsInWork.pow(powValue) * powK).toLong()
|
||||
requestsInWork++
|
||||
|
||||
init {
|
||||
scope.launch {
|
||||
var requestsInWork: Double = 0.0
|
||||
for (event in eventsChannel) {
|
||||
when (event) {
|
||||
is AddRequest -> {
|
||||
val awaitTime = (((requestsInWork.pow(powValue) * powK) * 1000L).toLong())
|
||||
requestsInWork++
|
||||
|
||||
event.continuation.resume(
|
||||
if (awaitTime in awaitTimeRange) {
|
||||
awaitTime
|
||||
} else {
|
||||
if (awaitTime < minAwaitTime) {
|
||||
minAwaitTime
|
||||
} else {
|
||||
maxAwaitTime
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
is CompleteRequest -> requestsInWork--
|
||||
it.continuation.resume(
|
||||
when {
|
||||
awaitTime in awaitTimeRange -> awaitTime
|
||||
awaitTime < awaitTimeRange.first -> awaitTimeRange.first
|
||||
else -> awaitTimeRange.last
|
||||
}
|
||||
)
|
||||
}
|
||||
is CompleteRequest -> requestsInWork--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun <T> limit(
|
||||
block: suspend () -> T
|
||||
private suspend inline fun <T> withDelay(
|
||||
crossinline block: suspend () -> T
|
||||
): T {
|
||||
val delayMillis = suspendCoroutine<Long> {
|
||||
scope.launch { eventsChannel.send(AddRequest(it)) }
|
||||
}
|
||||
delay(delayMillis)
|
||||
return try {
|
||||
block()
|
||||
safely { block() }
|
||||
} finally {
|
||||
eventsChannel.send(CompleteRequest)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun <T> limit(
|
||||
block: suspend () -> T
|
||||
): T {
|
||||
return withDelay(block)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user