From 61e3652edf88d5761038b5f350aa68e151b42257 Mon Sep 17 00:00:00 2001 From: Xilosof Date: Sun, 14 Mar 2021 21:43:07 +0300 Subject: [PATCH 1/4] Add MaxNumberOfRetries setting. Indicates the maximum number of times a request that received error 429 will be sent again after a timeout until it receives code 200 or an error code not equal to 429. --- TelegramBotBase/Enums/eSettings.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/TelegramBotBase/Enums/eSettings.cs b/TelegramBotBase/Enums/eSettings.cs index e425650..7e55710 100644 --- a/TelegramBotBase/Enums/eSettings.cs +++ b/TelegramBotBase/Enums/eSettings.cs @@ -27,9 +27,14 @@ namespace TelegramBotBase.Enums /// /// Does stick to the console event handler and saves all sessions on exit. /// - SaveSessionsOnConsoleExit = 4 + SaveSessionsOnConsoleExit = 4, + /// + /// Indicates the maximum number of times a request that received error + /// 429 will be sent again after a timeout until it receives code 200 or an error code not equal to 429. + /// + MaxNumberOfRetries = 5, } } From 706b0f522a981aebb4486c956ae1c8e956c80f2e Mon Sep 17 00:00:00 2001 From: Xilosof Date: Sun, 14 Mar 2021 21:55:07 +0300 Subject: [PATCH 2/4] Add static propert MaxNumberOfRetries. This is necessary so that the DeviceSession instance can get this setting. There is no direct access to the settings in BotBase at the moment. This property will be set when calling the Start method for the bot and is the same for all bots in the same application. --- TelegramBotBase/Sessions/DeviceSession.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs index 7ce946f..e925cac 100644 --- a/TelegramBotBase/Sessions/DeviceSession.cs +++ b/TelegramBotBase/Sessions/DeviceSession.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; @@ -876,5 +876,15 @@ namespace TelegramBotBase.Sessions } #endregion + + #region "Static" + + /// + /// Indicates the maximum number of times a request that received error + /// 429 will be sent again after a timeout until it receives code 200 or an error code not equal to 429. + /// + public static uint MaxNumberOfRetries { get; set; } + + #endregion "Static" } } From e742ded371e378615fba10de8ecc8cf85b24b252 Mon Sep 17 00:00:00 2001 From: Xilosof Date: Sun, 14 Mar 2021 22:00:51 +0300 Subject: [PATCH 3/4] Change exception handling when sending a request. 1) Now RetryAfter is multiplied by 1000, because request contains value in second instead milliseconds. (see https://core.telegram.org/bots/api#responseparameters) 2) Now calls occur in a loop, but a limited number of times. This also ensures that exceptions are caught during a repeated request. --- TelegramBotBase/Sessions/DeviceSession.cs | 46 ++++++++++++++--------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs index e925cac..7ce93c1 100644 --- a/TelegramBotBase/Sessions/DeviceSession.cs +++ b/TelegramBotBase/Sessions/DeviceSession.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; @@ -770,20 +770,24 @@ namespace TelegramBotBase.Sessions /// public async Task API(Func> call) { - try + var numberOfTries = 0; + while (numberOfTries < DeviceSession.MaxNumberOfRetries) { - return await call(this.Client.TelegramClient); - } - catch (ApiRequestException ex) - { - if (ex.Parameters != null) + try { - await Task.Delay(ex.Parameters.RetryAfter); + return await call(Client.TelegramClient); + } + catch (ApiRequestException ex) + { + if (ex.ErrorCode != 429) + throw; - return await call(this.Client.TelegramClient); + if (ex.Parameters != null) + await Task.Delay(ex.Parameters.RetryAfter * 1000); + + numberOfTries++; } } - return default(T); } @@ -794,17 +798,23 @@ namespace TelegramBotBase.Sessions /// public async Task API(Func call) { - try + var numberOfTries = 0; + while (numberOfTries < DeviceSession.MaxNumberOfRetries) { - await call(this.Client.TelegramClient); - } - catch (ApiRequestException ex) - { - if (ex.Parameters != null) + try { - await Task.Delay(ex.Parameters.RetryAfter); + await call(Client.TelegramClient); + return; + } + catch (ApiRequestException ex) + { + if (ex.ErrorCode != 429) + throw; - await call(this.Client.TelegramClient); + if (ex.Parameters != null) + await Task.Delay(ex.Parameters.RetryAfter * 1000); + + numberOfTries++; } } } From 5dcbe04a730dbecd945b6599f06fba1dac2e5d18 Mon Sep 17 00:00:00 2001 From: Xilosof Date: Sun, 14 Mar 2021 22:03:13 +0300 Subject: [PATCH 4/4] Added initialization of MaxNumberOfRetries setting --- TelegramBotBase/BotBase.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 74f2e52..9d9c354 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -72,6 +72,7 @@ namespace TelegramBotBase { this.SystemSettings = new Dictionary(); + SetSetting(eSettings.MaxNumberOfRetries, 5); SetSetting(eSettings.NavigationMaximum, 10); SetSetting(eSettings.LogAllMessages, false); SetSetting(eSettings.SkipAllMessages, false); @@ -177,6 +178,8 @@ namespace TelegramBotBase }); } + DeviceSession.MaxNumberOfRetries = this.GetSetting(eSettings.MaxNumberOfRetries, 5); + this.Client.TelegramClient.StartReceiving(); }