aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Transfers
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-10-05 10:10:55 -0500
committerGitHub <noreply@github.com>2026-10-05 10:10:55 -0500
commit7c302a753b1814345fcb26eb78dec97db6b698df (patch)
tree54bc206b20410009c32a2f8c9aa4579b50ecec78 /src/slskd/Transfers
parent2d432e52cbe7e8483783010fe3f8f1415245c1a5 (diff)
parentb0ca0504c8c13bbe73401ac00d16e13d5a4fda70 (diff)
downloadslskd-7c302a753b1814345fcb26eb78dec97db6b698df.tar.gz
Merge pull request #1869 from slskd/upload-queue-fixes
Remove GetGroupInfo, return slot info in ForecastPosition instead
Diffstat (limited to 'src/slskd/Transfers')
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs2
-rw-r--r--src/slskd/Transfers/Types/Upload.cs2
-rw-r--r--src/slskd/Transfers/Types/UploadGroup.cs2
-rw-r--r--src/slskd/Transfers/Uploads/UploadQueue.cs91
4 files changed, 45 insertions, 52 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs
index 30f3c9b1..cb9423bc 100644
--- a/src/slskd/Transfers/Downloads/DownloadService.cs
+++ b/src/slskd/Transfers/Downloads/DownloadService.cs
@@ -509,7 +509,7 @@ namespace slskd.Transfers.Downloads
add this to the dictionary before inserting the record, so we are guaranteed to have it
in the right place once the transfer hits the UI
*/
- var enqueuedTcs = new TaskCompletionSource<Transfer>();
+ var enqueuedTcs = new TaskCompletionSource<Transfer>(TaskCreationOptions.RunContinuationsAsynchronously);
// satisfies condition #3; CancellationTokenSource set cancelled by the user (via API call)
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
diff --git a/src/slskd/Transfers/Types/Upload.cs b/src/slskd/Transfers/Types/Upload.cs
index 2e9dc8ef..1e05a88a 100644
--- a/src/slskd/Transfers/Types/Upload.cs
+++ b/src/slskd/Transfers/Types/Upload.cs
@@ -42,7 +42,7 @@ namespace slskd.Transfers
public string Group { get; set; }
public DateTime? Ready { get; set; } = null;
public DateTime? Started { get; set; } = null;
- public TaskCompletionSource TaskCompletionSource { get; set; } = new TaskCompletionSource();
+ public TaskCompletionSource TaskCompletionSource { get; set; } = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
public string Username { get; set; }
}
} \ No newline at end of file
diff --git a/src/slskd/Transfers/Types/UploadGroup.cs b/src/slskd/Transfers/Types/UploadGroup.cs
index e416dd00..0b2b617d 100644
--- a/src/slskd/Transfers/Types/UploadGroup.cs
+++ b/src/slskd/Transfers/Types/UploadGroup.cs
@@ -38,7 +38,7 @@ namespace slskd.Transfers
{
public string Name { get; init; }
public int Priority { get; set; }
- public bool SlotAvailable => UsedSlots.Count < Slots;
+ public bool HasAvailableSlot => UsedSlots.Count < Slots;
public int Slots { get; set; }
public QueueStrategy Strategy { get; set; }
public HashSet<(string Username, string Filename)> UsedSlots { get; set; }
diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs
index b7db1c42..5839fa73 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>
@@ -104,13 +97,18 @@ namespace slskd.Transfers.Uploads
/// <summary>
/// Computes the estimated queue position of the specified <paramref name="username"/> if they were to enqueue a file,
- /// or zero if the transfer could start immediately.
+ /// along with the slot counts for the user's group.
/// </summary>
+ /// <remarks>
+ /// The returned position is relative to the user's group only. Higher priority groups are not factored in
+ /// because of the amount of data that would need to be processed to compute a number, and how variable it
+ /// would be due to the interplay of slot availability, number of higher priority users waiting, etc.
+ /// </remarks>
/// <param name="username">The username for which to estimate.</param>
/// <returns>
- /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately.
+ /// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file.
/// </returns>
- int ForecastPosition(string username);
+ (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username);
}
/// <summary>
@@ -235,6 +233,13 @@ namespace slskd.Transfers.Uploads
}
list.Remove(upload);
+
+ if (!upload.TaskCompletionSource.Task.IsCompleted)
+ {
+ Log.Debug("Upload {File} for {User} was removed without being completed, so it has been cancelled", Path.GetFileName(upload.Filename));
+ upload.TaskCompletionSource.TrySetCanceled();
+ }
+
Log.Debug("Complete: {File} for {User} at {Time}", Path.GetFileName(upload.Filename), upload.Username, upload.Enqueued);
// ensure the slot is returned to the group from which it was acquired the group may have been removed during the
@@ -294,30 +299,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>
@@ -332,7 +313,11 @@ namespace slskd.Transfers.Uploads
public int EstimatePosition(string username, string filename)
{
var groupName = Users.GetGroup(username);
- var groupRecord = Groups.GetValueOrDefault(groupName);
+
+ if (!Groups.TryGetValue(groupName, out var groupRecord))
+ {
+ throw new SlskdException($"Upload group {groupName} doesn't have an entry in upload dictionary. Please report this on GitHub: {Program.IssuesUrl}");
+ }
// 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
@@ -416,41 +401,49 @@ namespace slskd.Transfers.Uploads
/// <summary>
/// Computes the estimated queue position of the specified <paramref name="username"/> if they were to enqueue a file,
- /// or zero if the transfer could start immediately.
+ /// along with the slot counts for the user's group.
/// </summary>
+ /// <remarks>
+ /// The returned position is relative to the user's group only. Higher priority groups are not factored in
+ /// because of the amount of data that would need to be processed to compute a number, and how variable it
+ /// would be due to the interplay of slot availability, number of higher priority users waiting, etc.
+ /// </remarks>
/// <param name="username">The username for which to estimate.</param>
/// <returns>
- /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately.
+ /// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file.
/// </returns>
- public int ForecastPosition(string username)
+ public (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username)
{
var groupName = Users.GetGroup(username);
+ if (!Groups.TryGetValue(groupName, out var groupRecord))
+ {
+ throw new SlskdException($"Upload group {groupName} doesn't have an entry in upload dictionary. Please report this on GitHub: {Program.IssuesUrl}");
+ }
+
+ var totalSlots = Math.Min(GlobalSlots, groupRecord.Slots);
+ var freeSlots = Math.Max(0, totalSlots - groupRecord.UsedSlots.Count);
+
// 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 (freeSlots > 0)
{
- return 0;
+ return (groupName, totalSlots, freeSlots, 0);
}
// 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);
- if (groupRecord is null)
- {
- throw new SlskdException($"Upload group {groupName} doesn't have an entry in upload dictionary. Please report this on GitHub: {Program.IssuesUrl}");
- }
-
// 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.
+ // rotation (worst case), the user's start position will be equal to the number of users downloading or waiting.
if (groupRecord.Strategy == QueueStrategy.RoundRobin)
{
- return uploadsForGroup.Count() + 1;
+ return (groupName, totalSlots, freeSlots, uploadsForGroup.Count());
}
// 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;
+ // enqueued.
+ return (groupName, totalSlots, freeSlots, uploadsForGroup.Sum(kvp => kvp.Value.Count));
}
private void Configure(Options options)