-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ExceptionsThrottlerTelegramBotMiddleware
- Loading branch information
1 parent
f552903
commit b8a6534
Showing
7 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...v/inmo/tgbotapi/bot/ktor/middlewares/builtins/ExceptionsThrottlerTelegramBotMiddleware.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package dev.inmo.tgbotapi.bot.ktor.middlewares.builtins | ||
|
||
import dev.inmo.tgbotapi.bot.ktor.middlewares.TelegramBotMiddleware | ||
import dev.inmo.tgbotapi.requests.abstracts.Request | ||
import korlibs.time.milliseconds | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlin.reflect.KClass | ||
import kotlin.time.Duration | ||
|
||
/** | ||
* @see invoke | ||
*/ | ||
object ExceptionsThrottlerTelegramBotMiddleware { | ||
const val id: String = "ExceptionsThrottlerTelegramBotMiddleware" | ||
|
||
/** | ||
* Creates [TelegramBotMiddleware] and configures it with next parameters: | ||
* | ||
* * [TelegramBotMiddleware.onRequestException] will throttle after exception if exception has happened before | ||
* * [TelegramBotMiddleware.onRequestReturnResult] will clear state of all exceptions happened with the [Request] if its | ||
* handling has been completed successfully | ||
*/ | ||
operator fun invoke( | ||
exceptionDurationMultiplier: Float = 2f, | ||
initialExceptionDuration: Duration = 125.milliseconds, | ||
): TelegramBotMiddleware = TelegramBotMiddleware.build { | ||
val exceptionsTimeouts = mutableMapOf<KClass<*>, Duration>() | ||
val latestExceptionsRequestsTypes = mutableMapOf<KClass<*>, MutableSet<KClass<*>>>() | ||
val mutex = Mutex() | ||
onRequestException = onRequestException@{ request, t -> | ||
t ?: return@onRequestException null | ||
val kclass = t::class | ||
val toSleep = mutex.withLock { | ||
val latestDuration = exceptionsTimeouts[kclass] | ||
exceptionsTimeouts[kclass] = latestDuration ?.times(exceptionDurationMultiplier.toDouble()) ?: initialExceptionDuration | ||
latestExceptionsRequestsTypes.getOrPut(request::class) { mutableSetOf() }.add(kclass) | ||
latestDuration | ||
} | ||
toSleep ?.let { | ||
delay(it) | ||
} | ||
null | ||
} | ||
onRequestReturnResult = onRequestReturnResult@{ result, request, _ -> | ||
if (result.isSuccess) { | ||
mutex.withLock { | ||
val exceptionKClass = latestExceptionsRequestsTypes.remove(request::class) ?: return@withLock | ||
exceptionKClass.forEach { | ||
exceptionsTimeouts.remove(it) | ||
} | ||
} | ||
} | ||
null | ||
} | ||
id = ExceptionsThrottlerTelegramBotMiddleware.id | ||
} | ||
} |