From 4003fdbcd1dc5ba325723e3778e89e4961d68e07 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 Feb 2019 16:45:25 +0800 Subject: [PATCH] executeUnsafe to loop --- CHANGELOG.md | 2 ++ .../utils/extensions/RequestsExecutor.kt | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eaa5d4c2aa..a7aaf9caed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ ### 0.10.1 +* Change algorithm of `executeUnsafe`: now it use loop instead of recursive calling + ## 0.9.0 * Old extension `OkHttpClient.Builder#useWith` now deprecated and must be replaced by the same in diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt index 1c621db8b0..6f0c2e0767 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt @@ -173,14 +173,17 @@ suspend fun RequestsExecutor.executeUnsafe( retries: Int = 0, retriesDelay: Long = 1000L ): T? { - return try { - execute(request) - } catch (e: RequestException) { - if (retries > 0) { - delay(retriesDelay) - executeUnsafe(request, retries - 1, retriesDelay) - } else { - null + var leftRetries = retries + while(true) { + try { + return execute(request) + } catch (e: RequestException) { + if (leftRetries > 0) { + leftRetries-- + delay(retriesDelay) + } else { + return null + } } } }