From 2efd8278ce9a5a895fe0c75bd95080ecc386163b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 2 Jun 2022 07:28:10 -0500 Subject: differentiate configured shares vs cached shares --- src/slskd/Shares/API/Controllers/SharesController.cs | 2 +- src/slskd/Shares/IShareService.cs | 5 +++++ src/slskd/Shares/ShareService.cs | 14 +++++++++++--- src/slskd/Shares/SharedFileCache.cs | 20 ++++++++++++++++++-- src/slskd/State.cs | 9 +++++++-- 5 files changed, 42 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index 8ae8551f..41178bac 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -59,7 +59,7 @@ namespace slskd.Shares.API { var browse = await Shares.BrowseAsync(); - var summary = Shares.Shares.Select(share => + var summary = Shares.CachedShares.Select(share => { var directories = browse.Where(directory => directory.Name.StartsWith(share.RemotePath)); var fileCount = directories.Aggregate(seed: 0, (sum, directory) => diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index 16f1a890..fd88f2b0 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -26,6 +26,11 @@ namespace slskd.Shares /// public interface IShareService { + /// + /// Gets the list of shares stored in the cache. + /// + IReadOnlyList CachedShares { get; } + /// /// Gets the list of configured shares. /// diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 9cae0d42..ead96d10 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -55,6 +55,11 @@ namespace slskd.Shares Directories = current.Directories, Files = current.Files, }); + + if (previous.Filling && !current.Filling) + { + CachedShares = current.Shares.ToList().AsReadOnly(); + } }); OptionsMonitor = optionsMonitor; @@ -68,7 +73,10 @@ namespace slskd.Shares /// public IReadOnlyList Shares => SharesList.AsReadOnly(); - public IReadOnlyList FilterRegexes => FilterRegexList.AsReadOnly(); + /// + /// Gets the list of shares stored in the cache. + /// + public IReadOnlyList CachedShares { get; private set; } = new List().AsReadOnly(); /// /// Gets the state monitor for the service. @@ -81,7 +89,7 @@ namespace slskd.Shares private List SharesList { get; set; } = new List(); private IManagedState State { get; } = new ManagedState(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); - private List FilterRegexList { get; set; } = new List(); + private List FilterRegexes { get; set; } = new List(); /// /// Returns the entire contents of the share. @@ -160,7 +168,7 @@ namespace slskd.Shares .ToList(); SharesList = shares; - FilterRegexList = regexes; + FilterRegexes = regexes; State.SetValue(state => state with { ScanPending = true }); diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 8ac52462..6b7a3ca3 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -175,7 +175,14 @@ namespace slskd.Shares try { - State.SetValue(state => state with { Filling = true, Filled = false, FillProgress = 0 }); + State.SetValue(state => state with + { + Filling = true, + Filled = false, + FillProgress = 0, + Shares = Enumerable.Empty().ToList().AsReadOnly(), + }); + Log.Debug("Starting shared file scan"); await Task.Yield(); @@ -291,7 +298,16 @@ namespace slskd.Shares MaskedDirectories = maskedDirectories; MaskedFiles = files; - State.SetValue(state => state with { Filling = false, Faulted = false, Filled = true, FillProgress = 1, Directories = MaskedDirectories.Count, Files = MaskedFiles.Count }); + State.SetValue(state => state with { + Filling = false, + Faulted = false, + Filled = true, + FillProgress = 1, + Directories = MaskedDirectories.Count, + Files = MaskedFiles.Count, + Shares = shares.ToList().AsReadOnly(), + }); + Log.Debug($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {unmaskedDirectories.Count}, Files: {files.Count}"); } catch (Exception ex) diff --git a/src/slskd/State.cs b/src/slskd/State.cs index 407aeb9e..10ed80f6 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -21,6 +21,7 @@ namespace slskd using System.Collections.Generic; using System.Net; using System.Text.Json.Serialization; + using slskd.Shares; using slskd.Users; using Soulseek; @@ -32,7 +33,6 @@ namespace slskd public VersionState Version { get; init; } = new VersionState(); public bool PendingReconnect { get; init; } public bool PendingRestart { get; init; } - public bool PendingShareRescan { get; init; } public ServerState Server { get; init; } = new ServerState(); public DistributedNetworkState DistributedNetwork { get; init; } = new DistributedNetworkState(); public ShareState Shares { get; init; } = new ShareState(); @@ -69,7 +69,7 @@ namespace slskd public string BranchRoot { get; init; } public bool CanAcceptChildren { get; init; } public int ChildLimit { get; init; } - public IReadOnlyCollection Children { get; init; } + public IReadOnlyCollection Children { get; init; } = new List().AsReadOnly(); public bool HasParent { get; init; } public bool IsBranchRoot { get; init; } public string Parent { get; init; } @@ -124,5 +124,10 @@ namespace slskd /// Gets the number of directories excluded by filters. /// public int ExcludedDirectories { get; init; } + + /// + /// Gets the list of shares stored in the cache. + /// + public IReadOnlyCollection Shares { get; init; } = new List().AsReadOnly(); } } \ No newline at end of file -- cgit v1.2.3