From 94c680e0360f03cacba62ec50bd84c1cb1185a51 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 24 Jan 2024 17:39:18 +0100 Subject: [PATCH] Timeout fixes - Fixing wrong timeout setter - removing timeout setter in "Prepare" method in client which overrides setting --- TelegramBotBase/Base/MessageClient.cs | 9 ++++----- .../Base/ThreadPoolMessageClient.cs | 4 +--- TelegramBotBase/Builder/BotBaseBuilder.cs | 20 +++++++++---------- .../Interfaces/INetworkingSelectionStage.cs | 10 +++++----- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/TelegramBotBase/Base/MessageClient.cs b/TelegramBotBase/Base/MessageClient.cs index 62c083d..6e4ae5c 100644 --- a/TelegramBotBase/Base/MessageClient.cs +++ b/TelegramBotBase/Base/MessageClient.cs @@ -3,13 +3,14 @@ using System.Collections.Generic; using System.ComponentModel; using System.Net; using System.Net.Http; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using Telegram.Bot; using Telegram.Bot.Exceptions; using Telegram.Bot.Polling; using Telegram.Bot.Types; -using TelegramBotBase.Interfaces; + namespace TelegramBotBase.Base; @@ -104,8 +105,6 @@ public class MessageClient receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; - TelegramClient.Timeout = new TimeSpan(0, 1, 0); - TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token); } @@ -116,13 +115,13 @@ public class MessageClient } - public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) + private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) { await OnMessageLoop(new UpdateResult(update, null)); } - public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, + private async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) { await OnReceiveError(new ErrorResult(exception)); diff --git a/TelegramBotBase/Base/ThreadPoolMessageClient.cs b/TelegramBotBase/Base/ThreadPoolMessageClient.cs index 7808850..2b7cd3e 100644 --- a/TelegramBotBase/Base/ThreadPoolMessageClient.cs +++ b/TelegramBotBase/Base/ThreadPoolMessageClient.cs @@ -76,9 +76,7 @@ public class ThreadPoolMessageClient : MessageClient receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads); - - TelegramClient.Timeout = new TimeSpan(0, 1, 0); - + TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions, _cancellationTokenSource.Token); } diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 4291063..8a971c3 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -214,14 +214,14 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, #region "Step 4 (Network Settings)" - public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false) + public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false, int timeoutInSeconds = 60) { var url = new Uri(proxyAddress); _client = new MessageClient(_apiKey, url) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds) }, }; _client.ThrowPendingUpdates = throwPendingUpdates; @@ -229,13 +229,13 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, } - public IBotCommandsStage NoProxy(bool throwPendingUpdates = false) + public IBotCommandsStage NoProxy(bool throwPendingUpdates = false, int timeoutInSeconds = 60) { _client = new MessageClient(_apiKey) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0) } }; _client.ThrowPendingUpdates = throwPendingUpdates; @@ -243,13 +243,13 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, } - public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false) + public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false, int timeoutInSeconds = 60) { _client = new MessageClient(_apiKey, tgclient) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0) } }; _client.ThrowPendingUpdates = throwPendingUpdates; @@ -257,26 +257,26 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, } - public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort, bool throwPendingUpdates = false) + public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort, bool throwPendingUpdates = false, int timeoutInSeconds = 60) { _client = new MessageClient(_apiKey, proxyHost, proxyPort) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0) } }; _client.ThrowPendingUpdates = throwPendingUpdates; return this; } - public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false) + public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false, int timeoutInSeconds = 60) { _client = new MessageClient(_apiKey, tgclient) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0) } }; _client.ThrowPendingUpdates = throwPendingUpdates; diff --git a/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs b/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs index cd17e9d..8ab58d4 100644 --- a/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs +++ b/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs @@ -11,14 +11,14 @@ public interface INetworkingSelectionStage /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false); + IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false, int timeoutInSeconds = 60); /// /// Do not choose a proxy as network configuration. /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage NoProxy(bool throwPendingUpdates = false); + IBotCommandsStage NoProxy(bool throwPendingUpdates = false, int timeoutInSeconds = 60); /// @@ -27,7 +27,7 @@ public interface INetworkingSelectionStage /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false); + IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false, int timeoutInSeconds = 60); /// @@ -37,7 +37,7 @@ public interface INetworkingSelectionStage /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false); + IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false, int timeoutInSeconds = 60); /// /// Uses a custom http client. @@ -45,5 +45,5 @@ public interface INetworkingSelectionStage /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false); + IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false, int timeoutInSeconds = 60); } \ No newline at end of file