diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-04-21 19:30:06 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-04-21 19:30:06 -0500 |
| commit | d2a71a7340c5caf7923824910f112be5b169378d (patch) | |
| tree | 9853c4d1a224eb9dada890a266077d6a2fe417b7 /src | |
| parent | 73634ad0b54d486cc278c4950583754ad77aac00 (diff) | |
| download | slskd-d2a71a7340c5caf7923824910f112be5b169378d.tar.gz | |
wip, wtf is going on here
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Users/UserService.cs | 60 |
1 files changed, 41 insertions, 19 deletions
diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 73bf1118..0eb541e0 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -34,12 +34,14 @@ using Microsoft.Extensions.Options; namespace slskd.Users { + using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text.RegularExpressions; using System.Threading.Tasks; + using Microsoft.Extensions.Caching.Memory; using NetTools; using Serilog; using Soulseek; @@ -139,6 +141,7 @@ namespace slskd.Users private IOptionsMonitor<Options> OptionsMonitor { get; } private Blacklist Blacklist { get; } = new Blacklist(); private IReadOnlyCollection<Regex> CompiledBlacklistPatterns { get; set; } = []; + private MemoryCache GetGroupCache { get; } = new MemoryCache(new MemoryCacheOptions() { }); /// <summary> /// Gets or sets the internal cache of User data. @@ -171,33 +174,49 @@ namespace slskd.Users /// <returns>The group for the specified username.</returns> public string GetGroup(string username) { + if (GetGroupCache.TryGetValue<string>(username, out var cached)) + { + return cached; + } + else + { + var group = GetGroupInternal(username); + + GetGroupCache.Set<string>(username, group, new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(10) }); + + return group; + } + // note: this is an extremely hot path; keep the work done to an absolute minimum. - if (UserDictionary.TryGetValue(username ?? string.Empty, out var user)) + string GetGroupInternal(string username) { - if (IsBlacklisted(user.Username)) + if (UserDictionary.TryGetValue(username ?? string.Empty, out var user)) { - return Application.BlacklistedGroup; - } + if (IsBlacklisted(user.Username)) + { + return Application.BlacklistedGroup; + } - if (user.Status?.IsPrivileged ?? false) - { - return Application.PrivilegedGroup; - } + if (user.Status?.IsPrivileged ?? false) + { + return Application.PrivilegedGroup; + } - if (user.Group != null) - { - return user.Group; - } + if (user.Group != null) + { + return user.Group; + } - var thresholds = OptionsMonitor.CurrentValue.Transfers.Groups.Leechers.Thresholds; + var thresholds = OptionsMonitor.CurrentValue.Transfers.Groups.Leechers.Thresholds; - if (user.Statistics?.FileCount < thresholds.Files || user.Statistics?.DirectoryCount < thresholds.Directories) - { - return Application.LeecherGroup; + if (user.Statistics?.FileCount < thresholds.Files || user.Statistics?.DirectoryCount < thresholds.Directories) + { + return Application.LeecherGroup; + } } - } - return Application.DefaultGroup; + return Application.DefaultGroup; + } } /// <summary> @@ -293,7 +312,10 @@ namespace slskd.Users /// <returns>A value indicating whether the specified user and/or IP are blacklisted.</returns> public bool IsBlacklisted(string username, IPAddress ipAddress = null) { - if (username is null) return false; + if (string.IsNullOrEmpty(username)) + { + throw new ArgumentNullException(nameof(username)); + } var blacklist = OptionsMonitor.CurrentValue.Transfers.Groups.Blacklisted; |
