aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-11-05 14:05:31 -0500
committerGitHub <noreply@github.com>2022-11-05 14:05:31 -0500
commit2c6c42b03ba0254d92d176bad2f283be0a746969 (patch)
tree6de8a7834af99dae47ebef97d08d429da83fa816 /src
parentaccfdb85cac9ec5098297d45796f239f1a712c50 (diff)
parenta94f07ab844c074f4fcd931e3aacbbd888ca87aa (diff)
downloadslskd-2c6c42b03ba0254d92d176bad2f283be0a746969.tar.gz
Merge pull request #735 from slskd/client
Reconnect network client indefinitely, improvements to share state management
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs23
-rw-r--r--src/slskd/Network/NetworkClient.cs36
-rw-r--r--src/slskd/Shares/IShareService.cs7
-rw-r--r--src/slskd/Shares/ShareService.cs34
4 files changed, 56 insertions, 44 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 30cc6971..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;
@@ -418,14 +418,7 @@ namespace slskd.Network
return message;
};
})
- .WithAutomaticReconnect(new[]
- {
- TimeSpan.FromSeconds(0),
- TimeSpan.FromSeconds(1),
- TimeSpan.FromSeconds(3),
- TimeSpan.FromSeconds(10),
- TimeSpan.FromSeconds(30),
- })
+ .WithAutomaticReconnect(new ControllerRetryPolicy(0, 1, 3, 10, 30, 60))
.Build();
HubConnection.Reconnected += HubConnection_Reconnected;
@@ -453,5 +446,20 @@ namespace slskd.Network
SyncRoot.Release();
}
}
+
+ private class ControllerRetryPolicy : IRetryPolicy
+ {
+ public ControllerRetryPolicy(params int[] intervals)
+ {
+ Intervals = intervals;
+ }
+
+ private int[] Intervals { get; set; }
+
+ public TimeSpan? NextRetryDelay(RetryContext retryContext)
+ {
+ return TimeSpan.FromSeconds(Intervals[Math.Min(retryContext.PreviousRetryCount, Intervals.Length - 1)]);
+ }
+ }
}
}
diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs
index dff6a8ce..aea3240a 100644
--- a/src/slskd/Shares/IShareService.cs
+++ b/src/slskd/Shares/IShareService.cs
@@ -54,13 +54,6 @@ namespace slskd.Shares
Task<IEnumerable<Directory>> BrowseAsync(Share share = null);
/// <summary>
- /// Gets statistical information for the specified <paramref name="share"/>.
- /// </summary>
- /// <param name="share">The share to summarize.</param>
- /// <returns>The summary information.</returns>
- Task<(int Directories, int Files)> ComputeShareStatisticsAsync(Share share);
-
- /// <summary>
/// Dumps the local share cache to a file.
/// </summary>
/// <param name="filename">The destination file.</param>
diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs
index 76bce57c..109c9593 100644
--- a/src/slskd/Shares/ShareService.cs
+++ b/src/slskd/Shares/ShareService.cs
@@ -189,23 +189,6 @@ namespace slskd.Shares
}
/// <summary>
- /// Gets summary information for the specified <paramref name="share"/>.
- /// </summary>
- /// <param name="share">The share to summarize.</param>
- /// <returns>The summary information.</returns>
- public Task<(int Directories, int Files)> ComputeShareStatisticsAsync(Share share)
- {
- var prefix = share.RemotePath + (share.RemotePath.EndsWith('\\') ? string.Empty : '\\');
-
- var dirs = AllRepositories.Select(r => r.CountDirectories(prefix)).Sum();
- var files = AllRepositories.Select(r => r.CountFiles(prefix)).Sum();
-
- share.UpdateStatistics(dirs, files);
-
- return Task.FromResult((dirs, files));
- }
-
- /// <summary>
/// Dumps the local share cache to a file.
/// </summary>
/// <param name="filename">The destination file.</param>
@@ -336,7 +319,7 @@ namespace slskd.Shares
Log.Debug("Shared file cache database backup complete");
Log.Debug("Recomputing share statistics...");
- await Task.WhenAll(Local.Host.Shares.Select(share => ComputeShareStatisticsAsync(share)));
+ ComputeShareStatistics();
Log.Debug("Share statistics updated");
}
@@ -418,7 +401,7 @@ namespace slskd.Shares
Local.Repository.EnableKeepalive(true);
Log.Debug("Recomputing share statistics...");
- await Task.WhenAll(Local.Host.Shares.Select(share => ComputeShareStatisticsAsync(share)));
+ ComputeShareStatistics();
Log.Debug("Share statistics updated");
// one of several thigns happened above before we got here:
@@ -456,6 +439,19 @@ namespace slskd.Shares
}
}
+ private void ComputeShareStatistics()
+ {
+ foreach (var share in Local.Host.Shares)
+ {
+ var prefix = share.RemotePath + (share.RemotePath.EndsWith('\\') ? string.Empty : '\\');
+
+ var dirs = Local.Repository.CountDirectories(prefix);
+ var files = Local.Repository.CountFiles(prefix);
+
+ share.UpdateStatistics(dirs, files);
+ }
+ }
+
private void Configure(Options options)
{
SyncRoot.Wait();