From bbfd8de4468bc3e68ea5099e89b9bdad966fa4d8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 5 Nov 2022 12:45:12 -0500 Subject: reconnect client indefinitely --- src/slskd/Network/NetworkClient.cs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs index 30cc6971..c3ae9365 100644 --- a/src/slskd/Network/NetworkClient.cs +++ b/src/slskd/Network/NetworkClient.cs @@ -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)]); + } + } } } -- cgit v1.2.3 From f92ef3c7a13eeb65ded5a26fda2f261f120eb88e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 5 Nov 2022 13:26:58 -0500 Subject: only compute local shares --- src/slskd/Shares/IShareService.cs | 7 ------- src/slskd/Shares/ShareService.cs | 34 +++++++++++++++------------------- 2 files changed, 15 insertions(+), 26 deletions(-) (limited to 'src') 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 @@ -53,13 +53,6 @@ namespace slskd.Shares /// The entire contents of the share. Task> BrowseAsync(Share share = null); - /// - /// Gets statistical information for the specified . - /// - /// The share to summarize. - /// The summary information. - Task<(int Directories, int Files)> ComputeShareStatisticsAsync(Share share); - /// /// Dumps the local share cache to a file. /// 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 @@ -188,23 +188,6 @@ namespace slskd.Shares return Task.FromResult(results); } - /// - /// Gets summary information for the specified . - /// - /// The share to summarize. - /// The summary information. - 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)); - } - /// /// Dumps the local share cache to a file. /// @@ -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(); -- cgit v1.2.3 From a94f07ab844c074f4fcd931e3aacbbd888ca87aa Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 5 Nov 2022 13:28:13 -0500 Subject: update share state when hosts change --- src/slskd/Application.cs | 23 +++++++++++++++++++---- src/slskd/Network/NetworkClient.cs | 12 ++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) (limited to 'src') 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; -- cgit v1.2.3