From 38255041d2e68a6fee1f481b3922869d9b577510 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 29 May 2022 22:44:53 -0500 Subject: refactor shares service/cache --- src/slskd/State.cs | 68 +++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 31 deletions(-) (limited to 'src/slskd/State.cs') diff --git a/src/slskd/State.cs b/src/slskd/State.cs index 0daa5d76..b84efb6c 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -35,7 +35,7 @@ namespace slskd public bool PendingShareRescan { get; init; } public ServerState Server { get; init; } = new ServerState(); public DistributedNetworkState DistributedNetwork { get; init; } = new DistributedNetworkState(); - public SharedFileCacheState SharedFileCache { get; init; } = new SharedFileCacheState(); + public ShareState Shares { get; init; } = new ShareState(); public string[] Rooms { get; init; } = Array.Empty(); public User[] Users { get; init; } = Array.Empty(); } @@ -76,43 +76,49 @@ namespace slskd } /// - /// Share cache state. + /// Share state. /// - public record SharedFileCacheState + public record ShareState { - /// - /// Gets a value indicating whether the cache is being filled. - /// - public bool Filling { get; init; } = false; + public ShareCacheState Cache { get; init; } = new ShareCacheState(); + public bool ScanPending { get; init; } - /// - /// Gets a value indicating whether the cache is filled. - /// - public bool Filled { get; init; } = false; + public record ShareCacheState + { + /// + /// Gets a value indicating whether the cache is being filled. + /// + public bool Filling { get; init; } = false; - /// - /// Gets a value indicating whether the cache is faulted. - /// - public bool Faulted { get; init; } = false; + /// + /// Gets a value indicating whether the cache is filled. + /// + public bool Filled { get; init; } = false; - /// - /// Gets the current fill progress. - /// - public double FillProgress { get; init; } + /// + /// Gets a value indicating whether the cache is faulted. + /// + public bool Faulted { get; init; } = false; - /// - /// Gets the number of cached directories. - /// - public int Directories { get; init; } + /// + /// Gets the current fill progress. + /// + public double FillProgress { get; init; } - /// - /// Gets the number of cached files. - /// - public int Files { get; init; } + /// + /// Gets the number of cached directories. + /// + public int Directories { get; init; } - /// - /// Gets the number of directories excluded by filters. - /// - public int ExcludedDirectories { get; init; } + /// + /// Gets the number of cached files. + /// + public int Files { get; init; } + + /// + /// Gets the number of directories excluded by filters. + /// + public int ExcludedDirectories { get; init; } + } } } \ No newline at end of file -- cgit v1.2.3 From d9bd83fbec533d86e43e0a55ba3543948eba96ff Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 30 May 2022 13:54:26 -0500 Subject: ShareCacheState -> CacheState --- src/slskd/State.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/State.cs') diff --git a/src/slskd/State.cs b/src/slskd/State.cs index b84efb6c..05a96a76 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -80,10 +80,10 @@ namespace slskd /// public record ShareState { - public ShareCacheState Cache { get; init; } = new ShareCacheState(); + public CacheState Cache { get; init; } = new CacheState(); public bool ScanPending { get; init; } - public record ShareCacheState + public record CacheState { /// /// Gets a value indicating whether the cache is being filled. -- cgit v1.2.3 From 48548c7eb96fb723f67aea71dc9760581b41ac68 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 30 May 2022 16:10:45 -0500 Subject: simplify share state --- src/slskd/State.cs | 67 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 32 deletions(-) (limited to 'src/slskd/State.cs') diff --git a/src/slskd/State.cs b/src/slskd/State.cs index 05a96a76..863c59da 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -80,45 +80,48 @@ namespace slskd /// public record ShareState { - public CacheState Cache { get; init; } = new CacheState(); public bool ScanPending { get; init; } + public bool Scanning { get; init; } + public double ScanProgress { get; init; } + public int Directories { get; init; } + public int Files { get; init; } + } - public record CacheState - { - /// - /// Gets a value indicating whether the cache is being filled. - /// - public bool Filling { get; init; } = false; + public record SharedFileCacheState + { + /// + /// Gets a value indicating whether the cache is being filled. + /// + 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 filled. + /// + public bool Filled { get; init; } = false; - /// - /// Gets a value indicating whether the cache is faulted. - /// - public bool Faulted { get; init; } = false; + /// + /// Gets a value indicating whether the cache is faulted. + /// + public bool Faulted { get; init; } = false; - /// - /// Gets the current fill progress. - /// - public double FillProgress { get; init; } + /// + /// Gets the current fill progress. + /// + public double FillProgress { get; init; } - /// - /// Gets the number of cached directories. - /// - public int Directories { get; init; } + /// + /// Gets the number of cached directories. + /// + public int Directories { get; init; } - /// - /// Gets the number of cached files. - /// - public int Files { get; init; } + /// + /// Gets the number of cached files. + /// + public int Files { get; init; } - /// - /// Gets the number of directories excluded by filters. - /// - public int ExcludedDirectories { get; init; } - } + /// + /// Gets the number of directories excluded by filters. + /// + public int ExcludedDirectories { get; init; } } } \ No newline at end of file -- cgit v1.2.3 From f6aad956a83155e8e89add7e24804d6b99ec4119 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 1 Jun 2022 09:38:17 -0500 Subject: light refactoring --- src/slskd/Application.cs | 44 +++++++++++++++++++++++---------------- src/slskd/Program.cs | 2 +- src/slskd/Search/SearchService.cs | 15 ++++++++----- src/slskd/Shares/IShareService.cs | 4 ++-- src/slskd/Shares/ShareService.cs | 5 +++-- src/slskd/State.cs | 1 + 6 files changed, 43 insertions(+), 28 deletions(-) (limited to 'src/slskd/State.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 110b1d4f..880e8f74 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -441,7 +441,7 @@ namespace slskd // send whatever counts we have currently. we'll probably connect before the cache is primed, so these will be zero // initially, but we'll update them when the cache is filled. - _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Cache.Directories, State.CurrentValue.Shares.Cache.Files); + _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Directories, State.CurrentValue.Shares.Files); } private void Client_PrivateMessageRecieved(object sender, PrivateMessageReceivedEventArgs args) @@ -509,9 +509,7 @@ namespace slskd private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args) { - // this is really verbose. Console.WriteLine($"[{args.Transfer.Direction.ToString().ToUpper()}] - // [{args.Transfer.Username}/{Path.GetFileName(args.Transfer.Filename)}] - // {args.Transfer.BytesTransferred}/{args.Transfer.Size} {args.Transfer.PercentComplete}% {args.Transfer.AverageSpeed}kb/s"); + // no-op. this is really verbose, use for troubleshooting. } private void Client_TransferStateChanged(object sender, TransferStateChangedEventArgs args) @@ -555,7 +553,7 @@ namespace slskd /// A Task resolving an instance of Soulseek.Directory containing the contents of the requested directory. private async Task DirectoryContentsResponseResolver(string username, IPEndPoint endpoint, int token, string directory) { - var dir = await Shares.ListAsync(directory); + var dir = await Shares.ListDirectoryAsync(directory); return dir; } @@ -577,15 +575,25 @@ namespace slskd private async Task EnqueueDownloadAction(string username, IPEndPoint endpoint, string filename, ITransferTracker tracker) { _ = endpoint; - var localFilename = (await Shares.ResolveAsync(filename)).ToLocalOSPath(); - - var fileInfo = new FileInfo(localFilename); + string localFilename; + FileInfo fileInfo = default; Console.WriteLine($"[UPLOAD REQUESTED] [{username}/{filename}]"); - if (!fileInfo.Exists) + try + { + localFilename = (await Shares.ResolveFilenameAsync(filename)).ToLocalOSPath(); + + fileInfo = new FileInfo(localFilename); + + if (!fileInfo.Exists) + { + throw new NotFoundException(); + } + } + catch (NotFoundException) { - Console.WriteLine($"[UPLOAD REJECTED] File {localFilename} not found."); + Console.WriteLine($"[UPLOAD REJECTED] File {filename} not found."); throw new DownloadEnqueueException($"File not shared."); } @@ -840,7 +848,7 @@ namespace slskd { var (previous, current) = state; - if (!previous.Cache.Filling && current.Cache.Filling) + if (!previous.Scanning && current.Scanning) { SharesRefreshStarted = DateTime.UtcNow; @@ -848,33 +856,33 @@ namespace slskd Log.Information("Scanning shares"); } - var lastProgress = Math.Round(previous.Cache.FillProgress * 100); - var currentProgress = Math.Round(current.Cache.FillProgress * 100); + var lastProgress = Math.Round(previous.ScanProgress * 100); + var currentProgress = Math.Round(current.ScanProgress * 100); if (lastProgress != currentProgress && Math.Round(currentProgress, 0) % 10 == 0) { State.SetValue(s => s with { Shares = current }); - Log.Information("Scanned {Percent}% of shared directories. Found {Files} files so far.", currentProgress, current.Cache.Files); + Log.Information("Scanned {Percent}% of shared directories. Found {Files} files so far.", currentProgress, current.Files); } - if (previous.Cache.Filling && !current.Cache.Filling) + if (previous.Scanning && !current.Scanning) { State.SetValue(s => s with { Shares = current }); - if (current.Cache.Faulted) + if (current.Faulted) { Log.Error("Failed to scan shares."); } else { State.SetValue(s => s with { PendingShareRescan = false }); - Log.Information("Shares scanned successfully. Found {Directories} directories and {Files} files in {Duration}ms", current.Cache.Directories, current.Cache.Files, (DateTime.UtcNow - SharesRefreshStarted).TotalMilliseconds); + Log.Information("Shares scanned successfully. Found {Directories} directories and {Files} files in {Duration}ms", current.Directories, current.Files, (DateTime.UtcNow - SharesRefreshStarted).TotalMilliseconds); SharesRefreshStarted = default; if (Client.State.HasFlag(SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn)) { - _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Cache.Directories, State.CurrentValue.Shares.Cache.Files); + _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Directories, State.CurrentValue.Shares.Files); } } } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 1a5d2a28..df1fb55c 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -682,7 +682,7 @@ namespace slskd } // if we made it this far, the caller is either looking for a route that was synthesized with a SPA router, or is genuinely confused. - // if the request is for a directory, modify the request to redirect it to the index, otherwise leave it alone and let it 404 in the next + // if the request is for a directory, modify the request to redirect it to the index, otherwise leave it alone and let it 404 in the next // middleware app.Use(async (context, next) => { diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 949a7088..f077e76b 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -162,18 +162,23 @@ namespace slskd.Search /// /// The search to delete. /// The operation context. - public async Task DeleteAsync(Search search) + public Task DeleteAsync(Search search) { if (search == default) { throw new ArgumentNullException(nameof(search)); } - using var context = ContextFactory.CreateDbContext(); - context.Searches.Remove(search); - await context.SaveChangesAsync(); + return DoDeleteAsync(search); - await SearchHub.BroadcastDeleteAsync(search); + async Task DoDeleteAsync(Search search) + { + using var context = ContextFactory.CreateDbContext(); + context.Searches.Remove(search); + await context.SaveChangesAsync(); + + await SearchHub.BroadcastDeleteAsync(search); + } } /// diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index cc91ba07..16f1a890 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -47,7 +47,7 @@ namespace slskd.Shares /// /// The directory for which the contents are to be listed. /// The contents of the directory. - Task ListAsync(string directory); + Task ListDirectoryAsync(string directory); /// /// Resolves the local filename of the specified , if the mask is associated with a @@ -58,7 +58,7 @@ namespace slskd.Shares /// /// Thrown when the specified remote filename can not be associated with a configured share. /// - Task ResolveAsync(string remoteFilename); + Task ResolveFilenameAsync(string remoteFilename); /// /// Searches the cache for the specified and returns the matching files. diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index d99c5f6a..7c08ad13 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -52,6 +52,7 @@ namespace slskd.Shares // scan is pending if faulted, or if state DIDN'T just transition from filling to not filling AND a scan was already pending ScanPending = current.Faulted || (!(previous.Filling && !current.Filling) && state.ScanPending), Scanning = current.Filling, + Faulted = current.Faulted, ScanProgress = current.FillProgress, Directories = current.Directories, Files = current.Files, @@ -88,7 +89,7 @@ namespace slskd.Shares /// /// The directory for which the contents are to be listed. /// The contents of the directory. - public Task ListAsync(string directory) + public Task ListDirectoryAsync(string directory) => Task.FromResult(Cache.List(directory)); /// @@ -100,7 +101,7 @@ namespace slskd.Shares /// /// Thrown when the specified remote filename can not be associated with a configured share. /// - public Task ResolveAsync(string remoteFilename) + public Task ResolveFilenameAsync(string remoteFilename) { var resolvedFilename = Cache.Resolve(remoteFilename); diff --git a/src/slskd/State.cs b/src/slskd/State.cs index 863c59da..407aeb9e 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -82,6 +82,7 @@ namespace slskd { public bool ScanPending { get; init; } public bool Scanning { get; init; } + public bool Faulted { get; init; } public double ScanProgress { get; init; } public int Directories { get; init; } public int Files { get; init; } -- cgit v1.2.3 From 2efd8278ce9a5a895fe0c75bd95080ecc386163b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 2 Jun 2022 07:28:10 -0500 Subject: differentiate configured shares vs cached shares --- src/slskd/Shares/API/Controllers/SharesController.cs | 2 +- src/slskd/Shares/IShareService.cs | 5 +++++ src/slskd/Shares/ShareService.cs | 14 +++++++++++--- src/slskd/Shares/SharedFileCache.cs | 20 ++++++++++++++++++-- src/slskd/State.cs | 9 +++++++-- 5 files changed, 42 insertions(+), 8 deletions(-) (limited to 'src/slskd/State.cs') diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index 8ae8551f..41178bac 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -59,7 +59,7 @@ namespace slskd.Shares.API { var browse = await Shares.BrowseAsync(); - var summary = Shares.Shares.Select(share => + var summary = Shares.CachedShares.Select(share => { var directories = browse.Where(directory => directory.Name.StartsWith(share.RemotePath)); var fileCount = directories.Aggregate(seed: 0, (sum, directory) => diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index 16f1a890..fd88f2b0 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -26,6 +26,11 @@ namespace slskd.Shares /// public interface IShareService { + /// + /// Gets the list of shares stored in the cache. + /// + IReadOnlyList CachedShares { get; } + /// /// Gets the list of configured shares. /// diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 9cae0d42..ead96d10 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -55,6 +55,11 @@ namespace slskd.Shares Directories = current.Directories, Files = current.Files, }); + + if (previous.Filling && !current.Filling) + { + CachedShares = current.Shares.ToList().AsReadOnly(); + } }); OptionsMonitor = optionsMonitor; @@ -68,7 +73,10 @@ namespace slskd.Shares /// public IReadOnlyList Shares => SharesList.AsReadOnly(); - public IReadOnlyList FilterRegexes => FilterRegexList.AsReadOnly(); + /// + /// Gets the list of shares stored in the cache. + /// + public IReadOnlyList CachedShares { get; private set; } = new List().AsReadOnly(); /// /// Gets the state monitor for the service. @@ -81,7 +89,7 @@ namespace slskd.Shares private List SharesList { get; set; } = new List(); private IManagedState State { get; } = new ManagedState(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); - private List FilterRegexList { get; set; } = new List(); + private List FilterRegexes { get; set; } = new List(); /// /// Returns the entire contents of the share. @@ -160,7 +168,7 @@ namespace slskd.Shares .ToList(); SharesList = shares; - FilterRegexList = regexes; + FilterRegexes = regexes; State.SetValue(state => state with { ScanPending = true }); diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 8ac52462..6b7a3ca3 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -175,7 +175,14 @@ namespace slskd.Shares try { - State.SetValue(state => state with { Filling = true, Filled = false, FillProgress = 0 }); + State.SetValue(state => state with + { + Filling = true, + Filled = false, + FillProgress = 0, + Shares = Enumerable.Empty().ToList().AsReadOnly(), + }); + Log.Debug("Starting shared file scan"); await Task.Yield(); @@ -291,7 +298,16 @@ namespace slskd.Shares MaskedDirectories = maskedDirectories; MaskedFiles = files; - State.SetValue(state => state with { Filling = false, Faulted = false, Filled = true, 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, + Shares = shares.ToList().AsReadOnly(), + }); + Log.Debug($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {unmaskedDirectories.Count}, Files: {files.Count}"); } catch (Exception ex) diff --git a/src/slskd/State.cs b/src/slskd/State.cs index 407aeb9e..10ed80f6 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -21,6 +21,7 @@ namespace slskd using System.Collections.Generic; using System.Net; using System.Text.Json.Serialization; + using slskd.Shares; using slskd.Users; using Soulseek; @@ -32,7 +33,6 @@ namespace slskd public VersionState Version { get; init; } = new VersionState(); public bool PendingReconnect { get; init; } public bool PendingRestart { get; init; } - public bool PendingShareRescan { get; init; } public ServerState Server { get; init; } = new ServerState(); public DistributedNetworkState DistributedNetwork { get; init; } = new DistributedNetworkState(); public ShareState Shares { get; init; } = new ShareState(); @@ -69,7 +69,7 @@ namespace slskd public string BranchRoot { get; init; } public bool CanAcceptChildren { get; init; } public int ChildLimit { get; init; } - public IReadOnlyCollection Children { get; init; } + public IReadOnlyCollection Children { get; init; } = new List().AsReadOnly(); public bool HasParent { get; init; } public bool IsBranchRoot { get; init; } public string Parent { get; init; } @@ -124,5 +124,10 @@ namespace slskd /// Gets the number of directories excluded by filters. /// public int ExcludedDirectories { get; init; } + + /// + /// Gets the list of shares stored in the cache. + /// + public IReadOnlyCollection Shares { get; init; } = new List().AsReadOnly(); } } \ No newline at end of file -- cgit v1.2.3