From 40443e3c576418f98ddbe0f616be55359cb74dcb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 14 Apr 2024 17:59:51 -0500 Subject: bumpless (re)loading --- src/slskd/Core/Blacklist.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Blacklist.cs b/src/slskd/Core/Blacklist.cs index 1f55e1ba..907fd9ba 100644 --- a/src/slskd/Core/Blacklist.cs +++ b/src/slskd/Core/Blacklist.cs @@ -79,7 +79,7 @@ public enum BlacklistFormat public class Blacklist { private ILogger Log { get; set; } = Serilog.Log.ForContext(); - private ConcurrentDictionary Cache { get; } = new(); + private ConcurrentDictionary Cache { get; set; } = new(); /// /// Loads the contents of the specified into the Blacklist. @@ -101,7 +101,7 @@ public class Blacklist if (format == BlacklistFormat.AutoDetect) { Log.Debug("Attempting to auto-detect blacklist fomat from contents..."); - format = await AutoDetectBlacklistFormat(filename); // FormatException + format = await AutoDetectBlacklistFormat(filename); // FormatException if unsuccessful Log.Debug("Detected blacklist format {Format}", format); } @@ -110,7 +110,7 @@ public class Blacklist Access = FileAccess.Read, Mode = FileMode.Open, Share = FileShare.Read, - BufferSize = 8192, + BufferSize = 262144, // 256kib, assumes most blacklists will be > 1mb }); var dict = new ConcurrentDictionary>(); @@ -178,14 +178,19 @@ public class Blacklist } } - // copy working dictionary to cache, converting List<> to array to increase access speed + // copy working dictionary to a temporary cache, converting List<> to array to increase access speed + var tempCache = new ConcurrentDictionary(); + foreach (var key in dict.Keys) { - Cache.AddOrUpdate( + tempCache.AddOrUpdate( key, addValueFactory: (_) => dict[key].ToArray(), updateValueFactory: (_, _) => dict[key].ToArray()); } + + // swap the temporary cache for the "real" cache, enabling a bumpless update + Cache = tempCache; } /// @@ -229,11 +234,11 @@ public class Blacklist Access = FileAccess.Read, Mode = FileMode.Open, Share = FileShare.Read, - BufferSize = 512, }); string line = default; + // read the first non-empty, non-commented line from the file while ((line = await reader.ReadLineAsync()) != null) { if (string.IsNullOrWhiteSpace(line) || line.StartsWith('#')) -- cgit v1.2.3