aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-12-16 07:35:41 -0600
committerGitHub <noreply@github.com>2021-12-16 07:35:41 -0600
commitb7f6e2c305df5181205c8eec506cd60814ea9db2 (patch)
treed9056079e299f1aac22b370d5eca486528525881 /src
parent51bcb8d1bed3a01df06fa85b5a252e3c06689e05 (diff)
parent6239a3ef944529ef9b8d394f696d38293c154fe2 (diff)
downloadslskd-b7f6e2c305df5181205c8eec506cd60814ea9db2.tar.gz
Merge pull request #305 from slskd/shares
Respond to search, browse, and directory contents requests appropriately while the cache is unfilled
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Shares/SharedFileCache.cs33
-rw-r--r--src/slskd/State.cs5
2 files changed, 28 insertions, 10 deletions
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
/// <returns>The contents of the cache.</returns>
public IEnumerable<Directory> 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<Directory>();
}
var directories = new ConcurrentDictionary<string, Directory>();
@@ -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
/// <returns>The contents of the directory.</returns>
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
/// <returns>The matching files.</returns>
public async Task<IEnumerable<File>> SearchAsync(SearchQuery query)
{
- // ignore requests while the cache is building
- if (SyncRoot.CurrentCount == 0)
+ if (!State.CurrentValue.Filled)
{
return Enumerable.Empty<File>();
}
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
@@ -70,6 +70,11 @@ namespace slskd
public bool Filling { get; init; } = false;
/// <summary>
+ /// Gets a value indicating whether the cache is filled.
+ /// </summary>
+ public bool Filled { get; init; } = false;
+
+ /// <summary>
/// Gets a value indicating whether the cache is faulted.
/// </summary>
public bool Faulted { get; init; } = false;