aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-11-05 13:28:13 -0500
committerJP Dillingham <jp@dillingham.me>2022-11-05 13:28:13 -0500
commita94f07ab844c074f4fcd931e3aacbbd888ca87aa (patch)
tree6de8a7834af99dae47ebef97d08d429da83fa816 /src
parentf92ef3c7a13eeb65ded5a26fda2f261f120eb88e (diff)
downloadslskd-a94f07ab844c074f4fcd931e3aacbbd888ca87aa.tar.gz
update share state when hosts change
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs23
-rw-r--r--src/slskd/Network/NetworkClient.cs12
2 files changed, 25 insertions, 10 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index 5c48378f..5e8da061 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -1000,7 +1000,7 @@ namespace slskd
if (!previous.Scanning && current.Scanning)
{
- // the scan is starting
+ // the scan is starting. update the application state without manipulation
SharesRefreshStarted = DateTime.UtcNow;
State.SetValue(s => s with { Shares = current });
@@ -1009,7 +1009,7 @@ namespace slskd
}
else if (previous.Scanning && !current.Scanning)
{
- // the scan is finishing
+ // the scan is finishing. update the application state without manipulation...
State.SetValue(s => s with { Shares = current });
if (current.Faulted)
@@ -1018,6 +1018,7 @@ namespace slskd
}
else
{
+ // ...but if it completed successfully, immediately update again to lower the pending flag
State.SetValue(state => state with { Shares = state.Shares with { ScanPending = false } });
Log.Information("Shares scanned successfully. Found {Directories} directories and {Files} files in {Duration}ms", current.Directories, current.Files, (DateTime.UtcNow - SharesRefreshStarted).TotalMilliseconds);
rebuildBrowseCache = true;
@@ -1037,13 +1038,17 @@ namespace slskd
else if (!previous.Ready && current.Ready)
{
// the share transitioned into ready without completing a scan; it was loaded from disk
+ // this will (or should) also be true when the scan completes, which is why the code is using an else if here,
+ // but only if it happens without a corresponding lowering of the scanning flag do we want to execute this.
State.SetValue(state => state with { Shares = current with { ScanPending = false } });
Log.Information("Share cache loaded from disk successfully. Sharing {Directories} directories and {Files} files", current.Directories, current.Files);
rebuildBrowseCache = true;
}
else
{
- // the scan is neither starting nor finishing; progress update
+ // the scan is neither starting nor finishing; this is a status update of some sort,
+ // not a state change. we want to clamp the frequency of these during a scan to avoid overwhelming clients,
+ // only update if the progress has changed by at least 1%.
var lastProgress = Math.Round(previous.ScanProgress * 100);
var currentProgress = Math.Round(current.ScanProgress * 100);
@@ -1054,7 +1059,17 @@ namespace slskd
}
}
- if (rebuildBrowseCache || previous.Hosts.ToJson() != current.Hosts.ToJson())
+ // if the host configuration changed, update *just* the hosts to avoid stepping on anything that might
+ // have also happened above. a change in hosts will invalidate the cache, so do that too.
+ if (previous.Hosts.ToJson() != current.Hosts.ToJson())
+ {
+ State.SetValue(state => state with { Shares = state.Shares with { Hosts = current.Hosts } });
+ rebuildBrowseCache = true;
+ }
+
+ // if a scan just completed successfully (but not failed!), shares were loaded from disk, or
+ // host configuration changed, rebuild caches and upload shares to the network controller (if connected)
+ if (rebuildBrowseCache)
{
_ = CacheBrowseResponse();
_ = NetworkClient.SynchronizeAsync();
diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs
index c3ae9365..bf79a790 100644
--- a/src/slskd/Network/NetworkClient.cs
+++ b/src/slskd/Network/NetworkClient.cs
@@ -196,12 +196,12 @@ namespace slskd.Network
using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/network/shares/{token}");
using var content = new MultipartFormDataContent
- {
- { new StringContent(OptionsMonitor.CurrentValue.InstanceName), "name" },
- { new StringContent(ComputeCredential(token)), "credential" },
- { new StringContent(Shares.LocalHost.Shares.ToJson()), "shares" },
- { new StreamContent(stream), "database", "shares" },
- };
+ {
+ { new StringContent(OptionsMonitor.CurrentValue.InstanceName), "name" },
+ { new StringContent(ComputeCredential(token)), "credential" },
+ { new StringContent(Shares.LocalHost.Shares.ToJson()), "shares" },
+ { new StreamContent(stream), "database", "shares" },
+ };
request.Headers.Add("X-API-Key", OptionsMonitor.CurrentValue.Network.Controller.ApiKey);
request.Content = content;