Timeout fixes

- Fixing wrong timeout setter
- removing timeout setter in "Prepare" method in client which overrides setting
This commit is contained in:
Florian Zevedei 2024-01-24 17:39:18 +01:00
parent dbdc40582a
commit 94c680e036
4 changed files with 20 additions and 23 deletions

View File

@ -3,13 +3,14 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Exceptions; using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling; using Telegram.Bot.Polling;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Base; namespace TelegramBotBase.Base;
@ -104,8 +105,6 @@ public class MessageClient
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
TelegramClient.Timeout = new TimeSpan(0, 1, 0);
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token); 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)); await OnMessageLoop(new UpdateResult(update, null));
} }
public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, private async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
await OnReceiveError(new ErrorResult(exception)); await OnReceiveError(new ErrorResult(exception));

View File

@ -76,9 +76,7 @@ public class ThreadPoolMessageClient : MessageClient
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads); ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads);
TelegramClient.Timeout = new TimeSpan(0, 1, 0);
TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions, _cancellationTokenSource.Token); TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions, _cancellationTokenSource.Token);
} }

View File

@ -214,14 +214,14 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
#region "Step 4 (Network Settings)" #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); var url = new Uri(proxyAddress);
_client = new MessageClient(_apiKey, url) _client = new MessageClient(_apiKey, url)
{ {
TelegramClient = TelegramClient =
{ {
Timeout = new TimeSpan(0, 1, 0) Timeout = TimeSpan.FromSeconds(timeoutInSeconds)
}, },
}; };
_client.ThrowPendingUpdates = throwPendingUpdates; _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) _client = new MessageClient(_apiKey)
{ {
TelegramClient = TelegramClient =
{ {
Timeout = new TimeSpan(0, 1, 0) Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0)
} }
}; };
_client.ThrowPendingUpdates = throwPendingUpdates; _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) _client = new MessageClient(_apiKey, tgclient)
{ {
TelegramClient = TelegramClient =
{ {
Timeout = new TimeSpan(0, 1, 0) Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0)
} }
}; };
_client.ThrowPendingUpdates = throwPendingUpdates; _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) _client = new MessageClient(_apiKey, proxyHost, proxyPort)
{ {
TelegramClient = TelegramClient =
{ {
Timeout = new TimeSpan(0, 1, 0) Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0)
} }
}; };
_client.ThrowPendingUpdates = throwPendingUpdates; _client.ThrowPendingUpdates = throwPendingUpdates;
return this; 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) _client = new MessageClient(_apiKey, tgclient)
{ {
TelegramClient = TelegramClient =
{ {
Timeout = new TimeSpan(0, 1, 0) Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0)
} }
}; };
_client.ThrowPendingUpdates = throwPendingUpdates; _client.ThrowPendingUpdates = throwPendingUpdates;

View File

@ -11,14 +11,14 @@ public interface INetworkingSelectionStage
/// <param name="proxyAddress"></param> /// <param name="proxyAddress"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param> /// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false); IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false, int timeoutInSeconds = 60);
/// <summary> /// <summary>
/// Do not choose a proxy as network configuration. /// Do not choose a proxy as network configuration.
/// </summary> /// </summary>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param> /// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage NoProxy(bool throwPendingUpdates = false); IBotCommandsStage NoProxy(bool throwPendingUpdates = false, int timeoutInSeconds = 60);
/// <summary> /// <summary>
@ -27,7 +27,7 @@ public interface INetworkingSelectionStage
/// <param name="client"></param> /// <param name="client"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param> /// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false); IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false, int timeoutInSeconds = 60);
/// <summary> /// <summary>
@ -37,7 +37,7 @@ public interface INetworkingSelectionStage
/// <param name="Port"></param> /// <param name="Port"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param> /// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false); IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false, int timeoutInSeconds = 60);
/// <summary> /// <summary>
/// Uses a custom http client. /// Uses a custom http client.
@ -45,5 +45,5 @@ public interface INetworkingSelectionStage
/// <param name="client"></param> /// <param name="client"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param> /// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false); IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false, int timeoutInSeconds = 60);
} }