From 64ef939fc06e24c2290caf80fba42fe462075753 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 16 Dec 2021 07:29:42 -0600 Subject: add Filled for share state --- src/slskd/State.cs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src') diff --git a/src/slskd/State.cs b/src/slskd/State.cs index d07c603a..4c3deff8 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -69,6 +69,11 @@ namespace slskd /// public bool Filling { get; init; } = false; + /// + /// Gets a value indicating whether the cache is filled. + /// + public bool Filled { get; init; } = false; + /// /// Gets a value indicating whether the cache is faulted. /// -- cgit v1.2.3 From 6239a3ef944529ef9b8d394f696d38293c154fe2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 16 Dec 2021 07:30:17 -0600 Subject: respond appropriately when the cache is filling --- src/slskd/Shares/SharedFileCache.cs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 6ed21d9d..ad2468da 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -112,10 +112,14 @@ namespace slskd.Shares /// The contents of the cache. public IEnumerable Browse() { - // ignore requests while the cache is building - if (SyncRoot.CurrentCount == 0) + if (!State.CurrentValue.Filled) { - return new[] { new Directory("Scanning shares, please check back in a few minutes.") }; + if (State.CurrentValue.Filling) + { + return new[] { new Directory("Scanning shares, please check back in a few minutes.") }; + } + + return Enumerable.Empty(); } var directories = new ConcurrentDictionary(); @@ -168,13 +172,13 @@ namespace slskd.Shares try { + State.SetValue(state => state with { Filling = true, Filled = false, FillProgress = 0 }); + Log.Debug("Starting shared file scan"); + await Task.Yield(); ResetCache(); - State.SetValue(state => state with { Filling = true, FillProgress = 0 }); - Log.Debug("Starting shared file scan"); - var sw = new Stopwatch(); var swSnapshot = 0L; sw.Start(); @@ -289,12 +293,12 @@ namespace slskd.Shares MaskedDirectories = maskedDirectories; MaskedFiles = files; - State.SetValue(state => state with { Filling = false, Faulted = false, 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 }); Log.Debug($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {unmaskedDirectories.Count}, Files: {files.Count}"); } catch (Exception ex) { - State.SetValue(state => state with { Filling = false, Faulted = true, FillProgress = 0 }); + State.SetValue(state => state with { Filling = false, Faulted = true, Filled = false, FillProgress = 0 }); Log.Warning(ex, "Encountered error during scan of shared files: {Message}", ex.Message); throw; } @@ -311,6 +315,16 @@ namespace slskd.Shares /// The contents of the directory. public Directory List(string directory) { + if (!State.CurrentValue.Filled) + { + if (State.CurrentValue.Filling) + { + return new Directory(directory, new[] { new File(1, "Scanning shares, please check back in a few minutes.", 0, string.Empty) }); + } + + return null; + } + var files = MaskedFiles.Where(f => f.Key.StartsWith(directory)).Select(kvp => { var f = kvp.Value; @@ -373,8 +387,7 @@ namespace slskd.Shares /// The matching files. public async Task> SearchAsync(SearchQuery query) { - // ignore requests while the cache is building - if (SyncRoot.CurrentCount == 0) + if (!State.CurrentValue.Filled) { return Enumerable.Empty(); } -- cgit v1.2.3