From dcee8c07f9a76e4299b59e81e80a07f6df126ee3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 7 Sep 2022 21:39:25 -0500 Subject: wip --- src/slskd/Application.cs | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index e4d47bb1..6c6ee475 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -369,14 +369,22 @@ namespace slskd _ = CheckVersionAsync(); } - if (OptionsAtStartup.Flags.NoShareScan) - { - Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed."); - } - else - { - _ = Shares.StartScanAsync(); - } + //if (OptionsAtStartup.Flags.NoShareScan) + //{ + // Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed."); + //} + //else + //{ + // _ = Shares.StartScanAsync(); + //} + + Shares.LoadFromDisk(); + Log.Information("Shares loaded from disk"); + + Log.Information("Caching browse response..."); + var browse = new BrowseResponse(await Shares.BrowseAsync()).ToByteArray(); + System.IO.File.WriteAllBytes(Path.Combine(Program.DataDirectory, "browse.cache"), browse); + Log.Information("Browse response cached."); if (OptionsAtStartup.Flags.NoConnect) { @@ -417,7 +425,21 @@ namespace slskd var sw = new Stopwatch(); sw.Start(); - var directories = await Shares.BrowseAsync(); + BrowseResponse response = default; + + var cacheFilename = Path.Combine(Program.DataDirectory, "browse.cache"); + var cacheFileInfo = new System.IO.FileInfo(cacheFilename); + + if (cacheFileInfo.Exists) + { + var stream = new System.IO.FileStream(cacheFilename, FileMode.Open, FileAccess.Read); + response = new RawBrowseResponse(cacheFileInfo.Length, stream); + } + else + { + var directories = await Shares.BrowseAsync(); + response = new BrowseResponse(directories); + } sw.Stop(); @@ -425,7 +447,7 @@ namespace slskd Metrics.Browse.CurrentResponseLatency.Update(sw.ElapsedMilliseconds); Metrics.Browse.ResponsesSent.Inc(1); - return new BrowseResponse(directories); + return response; } catch (Exception ex) { -- cgit v1.2.3 From 2729bc7c6315bf1555a8761de94134e5171d4014 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 7 Sep 2022 23:29:07 -0500 Subject: load cache on start, or force scan. cache first browse response --- src/slskd/Application.cs | 60 +++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 23 deletions(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 6c6ee475..07cf5dbd 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -369,22 +369,23 @@ namespace slskd _ = CheckVersionAsync(); } - //if (OptionsAtStartup.Flags.NoShareScan) - //{ - // Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed."); - //} - //else - //{ - // _ = Shares.StartScanAsync(); - //} - - Shares.LoadFromDisk(); - Log.Information("Shares loaded from disk"); - - Log.Information("Caching browse response..."); - var browse = new BrowseResponse(await Shares.BrowseAsync()).ToByteArray(); - System.IO.File.WriteAllBytes(Path.Combine(Program.DataDirectory, "browse.cache"), browse); - Log.Information("Browse response cached."); + if (await Shares.TryLoadFromDiskAsync()) + { + Log.Information("Loaded share cache from disk"); + } + else + { + Log.Warning("Failed to load share cache from disk. A share scan is required."); + + if (OptionsAtStartup.Flags.NoShareScan) + { + Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed."); + } + else + { + _ = Shares.StartScanAsync(); + } + } if (OptionsAtStartup.Flags.NoConnect) { @@ -427,20 +428,33 @@ namespace slskd BrowseResponse response = default; - var cacheFilename = Path.Combine(Program.DataDirectory, "browse.cache"); + var cacheFilename = Path.Combine(Program.DataDirectory, Filenames.BrowseCache); var cacheFileInfo = new System.IO.FileInfo(cacheFilename); - if (cacheFileInfo.Exists) - { - var stream = new System.IO.FileStream(cacheFilename, FileMode.Open, FileAccess.Read); - response = new RawBrowseResponse(cacheFileInfo.Length, stream); - } - else + if (!cacheFileInfo.Exists) { + Log.Warning("Browse response not cached. Rebuilding..."); + var directories = await Shares.BrowseAsync(); response = new BrowseResponse(directories); + + Log.Information("Caching browse response..."); + System.IO.File.WriteAllBytes(cacheFilename, response.ToByteArray()); + Log.Information("Browse response cached successfully"); } + cacheFileInfo = new System.IO.FileInfo(cacheFilename); + + if (!cacheFileInfo.Exists) + { + throw new IOException("Unable to access recently written browse cache; is the file still open?"); + } + + var stream = new System.IO.FileStream(cacheFilename, FileMode.Open, FileAccess.Read); + response = new RawBrowseResponse(cacheFileInfo.Length, stream); + + Log.Information("Returned cached browse response for {User}", username); + sw.Stop(); Metrics.Browse.ResponseLatency.Observe(sw.ElapsedMilliseconds); -- cgit v1.2.3 From e504c397a92f11fbc6ed724b52e1acf041a78850 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 8 Sep 2022 21:30:52 -0500 Subject: refine handling of browse cache --- src/slskd/Application.cs | 51 ++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 07cf5dbd..f3feec23 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -369,13 +369,13 @@ namespace slskd _ = CheckVersionAsync(); } - if (await Shares.TryLoadFromDiskAsync()) + // try to load the share cache from disk. + // if not successful, the file is missing one or more tables, or is not present at all + if (!await Shares.TryLoadFromDiskAsync()) { - Log.Information("Loaded share cache from disk"); - } - else - { - Log.Warning("Failed to load share cache from disk. A share scan is required."); + Log.Warning("Unable to load share cache from disk. A share scan is required."); + + State.SetValue(state => state with { Shares = state.Shares with { ScanPending = true } }); if (OptionsAtStartup.Flags.NoShareScan) { @@ -429,31 +429,18 @@ namespace slskd BrowseResponse response = default; var cacheFilename = Path.Combine(Program.DataDirectory, Filenames.BrowseCache); - var cacheFileInfo = new System.IO.FileInfo(cacheFilename); + var cacheFileInfo = new FileInfo(cacheFilename); if (!cacheFileInfo.Exists) { Log.Warning("Browse response not cached. Rebuilding..."); - - var directories = await Shares.BrowseAsync(); - response = new BrowseResponse(directories); - - Log.Information("Caching browse response..."); - System.IO.File.WriteAllBytes(cacheFilename, response.ToByteArray()); - Log.Information("Browse response cached successfully"); + await CacheBrowseResponse(); } - cacheFileInfo = new System.IO.FileInfo(cacheFilename); - - if (!cacheFileInfo.Exists) - { - throw new IOException("Unable to access recently written browse cache; is the file still open?"); - } - - var stream = new System.IO.FileStream(cacheFilename, FileMode.Open, FileAccess.Read); + var stream = new FileStream(cacheFilename, FileMode.Open, FileAccess.Read); response = new RawBrowseResponse(cacheFileInfo.Length, stream); - Log.Information("Returned cached browse response for {User}", username); + Log.Information("Sent browse response to {User}", username); sw.Stop(); @@ -1017,6 +1004,7 @@ namespace slskd { 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); + _ = CacheBrowseResponse(); SharesRefreshStarted = default; @@ -1030,6 +1018,13 @@ namespace slskd } } } + else if (!previous.Ready && current.Ready) + { + // the share transitioned into ready without completing a scan; it was loaded from disk + State.SetValue(state => state with { Shares = state.Shares with { ScanPending = false } }); + Log.Information("Shares loaded from disk successfully. Sharing {Directories} directories and {Files}", current.Directories, current.Files); + _ = CacheBrowseResponse(); + } else { // the scan is neither starting nor finishing; progress update @@ -1044,6 +1039,16 @@ namespace slskd } } + private async Task CacheBrowseResponse() + { + var directories = await Shares.BrowseAsync(); + var response = new BrowseResponse(directories); + + Log.Information("Warming browse response cache..."); + System.IO.File.WriteAllBytes(Path.Combine(Program.DataDirectory, Filenames.BrowseCache), response.ToByteArray()); + Log.Information("Browse response cached successfully"); + } + private void State_OnChange((State Previous, State Current) state) { _ = ApplicationHub.BroadcastStateAsync(state.Current); -- cgit v1.2.3