1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 12:08:43 +00:00

add handling of 429 status

This commit is contained in:
InsanusMokrassar 2020-11-11 11:15:19 +06:00
parent 66b4d06064
commit d83e3eb10a
2 changed files with 40 additions and 19 deletions

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.bot.Ktor.base package dev.inmo.tgbotapi.bot.Ktor.base
import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.tgbotapi.bot.Ktor.KtorCallFactory import dev.inmo.tgbotapi.bot.Ktor.KtorCallFactory
import dev.inmo.tgbotapi.bot.exceptions.newRequestException import dev.inmo.tgbotapi.bot.exceptions.newRequestException
import dev.inmo.tgbotapi.requests.GetUpdates import dev.inmo.tgbotapi.requests.GetUpdates
@ -51,7 +52,8 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
val content = response.receive<String>() val content = response.receive<String>()
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content) val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
return (responseObject.result?.let { return safely {
(responseObject.result?.let {
jsonFormatter.decodeFromJsonElement(request.resultDeserializer, it) jsonFormatter.decodeFromJsonElement(request.resultDeserializer, it)
} ?: response.let { } ?: response.let {
throw newRequestException( throw newRequestException(
@ -62,6 +64,7 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
}) })
} }
} }
}
protected abstract fun <T : Any> prepareCallBody( protected abstract fun <T : Any> prepareCallBody(
client: HttpClient, client: HttpClient,

View File

@ -3,6 +3,8 @@ package dev.inmo.tgbotapi.bot.settings.limiters
import dev.inmo.micro_utils.coroutines.safely import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.tgbotapi.bot.exceptions.TooMuchRequestsException import dev.inmo.tgbotapi.bot.exceptions.TooMuchRequestsException
import dev.inmo.tgbotapi.types.RetryAfterError import dev.inmo.tgbotapi.types.RetryAfterError
import io.ktor.client.features.ClientRequestException
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
@ -16,7 +18,8 @@ object ExceptionsOnlyLimiter : RequestLimiter {
while (true) { while (true) {
lockState.first { !it } lockState.first { !it }
val result = safely({ val result = safely({
if (it is TooMuchRequestsException) { when (it) {
is TooMuchRequestsException -> {
try { try {
safely { safely {
lockState.emit(true) lockState.emit(true)
@ -26,9 +29,24 @@ object ExceptionsOnlyLimiter : RequestLimiter {
lockState.emit(false) lockState.emit(false)
} }
Result.failure(it) Result.failure(it)
}
is ClientRequestException -> {
if (it.response.status == HttpStatusCode.TooManyRequests) {
try {
safely {
lockState.emit(true)
delay(1000L)
}
} finally {
lockState.emit(false)
}
} else { } else {
throw it throw it
} }
Result.failure(it)
}
else -> throw it
}
}) { }) {
Result.success(block()) Result.success(block())
} }