Skip to content

Commit

Permalink
imrpove RateLimiter configuration a little + fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
vendelieu committed Oct 27, 2023
1 parent 07619f7 commit 82ce842
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package eu.vendeli.tgbot.types.internal.configuration

import eu.vendeli.tgbot.core.ClassManagerImpl
import eu.vendeli.tgbot.core.InputListenerMapImpl
import eu.vendeli.tgbot.core.TokenBucketLimiterImpl
import eu.vendeli.tgbot.interfaces.ClassManager
import eu.vendeli.tgbot.interfaces.InputListener

Expand All @@ -17,7 +16,7 @@ data class BotConfiguration(
var apiHost: String = "api.telegram.org",
var inputListener: InputListener = InputListenerMapImpl(),
var classManager: ClassManager = ClassManagerImpl(),
internal var rateLimiter: RateLimiterConfiguration = RateLimiterConfiguration(TokenBucketLimiterImpl),
internal var rateLimiter: RateLimiterConfiguration = RateLimiterConfiguration(),
internal var httpClient: HttpConfiguration = HttpConfiguration(),
internal var logging: LoggingConfiguration = LoggingConfiguration(),
internal var updatesListener: UpdatesListenerConfiguration = UpdatesListenerConfiguration(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package eu.vendeli.tgbot.types.internal.configuration

import eu.vendeli.tgbot.TelegramBot
import eu.vendeli.tgbot.api.message
import eu.vendeli.tgbot.core.TokenBucketLimiterImpl
import eu.vendeli.tgbot.interfaces.RateLimitMechanism

data class RateLimiterConfiguration(
var mechanism: RateLimitMechanism,
var limits: RateLimits = RateLimits(),
var mechanism: RateLimitMechanism = TokenBucketLimiterImpl,
) {
var exceededAction: suspend (Long, TelegramBot) -> Unit = { telegramId: Long, bot: TelegramBot ->
message("Request limit exceeded, try again later.")
Expand Down
8 changes: 3 additions & 5 deletions telegram-bot/src/test/kotlin/eu/vendeli/RateLimitingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ package eu.vendeli
import BotTestContext
import ch.qos.logback.classic.Level
import eu.vendeli.tgbot.TelegramBot
import eu.vendeli.tgbot.core.TokenBucketLimiterImpl
import eu.vendeli.tgbot.types.internal.configuration.RateLimits
import io.kotest.core.spec.IsolationMode
import io.kotest.matchers.shouldBe
import java.util.concurrent.atomic.AtomicInteger

class RateLimitingTest : BotTestContext(false, true) {
override fun isolationMode(): IsolationMode = IsolationMode.InstancePerTest

@BeforeAll
fun prepareBot() {
private fun prepareBot() {
bot = TelegramBot("not necessary") {
logging {
botLogLevel = Level.INFO
}
rateLimiter {
mechanism = TokenBucketLimiterImpl
limits = RateLimits(10000, 5)
}
}
}

@Test
suspend fun `test limit exceeding`() {
prepareBot()
val hitsCounter = AtomicInteger(0)
val loopCounter = AtomicInteger(0)

Expand All @@ -44,6 +41,7 @@ class RateLimitingTest : BotTestContext(false, true) {

@Test
suspend fun `test certain command limit exceeding`() {
prepareBot()
val messageHitsCounter = AtomicInteger(0)
val commandHitsCounter = AtomicInteger(0)
val loopsCounter = AtomicInteger(0)
Expand Down

0 comments on commit 82ce842

Please sign in to comment.