fix: refine polling and settings layout

This commit is contained in:
2026-04-26 11:28:45 +08:00
parent 79047b5488
commit 85f24f8a84
3 changed files with 130 additions and 20 deletions
@@ -24,6 +24,8 @@ public sealed class LiveRoomPollingBackgroundService : BackgroundService
private static readonly TimeSpan OfflineGracefulStopTimeout = TimeSpan.FromSeconds(20);
private static readonly TimeSpan OfflineForcedStopTimeout = TimeSpan.FromSeconds(8);
private static readonly TimeSpan ExceptionEmailCooldown = TimeSpan.FromHours(6);
private static readonly TimeSpan PollDispatchSpacing = TimeSpan.FromMilliseconds(400);
private const int MaxConcurrentLiveRoomPolls = 2;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ILogger<LiveRoomPollingBackgroundService> _logger;
@@ -73,15 +75,7 @@ public sealed class LiveRoomPollingBackgroundService : BackgroundService
.Select(static item => item.Id)
.ToList();
foreach (var liveRoomId in liveRoomIds)
{
if (stoppingToken.IsCancellationRequested)
{
break;
}
await PollLiveRoomAsync(liveRoomId, settings, stoppingToken);
}
await PollLiveRoomsAsync(liveRoomIds, settings, stoppingToken);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
@@ -130,6 +124,52 @@ public sealed class LiveRoomPollingBackgroundService : BackgroundService
private static Task DelayAsync(int delaySeconds, CancellationToken cancellationToken) =>
Task.Delay(TimeSpan.FromSeconds(Math.Clamp(delaySeconds, 10, 3600)), cancellationToken);
private async Task PollLiveRoomsAsync(
IReadOnlyList<Guid> liveRoomIds,
SystemSettingsDto settings,
CancellationToken cancellationToken)
{
if (liveRoomIds.Count == 0)
{
return;
}
using var semaphore = new SemaphoreSlim(Math.Min(MaxConcurrentLiveRoomPolls, liveRoomIds.Count));
var tasks = new List<Task>(liveRoomIds.Count);
for (var index = 0; index < liveRoomIds.Count; index++)
{
cancellationToken.ThrowIfCancellationRequested();
await semaphore.WaitAsync(cancellationToken);
var liveRoomId = liveRoomIds[index];
tasks.Add(PollLiveRoomWithReleaseAsync(liveRoomId, settings, semaphore, cancellationToken));
if (index < liveRoomIds.Count - 1)
{
await Task.Delay(PollDispatchSpacing, cancellationToken);
}
}
await Task.WhenAll(tasks);
}
private async Task PollLiveRoomWithReleaseAsync(
Guid liveRoomId,
SystemSettingsDto settings,
SemaphoreSlim semaphore,
CancellationToken cancellationToken)
{
try
{
await PollLiveRoomAsync(liveRoomId, settings, cancellationToken);
}
finally
{
semaphore.Release();
}
}
private async Task PollLiveRoomAsync(Guid liveRoomId, SystemSettingsDto settings, CancellationToken cancellationToken)
{
using var scope = _serviceScopeFactory.CreateScope();