diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-10-04 02:55:02 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-10-04 02:55:02 -0500 |
| commit | 06939533b71112ba6a8eb3b95f6b892a5c9505a4 (patch) | |
| tree | 4a86765e3ce1ad23b25546b0f9a9765b990b5075 /src | |
| parent | 36e1090134526e94eaab729f44aca32f843ae1c3 (diff) | |
| download | slskd-06939533b71112ba6a8eb3b95f6b892a5c9505a4.tar.gz | |
get rid of GetGroupInfo, return group info in Forecast
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 7 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadQueue.cs | 46 |
2 files changed, 12 insertions, 41 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 8de4fe53..f8e92bc2 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1749,7 +1749,7 @@ namespace slskd // note: the following uses cached user data to determine group, so if the user's data // isn't cached they may get a forecast based on the wrong group. this is a hot path though, // and we don't want to incur the massive penalties that would caching data for each request. - var forecastedPosition = Transfers.Uploads.Queue.ForecastPosition(username); + var (groupName, totalSlots, freeSlots, forecastedPosition) = Transfers.Uploads.Queue.ForecastPosition(username); Log.Debug("Sending search response with {Count} files to {Username} for query '{Query}'", results.Count(), username, query.SearchText); @@ -1957,12 +1957,11 @@ namespace slskd // we are deliberately skipping it here; if the username is watched // leech detection works and they get accurate info, if not, they won't var groupName = await Users.GetOrFetchGroupAsync(username); - var group = Transfers.Uploads.Queue.GetGroupInfo(groupName); // forecast the position at which this user would enter the queue if they were to request // a file at this moment. this will be zero if a slot is available and the transfer would // begin immediately - var forecastedPosition = Transfers.Uploads.Queue.ForecastPosition(username); + var (group, totalSlots, freeSlots, forecastedPosition) = Transfers.Uploads.Queue.ForecastPosition(username); // if i get a user's info to determine whether i want to download files from them, // i want to know how many slots they have, which gives me an idea of how fast their @@ -1971,7 +1970,7 @@ namespace slskd // revisited 3 years later: why was it important to leave this comment?? var info = new UserInfo( description: Options.Soulseek.Description, - uploadSlots: group.Slots, + uploadSlots: totalSlots, queueLength: forecastedPosition, hasFreeUploadSlot: forecastedPosition == 0, picture: pictureBytes); diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index b7db1c42..9fc48497 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -82,13 +82,6 @@ namespace slskd.Transfers.Uploads void Enqueue(string username, string filename); /// <summary> - /// Gets information about the specified <paramref name="groupName"/>. - /// </summary> - /// <param name="groupName">The name of the group.</param> - /// <returns>The group information.</returns> - UploadGroup GetGroupInfo(string groupName); - - /// <summary> /// Computes the estimated queue position of the specified <paramref name="filename"/> for the specified <paramref name="username"/>. /// </summary> /// <remarks> @@ -110,7 +103,7 @@ namespace slskd.Transfers.Uploads /// <returns> /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. /// </returns> - int ForecastPosition(string username); + (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username); } /// <summary> @@ -294,30 +287,6 @@ namespace slskd.Transfers.Uploads } /// <summary> - /// Gets information about the specified <paramref name="groupName"/>. - /// </summary> - /// <param name="groupName">The name of the group.</param> - /// <returns>The group information.</returns> - public UploadGroup GetGroupInfo(string groupName) - { - if (Groups.TryGetValue(groupName, out var group)) - { - SyncRoot.Wait(); - - try - { - return group with { UsedSlots = [.. group.UsedSlots] }; - } - finally - { - SyncRoot.Release(); - } - } - - throw new NotFoundException($"A group with the name {groupName} could not be found"); - } - - /// <summary> /// Computes the estimated queue position of the specified <paramref name="filename"/> for the specified <paramref name="username"/>. /// </summary> /// <remarks> @@ -422,16 +391,19 @@ namespace slskd.Transfers.Uploads /// <returns> /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. /// </returns> - public int ForecastPosition(string username) + public (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string 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) + if (Groups.TryGetValue(groupName, out var groupRecord) && groupRecord.HasAvailableSlot) { - return 0; + return (string.Empty, 0, 0, 0); } + var totalSlots = groupRecord.Slots; + var freeSlots = totalSlots - groupRecord.UsedSlots.Count; + // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user // a user's group can change either by user changing the config or updating counts (for leech detection); resist the urge to cache this var uploadsForGroup = UploadDictionary.Where(kvp => Users.GetGroup(kvp.Key) == groupName); @@ -445,12 +417,12 @@ namespace slskd.Transfers.Uploads // rotation (worst case), the user's start position will be equal to the number of users downloading or waiting, + 1. if (groupRecord.Strategy == QueueStrategy.RoundRobin) { - return uploadsForGroup.Count() + 1; + return (groupName, totalSlots, freeSlots, uploadsForGroup.Count() + 1); } // for FIFO queues, the user will enter the queue at the very back. return the total number of uploads in progress and // enqueued, + 1. - return uploadsForGroup.Sum(kvp => kvp.Value.Count) + 1; + return (groupName, totalSlots, freeSlots, uploadsForGroup.Sum(kvp => kvp.Value.Count) + 1); } private void Configure(Options options) |
