From aa9d2f67682897cb4873c933b138a85d3739d048 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Sun, 23 Feb 2025 11:02:44 +0200 Subject: [PATCH] Implements debounced library scan progress updater --- ...bouncedMediaLibraryScanProgressNotifier.cs | 74 ++++++++++++------- .../Lumina.Infrastructure.csproj | 1 + 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/Lumina.Infrastructure/Core/MediaLibrary/Management/Scanning/Progress/DebouncedMediaLibraryScanProgressNotifier.cs b/src/Lumina.Infrastructure/Core/MediaLibrary/Management/Scanning/Progress/DebouncedMediaLibraryScanProgressNotifier.cs index 0ad19a8d..bc994143 100644 --- a/src/Lumina.Infrastructure/Core/MediaLibrary/Management/Scanning/Progress/DebouncedMediaLibraryScanProgressNotifier.cs +++ b/src/Lumina.Infrastructure/Core/MediaLibrary/Management/Scanning/Progress/DebouncedMediaLibraryScanProgressNotifier.cs @@ -1,4 +1,5 @@ #region ========================================================================= USING ===================================================================================== +using DebounceThrottle; using ErrorOr; using Lumina.Application.Common.Mapping.MediaLibrary.Management; using Lumina.Application.Core.MediaLibrary.Management.Progress; @@ -7,6 +8,8 @@ using Lumina.Domain.Common.Enums.MediaLibrary; using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Progress; using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects; using Microsoft.AspNetCore.SignalR; +using System; +using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks; #endregion @@ -20,6 +23,7 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca { private readonly IHubContext _hubContext; private readonly IMediaLibrariesScanProgressTracker _mediaLibrariesScanProgressTracker; + private readonly ConcurrentDictionary _debouncers = new(); /// /// Initializes a new instance of the class. @@ -39,18 +43,26 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca /// Cancellation token that can be used to stop the execution. public async Task SendLibraryProgressUpdateEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken) { - if (!cancellationToken.IsCancellationRequested) + if (cancellationToken.IsCancellationRequested) + return; + + // throttle network traffic to no more than 20 updates a second, any more are pointless from a progress report perspective + DebounceDispatcher debouncer = _debouncers.GetOrAdd(mediaLibraryScanCompositeId, _ => new DebounceDispatcher(TimeSpan.FromMilliseconds(50))); + await debouncer.DebounceAsync(async () => { + // the token might be canceled after initial check but before debounced execution starts + if (cancellationToken.IsCancellationRequested) + return; + ErrorOr progressResult = _mediaLibrariesScanProgressTracker.GetScanProgress(mediaLibraryScanCompositeId); + if (progressResult.IsError) + return; - if (!progressResult.IsError) - { - MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse(); - await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanProgressUpdateEvent", progress, cancellationToken).ConfigureAwait(false); - } - } + MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse(); + await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanProgressUpdateEvent", progress, cancellationToken).ConfigureAwait(false); + }, cancellationToken).ConfigureAwait(false); } - + /// /// Notifies the SignalR clients about the completion of a media library scan. /// @@ -58,15 +70,16 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca /// Cancellation token that can be used to stop the execution. public async Task SendLibraryScanFinishedEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken) { - if (!cancellationToken.IsCancellationRequested) - { - ErrorOr progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId); + if (cancellationToken.IsCancellationRequested) + return; + ErrorOr progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId); + + CleanupDebouncer(mediaLibraryScanCompositeId); - if (!progressResult.IsError) - { - MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse(); - await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanFinishedEvent", progress, cancellationToken).ConfigureAwait(false); - } + if (!progressResult.IsError) + { + MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse(); + await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanFinishedEvent", progress, cancellationToken).ConfigureAwait(false); } } @@ -77,16 +90,27 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca /// Cancellation token that can be used to stop the execution. public async Task SendLibraryScanFailedEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken) { - if (!cancellationToken.IsCancellationRequested) - { - ErrorOr progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId); + if (cancellationToken.IsCancellationRequested) + return; + ErrorOr progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId); - if (!progressResult.IsError) - { - MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse(); - await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync( - "libraryScanFailedEvent", progress with { Status = LibraryScanJobStatus.Failed.ToString() }, cancellationToken).ConfigureAwait(false); - } + CleanupDebouncer(mediaLibraryScanCompositeId); + + if (!progressResult.IsError) + { + MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse(); + await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync( + "libraryScanFailedEvent", progress with { Status = LibraryScanJobStatus.Failed.ToString() }, cancellationToken).ConfigureAwait(false); } } + + /// + /// Disposes the debouncer that was used in a scan identified by , that is now completed. + /// + /// The object representing the unique identifier for the media library scan whose debouncer needs disposing. + private void CleanupDebouncer(MediaLibraryScanCompositeId mediaLibraryScanCompositeId) + { + if (_debouncers.TryRemove(mediaLibraryScanCompositeId, out DebounceDispatcher? debouncer)) + debouncer.Dispose(); + } } diff --git a/src/Lumina.Infrastructure/Lumina.Infrastructure.csproj b/src/Lumina.Infrastructure/Lumina.Infrastructure.csproj index 978ca0bb..2b5504ac 100644 --- a/src/Lumina.Infrastructure/Lumina.Infrastructure.csproj +++ b/src/Lumina.Infrastructure/Lumina.Infrastructure.csproj @@ -14,6 +14,7 @@ +