From 1cd4b10a25052e4e82c1f86a9bc92f06f5605114 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 1 Jan 2024 18:43:18 -0600 Subject: wip --- src/slskd/Application.cs | 12 +++++-- src/slskd/Transfers/Uploads/UploadQueue.cs | 10 +++--- src/slskd/Users/IUserService.cs | 17 +++++++++ src/slskd/Users/UserService.cs | 56 ++++++++++++++++++++++++++++-- 4 files changed, 86 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 6e430fe5..506f08ac 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1357,13 +1357,21 @@ namespace slskd /// A Task resolving the UserInfo instance. private Task UserInfoResolver(string username, IPEndPoint endpoint) { + if (IsBlacklisted(username, endpoint.Address)) + { + return Task.FromResult(new UserInfo( + description: Options.Soulseek.Description, + uploadSlots: 0, + queueLength: int.MaxValue, + hasFreeUploadSlot: false)); + } + try { // note: users must first be watched or cached for leech and privilege detection to work. // we are deliberately skipping it here; if the username is watched // leech detection works and they get accurate info, if not, they won't - // todo: add a fetch of user info to ensure group accuracy - var groupName = Users.ResolveGroup(username); + var groupName = Users.GetGroup(username); var group = Transfers.Uploads.Queue.GetGroupInfo(groupName); // forecast the position at which this user would enter the queue if they were to request diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index 0b5f51b1..53e135e8 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -254,11 +254,11 @@ namespace slskd.Transfers /// Thrown if the specified filename is not enqueued. public int EstimatePosition(string username, string filename) { - var groupName = Users.ResolveGroup(username); + var groupName = Users.GetGroup(username); var groupRecord = Groups.GetValueOrDefault(groupName); // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user - var uploadsForGroup = Uploads.Where(kvp => Users.ResolveGroup(kvp.Key) == groupName); + var uploadsForGroup = Uploads.Where(kvp => Users.GetGroup(kvp.Key) == groupName); // the RoundRobin queue implementation is not strictly fair to all users; only uploads that are ready are candidates // for selection. this means that if Bob downloads files twice as fast as Alice, Bob is going to advance through the @@ -337,7 +337,7 @@ namespace slskd.Transfers /// public int ForecastPosition(string username) { - var groupName = Users.ResolveGroup(username); + var groupName = Users.GetGroup(username); // if there's a slot available, the user will enter the queue at position 0 (will start immediately) if (Groups.TryGetValue(groupName, out var groupRecord) && groupRecord.SlotAvailable) @@ -346,7 +346,7 @@ namespace slskd.Transfers } // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user - var uploadsForGroup = Uploads.Where(kvp => Users.ResolveGroup(kvp.Key) == groupName); + var uploadsForGroup = Uploads.Where(kvp => Users.GetGroup(kvp.Key) == groupName); // assuming that the queue will be processed in a true round-robin fashion and that the user will be the last in the // rotation (worst case), the user's start position will be equal to the number of users downloading or waiting, + 1. @@ -462,7 +462,7 @@ namespace slskd.Transfers if (ready.Any()) { - var group = Users.ResolveGroup(user.Key); + var group = Users.GetGroup(user.Key); groups.AddOrUpdate( key: group, diff --git a/src/slskd/Users/IUserService.cs b/src/slskd/Users/IUserService.cs index 88090f9f..04ef1527 100644 --- a/src/slskd/Users/IUserService.cs +++ b/src/slskd/Users/IUserService.cs @@ -39,10 +39,27 @@ namespace slskd.Users /// /// Gets the name of the group for the specified . /// + /// + /// The group name is fetched from cached data, and lookups should always be fast. + /// /// The username of the peer. /// The group for the specified username. string GetGroup(string username); + /// + /// Gets the name of the group for the specified . + /// + /// + /// An alternative to that does not rely on the + /// internal cache. This method allows privilege and leech detection for users + /// that are not watched. + /// + /// The username of the peer. + /// The peer's status. + /// The peer's statistics + /// The group for the specified username. + string GetGroup(string username, Status status, Statistics statistics); + /// /// Retrieves peer . /// diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 95fed12d..4ab3840c 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -30,6 +30,13 @@ namespace slskd.Users /// /// Provides information and operations for network peers. /// + /// + /// The method should be called to 'watch' users prior to any call to + /// . relies *only* on information stored in + /// the internal , and this dictionary is only populated for users that have been + /// watched. Prior to calling , check to + /// reduce round trips to the server. + /// public class UserService : IUserService { /// @@ -83,9 +90,33 @@ namespace slskd.Users private ILogger Log { get; set; } = Serilog.Log.ForContext(); private IOptionsMonitor OptionsMonitor { get; } private IStateMutator StateMutator { get; } - private ConcurrentDictionary UserDictionary { get; set; } = new ConcurrentDictionary(); private ConcurrentDictionary WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary(); + /// + /// Gets or sets the internal cache of User data. + /// + /// + /// + /// This method retrieves the requested user's information from a dictionary, and + /// is therefore a 'get'. This information is needed to control who can do what, + /// who is subject to what limits, how to control the upload queue, and how to + /// govern speeds. The logic in this method is the 'hottest' in the application + /// and *must* remain very simple and _fast_. + /// + /// + /// If this method is called for a user who has not previously been cached, it + /// will return the default group, meaning leech, privilege, and user defined group + /// discrimination will not work. For this reason it is critical that user information + /// is retrieved and cached upon first interaction with that user. + /// + /// + /// Uploads can be cached indefinitely, and for that reason the user data cache must be + /// filled indefinitely; meaning there is no invalidation and that user data will accrue + /// for the lifetime of the application (instance). + /// + /// + private ConcurrentDictionary UserDictionary { get; set; } = new ConcurrentDictionary(); + /// /// Gets the name of the group for the specified . /// @@ -198,12 +229,25 @@ namespace slskd.Users /// /// Adds the specified username to the server-side user list. /// + /// + /// + /// Idempotent; if a user is already watched, subsequent calls will + /// only update their status and statistics. + /// + /// + /// Any user we want to track in any way needs to be watched so that + /// their statistics and status are updated properly via server side events. + /// This seems extreme and wasteful, but the only alternative is to periodically + /// spam the server for the information instead of letting the server spam the client + /// when things change. + /// + /// /// The username of the peer. /// The operation context. public async Task WatchAsync(string username) { await Client.AddUserAsync(username); - WatchedUsernamesDictionary.TryAdd(username, true); + UserDictionary.TryAdd(username, new User { Username = username }); Log.Information("Added user {Username} to watch list", username); @@ -212,6 +256,14 @@ namespace slskd.Users // watched and all information populated when this method returns. await GetStatusAsync(username); await GetStatisticsAsync(username); + + // delay the add until last, so that IsWatched won't return true until stats + // and status are populated. this may result in several unecessary calls to + // WatchAsync (if someone is checking IsWatched() and WatchAsync()ing if false), + // but we can be sure that if IsWatched() is true, we have valid stats and status. + // if we can't ensure this, the application will have non-deterministic behavior + // when it makes decisions about user groups, limits, governance etc. + WatchedUsernamesDictionary.TryAdd(username, true); } private void UpdateStatistics(string username, Statistics statistics) -- cgit v1.2.3