aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-01-01 18:43:18 -0600
committerJP Dillingham <jp@dillingham.me>2024-01-01 18:43:18 -0600
commit1cd4b10a25052e4e82c1f86a9bc92f06f5605114 (patch)
tree2ef809c949945e44de0df0e914134a34ebb06a16 /src
parentd409b1e3e371cfe8fd1e59e9ad53b0f7e4edafca (diff)
downloadslskd-1cd4b10a25052e4e82c1f86a9bc92f06f5605114.tar.gz
wip
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs12
-rw-r--r--src/slskd/Transfers/Uploads/UploadQueue.cs10
-rw-r--r--src/slskd/Users/IUserService.cs17
-rw-r--r--src/slskd/Users/UserService.cs56
4 files changed, 86 insertions, 9 deletions
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
/// <returns>A Task resolving the UserInfo instance.</returns>
private Task<UserInfo> 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
/// <exception cref="NotFoundException">Thrown if the specified filename is not enqueued.</exception>
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
/// </returns>
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,11 +39,28 @@ namespace slskd.Users
/// <summary>
/// Gets the name of the group for the specified <paramref name="username"/>.
/// </summary>
+ /// <remarks>
+ /// The group name is fetched from cached data, and lookups should always be fast.
+ /// </remarks>
/// <param name="username">The username of the peer.</param>
/// <returns>The group for the specified username.</returns>
string GetGroup(string username);
/// <summary>
+ /// Gets the name of the group for the specified <paramref name="username"/>.
+ /// </summary>
+ /// <remarks>
+ /// An alternative to <see cref="GetGroup(string)"/> that does not rely on the
+ /// internal cache. This method allows privilege and leech detection for users
+ /// that are not watched.
+ /// </remarks>
+ /// <param name="username">The username of the peer.</param>
+ /// <param name="status">The peer's status.</param>
+ /// <param name="statistics">The peer's statistics</param>
+ /// <returns>The group for the specified username.</returns>
+ string GetGroup(string username, Status status, Statistics statistics);
+
+ /// <summary>
/// Retrieves peer <see cref="Info"/>.
/// </summary>
/// <param name="username">The username of the peer.</param>
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
/// <summary>
/// Provides information and operations for network peers.
/// </summary>
+ /// <remarks>
+ /// The <see cref="WatchAsync(string)"/> method should be called to 'watch' users prior to any call to
+ /// <see cref="GetGroup(string)"/>. <see cref="GetGroup(string)"/> relies *only* on information stored in
+ /// the internal <see cref="UserDictionary"/>, and this dictionary is only populated for users that have been
+ /// watched. Prior to calling <see cref="WatchAsync(string)"/>, check <see cref="IsWatched(string)"/> to
+ /// reduce round trips to the server.
+ /// </remarks>
public class UserService : IUserService
{
/// <summary>
@@ -83,10 +90,34 @@ namespace slskd.Users
private ILogger Log { get; set; } = Serilog.Log.ForContext<UserService>();
private IOptionsMonitor<Options> OptionsMonitor { get; }
private IStateMutator<State> StateMutator { get; }
- private ConcurrentDictionary<string, User> UserDictionary { get; set; } = new ConcurrentDictionary<string, User>();
private ConcurrentDictionary<string, bool> WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary<string, bool>();
/// <summary>
+ /// Gets or sets the internal cache of User data.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// 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_.
+ /// </para>
+ /// <para>
+ /// 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.
+ /// </para>
+ /// <para>
+ /// 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).
+ /// </para>
+ /// </remarks>
+ private ConcurrentDictionary<string, User> UserDictionary { get; set; } = new ConcurrentDictionary<string, User>();
+
+ /// <summary>
/// Gets the name of the group for the specified <paramref name="username"/>.
/// </summary>
/// <param name="username">The username of the peer.</param>
@@ -198,12 +229,25 @@ namespace slskd.Users
/// <summary>
/// Adds the specified username to the server-side user list.
/// </summary>
+ /// <remarks>
+ /// <para>
+ /// Idempotent; if a user is already watched, subsequent calls will
+ /// only update their status and statistics.
+ /// </para>
+ /// <para>
+ /// 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.
+ /// </para>
+ /// </remarks>
/// <param name="username">The username of the peer.</param>
/// <returns>The operation context.</returns>
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)