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/Users/UserService.cs | 56 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) (limited to 'src/slskd/Users/UserService.cs') 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