aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-10-04 02:55:02 -0500
committerJP Dillingham <jp@dillingham.me>2026-10-04 02:55:02 -0500
commit06939533b71112ba6a8eb3b95f6b892a5c9505a4 (patch)
tree4a86765e3ce1ad23b25546b0f9a9765b990b5075
parent36e1090134526e94eaab729f44aca32f843ae1c3 (diff)
downloadslskd-06939533b71112ba6a8eb3b95f6b892a5c9505a4.tar.gz
get rid of GetGroupInfo, return group info in Forecast
-rw-r--r--src/slskd/Application.cs7
-rw-r--r--src/slskd/Transfers/Uploads/UploadQueue.cs46
-rw-r--r--tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs162
3 files changed, 174 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)
diff --git a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs
index 6174932d..e31d5711 100644
--- a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs
+++ b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs
@@ -890,6 +890,168 @@ namespace slskd.Tests.Unit.Transfers.Uploads
}
}
+ public class ForecastPosition
+ {
+ [Theory]
+ [InlineAutoData(QueueStrategy.RoundRobin)]
+ [InlineAutoData(QueueStrategy.FirstInFirstOut)]
+ public void Returns_Zero_If_Slot_Is_Available_Regardless_Of_Queue(QueueStrategy strategy, string username, string other)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, strategy);
+ SetSlotAvailable(queue, true);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other, CreateUploads(other, count: 10));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(0, queue.ForecastPosition(username).Position);
+ }
+
+ [Theory]
+ [InlineAutoData(QueueStrategy.RoundRobin)]
+ [InlineAutoData(QueueStrategy.FirstInFirstOut)]
+ public void Returns_One_If_No_Slot_Is_Available_And_Queue_Is_Empty(QueueStrategy strategy, string username)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, strategy);
+ SetSlotAvailable(queue, false);
+
+ Assert.Equal(1, queue.ForecastPosition(username).Position);
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_Returns_Number_Of_Users_In_Group_Plus_One(string username, string other1, string other2)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+ SetSlotAvailable(queue, false);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other1, CreateUploads(other1, count: 10));
+ uploads.TryAdd(other2, CreateUploads(other2, count: 1));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ // worst case; the new file is last in the rotation. file counts don't matter
+ Assert.Equal(3, queue.ForecastPosition(username).Position);
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_Counts_Requesting_User_Once_Regardless_Of_Their_Queued_Files(string username, string other)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+ SetSlotAvailable(queue, false);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(username, CreateUploads(username, count: 10));
+ uploads.TryAdd(other, CreateUploads(other, count: 1));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(3, queue.ForecastPosition(username).Position);
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_Ignores_Users_In_Other_Groups(string username, string other1, string other2)
+ {
+ var (queue, mocks) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+ SetSlotAvailable(queue, false);
+
+ mocks.UserService.Setup(m => m.GetGroup(other2)).Returns(Application.PrivilegedGroup);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other1, CreateUploads(other1, count: 1));
+ uploads.TryAdd(other2, CreateUploads(other2, count: 1));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(2, queue.ForecastPosition(username).Position);
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Returns_Number_Of_Uploads_In_Group_Plus_One(string username, string other1, string other2)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+ SetSlotAvailable(queue, false);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(username, CreateUploads(username, count: 2));
+ uploads.TryAdd(other1, CreateUploads(other1, count: 3));
+ uploads.TryAdd(other2, CreateUploads(other2, count: 4));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ // the new file goes to the back of the queue, behind every upload in the group, including the user's own
+ Assert.Equal(2 + 3 + 4 + 1, queue.ForecastPosition(username).Position);
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Counts_Uploads_In_Progress(string username, string other)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+ SetSlotAvailable(queue, false);
+
+ var started = CreateUploads(other, count: 2);
+ started.ForEach(u => u.Started = Now);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other, started);
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(3, queue.ForecastPosition(username).Position);
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Ignores_Users_In_Other_Groups(string username, string other1, string other2)
+ {
+ var (queue, mocks) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+ SetSlotAvailable(queue, false);
+
+ mocks.UserService.Setup(m => m.GetGroup(other2)).Returns(Application.PrivilegedGroup);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other1, CreateUploads(other1, count: 3));
+ uploads.TryAdd(other2, CreateUploads(other2, count: 10));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(4, queue.ForecastPosition(username).Position);
+ }
+
+ [Theory]
+ [InlineAutoData(QueueStrategy.RoundRobin)]
+ [InlineAutoData(QueueStrategy.FirstInFirstOut)]
+ public void Returns_Group_And_Slot_Counts_If_No_Slot_Is_Available(QueueStrategy strategy, string username)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, strategy);
+ SetSlotAvailable(queue, false);
+
+ var (group, totalSlots, freeSlots, _) = queue.ForecastPosition(username);
+
+ Assert.Equal(Application.DefaultGroup, group);
+ Assert.Equal(1, totalSlots);
+ Assert.Equal(0, freeSlots);
+ }
+ }
+
public class Process
{
[Fact]