From 13e8d31658fbe9083bb44bc0666983fa988af0ca Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 22 Apr 2026 18:47:48 -0500 Subject: add cache for IsBlacklisted --- src/slskd/Users/IUserService.cs | 3 ++- src/slskd/Users/UserService.cs | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/slskd/Users/IUserService.cs b/src/slskd/Users/IUserService.cs index f2cfa5f1..28bb6459 100644 --- a/src/slskd/Users/IUserService.cs +++ b/src/slskd/Users/IUserService.cs @@ -144,8 +144,9 @@ namespace slskd.Users /// /// The username to check. /// The IPAddress to check, if available. + /// A value indicating whether to compute blacklisted status on a cache miss. /// A value indicating whether the specified user and/or IP are blacklisted. - bool IsBlacklisted(string username, IPAddress ipAddress = null); + bool IsBlacklisted(string username, IPAddress ipAddress = null, bool bypassCache = true); /// /// Retrieves a value indicating whether the specified peer is privileged. diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index ee117855..0a620992 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -88,15 +88,20 @@ namespace slskd.Users /// /// /// + /// public UserService( ISoulseekClient soulseekClient, - IOptionsMonitor optionsMonitor) + IOptionsMonitor optionsMonitor, + ISystemClock systemClock) { Client = soulseekClient; OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); + InjectedClock = systemClock; + BlacklistDecisionCache = new MemoryCache(new MemoryCacheOptions { Clock = InjectedClock, ExpirationScanFrequency = TimeSpan.FromMinutes(1), SizeLimit = 1000 }); + // updates may be sent unsolicited from the server, so update when we get them. binding these events will cause // multiple redundant round trips when initially watching a user or when explicitly requesting via // GetStatus/GetStatistics. this is wasteful, but there's no functional side effect. @@ -141,8 +146,8 @@ namespace slskd.Users private IOptionsMonitor OptionsMonitor { get; } private Blacklist Blacklist { get; } = new Blacklist(); private IReadOnlyCollection CompiledBlacklistPatterns { get; set; } = []; - private MemoryCache BlacklistDecisionCache { get; } = - new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromMinutes(1), SizeLimit = 1000 }); + private ISystemClock InjectedClock { get; } + private MemoryCache BlacklistDecisionCache { get; } /// /// Gets or sets the internal cache of User data. @@ -291,23 +296,39 @@ namespace slskd.Users /// /// Gets a value indicating whether the specified and/or are blacklisted. /// + /// + /// This method may be taxing if the user has defined a blacklist file or large number of patterns. Call it to 'guard' + /// actions at the network level (with the exception of search results where latency is sensitive), but don't + /// use it to check once the user is 'in' the system (e.g. don't call this in the upload governor or queue logic). + /// /// The username to check. /// The IPAddress to check, if available. + /// A value indicating whether to compute blacklisted status on a cache miss. /// A value indicating whether the specified user and/or IP are blacklisted. - public bool IsBlacklisted(string username, IPAddress ipAddress = null) + public bool IsBlacklisted(string username, IPAddress ipAddress = null, bool bypassCache = true) { ArgumentNullException.ThrowIfNull(username); - if (BlacklistDecisionCache.TryGetValue(username, out var u)) + if (BlacklistDecisionCache.TryGetValue(username, out var cached)) { - return u; + return cached; + } + + // if we have a cache miss and the caller has specified bypassCache = false, assume that a cache miss + // means that the user is not blacklisted. we will only specify bypassCache = false when we're checking + // in a hot path, which at the time of this writing is only when determining whether to search shares for a + // request. we will check again before sending the results with bypassCache = true, and we should always + // follow this pattern when using bypassCache = false (specifically, check again before sending something) + if (!bypassCache) + { + return false; } - var b = IsBlacklistedInternal(username, ipAddress); + var blacklisted = IsBlacklistedInternal(username, ipAddress); - BlacklistDecisionCache.Set(username, b, new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(10) }); + BlacklistDecisionCache.Set(username, blacklisted, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) }); - return b; + return blacklisted; bool IsBlacklistedInternal(string username, IPAddress iPAddress = null) { -- cgit v1.2.3