Skip to content

Commit 9077813

Browse files
authored
Merge 9bd59f4 into fe60c58
2 parents fe60c58 + 9bd59f4 commit 9077813

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

TelegramSearchBot/Service/BotAPI/TelegramBotReceiverService.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
namespace TelegramSearchBot.Service.BotAPI {
1616
[Injectable(ServiceLifetime.Singleton)]
1717
public class TelegramBotReceiverService : BackgroundService {
18+
private const int MaxConcurrentUpdates = 8;
1819
private readonly ITelegramBotClient _botClient;
1920
private readonly IServiceProvider _serviceProvider;
2021
private readonly ILogger<TelegramBotReceiverService> _logger;
22+
private readonly SemaphoreSlim _updateProcessingSemaphore;
2123

2224
public TelegramBotReceiverService(
2325
ITelegramBotClient botClient,
@@ -26,6 +28,7 @@ public TelegramBotReceiverService(
2628
_botClient = botClient;
2729
_serviceProvider = serviceProvider;
2830
_logger = logger;
31+
_updateProcessingSemaphore = new SemaphoreSlim(MaxConcurrentUpdates, MaxConcurrentUpdates);
2932
}
3033

3134
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
@@ -45,14 +48,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
4548
}
4649
}
4750

48-
private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) {
49-
try {
50-
using var scope = _serviceProvider.CreateScope();
51-
var executor = new ControllerExecutor(scope.ServiceProvider.GetServices<IOnUpdate>());
52-
await executor.ExecuteControllers(update);
53-
} catch (Exception ex) {
54-
_logger.LogError(ex, "Error handling update {UpdateId}", update.Id);
51+
private Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) {
52+
var processingTask = ProcessUpdateAsync(update, cancellationToken);
53+
54+
if (cancellationToken.IsCancellationRequested) {
55+
return processingTask;
5556
}
57+
58+
_ = processingTask;
59+
return Task.CompletedTask;
5660
}
5761

5862
private Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) {
@@ -64,5 +68,23 @@ private Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
6468
_logger.LogError(errorMessage);
6569
return Task.CompletedTask;
6670
}
71+
72+
private async Task ProcessUpdateAsync(Update update, CancellationToken cancellationToken) {
73+
await _updateProcessingSemaphore.WaitAsync().ConfigureAwait(false);
74+
75+
try {
76+
if (cancellationToken.IsCancellationRequested) {
77+
_logger.LogDebug("Cancellation requested while handling update {UpdateId}; finishing in-flight processing before shutdown.", update.Id);
78+
}
79+
80+
using var scope = _serviceProvider.CreateScope();
81+
var executor = new ControllerExecutor(scope.ServiceProvider.GetServices<IOnUpdate>());
82+
await executor.ExecuteControllers(update).ConfigureAwait(false);
83+
} catch (Exception ex) {
84+
_logger.LogError(ex, "Error handling update {UpdateId}", update.Id);
85+
} finally {
86+
_updateProcessingSemaphore.Release();
87+
}
88+
}
6789
}
6890
}

0 commit comments

Comments
 (0)