aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-10-07 11:04:04 -0500
committerGitHub <noreply@github.com>2026-10-07 11:04:04 -0500
commit7974ede205de6ce85a45112c5fa901bfa4aa7ece (patch)
tree20a8e547aa428b6b0ad8ecb26c509e121177c543 /src
parent7c302a753b1814345fcb26eb78dec97db6b698df (diff)
parentf503bdbfbdd9d02e142b61ed3b33542d5dc291c5 (diff)
downloadslskd-7974ede205de6ce85a45112c5fa901bfa4aa7ece.tar.gz
Merge pull request #1871 from slskd/upload-queue-fixes
Organize upload queue by transfer Id instead of username/password
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs11
-rw-r--r--src/slskd/Transfers/Types/Upload.cs7
-rw-r--r--src/slskd/Transfers/Types/UploadGroup.cs7
-rw-r--r--src/slskd/Transfers/Uploads/UploadQueue.cs156
-rw-r--r--src/slskd/Transfers/Uploads/UploadService.cs8
5 files changed, 105 insertions, 84 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index c34cd316..506b1980 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -1408,10 +1408,17 @@ namespace slskd
try
{
- var place = Transfers.Uploads.Queue.EstimatePosition(username, filename);
+ var upload = Transfers.Uploads.Find(u => u.Username == username && u.Filename == filename);
+
+ if (upload is null) // transfer doesn't exist at all
+ {
+ return Task.FromResult<int?>(null);
+ }
+
+ var place = Transfers.Uploads.Queue.EstimatePosition(upload);
return Task.FromResult((int?)place);
}
- catch (NotFoundException)
+ catch (NotFoundException) // transfer is not in the queue
{
return Task.FromResult<int?>(null);
}
diff --git a/src/slskd/Transfers/Types/Upload.cs b/src/slskd/Transfers/Types/Upload.cs
index 1e05a88a..ab209cff 100644
--- a/src/slskd/Transfers/Types/Upload.cs
+++ b/src/slskd/Transfers/Types/Upload.cs
@@ -37,12 +37,13 @@ namespace slskd.Transfers
public sealed class Upload
{
- public DateTime Enqueued { get; set; } = DateTime.UtcNow;
- public string Filename { get; set; }
+ public required Guid Id { get; init; }
+ public required DateTime Enqueued { get; init; }
+ public required string Filename { get; init; }
public string Group { get; set; }
public DateTime? Ready { get; set; } = null;
public DateTime? Started { get; set; } = null;
public TaskCompletionSource TaskCompletionSource { get; set; } = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
- public string Username { get; set; }
+ public required string Username { get; init; }
}
} \ No newline at end of file
diff --git a/src/slskd/Transfers/Types/UploadGroup.cs b/src/slskd/Transfers/Types/UploadGroup.cs
index 0b2b617d..0a142c9f 100644
--- a/src/slskd/Transfers/Types/UploadGroup.cs
+++ b/src/slskd/Transfers/Types/UploadGroup.cs
@@ -30,10 +30,11 @@
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
// </copyright>
-using System.Collections.Generic;
-
namespace slskd.Transfers
{
+ using System;
+ using System.Collections.Generic;
+
public sealed record UploadGroup
{
public string Name { get; init; }
@@ -41,6 +42,6 @@ namespace slskd.Transfers
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; }
+ public HashSet<Guid> UsedSlots { get; set; }
}
}
diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs
index 5839fa73..9aff5195 100644
--- a/src/slskd/Transfers/Uploads/UploadQueue.cs
+++ b/src/slskd/Transfers/Uploads/UploadQueue.cs
@@ -50,50 +50,50 @@ namespace slskd.Transfers.Uploads
public interface IUploadQueue
{
/// <summary>
- /// Awaits the start of an upload.
+ /// Awaits the start of an upload by returning a <see cref="Task"/> that is completed when the queue initiates it.
/// </summary>
- /// <param name="username">The username of the remote user.</param>
- /// <param name="filename">The filename for which to await the start.</param>
+ /// <param name="transfer">The Transfer to await.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>The operation context.</returns>
- Task AwaitStartAsync(string username, string filename, CancellationToken cancellationToken = default);
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
+ Task AwaitStartAsync(Transfer transfer, CancellationToken cancellationToken = default);
/// <summary>
/// Signals the completion of an upload.
/// </summary>
- /// <param name="username">The username of the remote user.</param>
- /// <param name="filename">The completed filename.</param>
- void Complete(string username, string filename);
+ /// <param name="transfer">The Transfer to complete.</param>
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
+ void Complete(Transfer transfer);
/// <summary>
/// Gracefully attempts to signal the completion of an upload, returning false if a problem is encountered
/// (such as the upload not being tracked currently).
/// </summary>
- /// <param name="username">The username of the remote user.</param>
- /// <param name="filename">The completed filename.</param>
- /// <returns>A value indicating whether a problem was encountered.</returns>
- bool TryComplete(string username, string filename);
+ /// <param name="transfer">The Transfer to complete.</param>
+ /// <returns>A value indicating whether a problem was encountered.</return>
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
+ bool TryComplete(Transfer transfer);
/// <summary>
/// Enqueues an upload.
/// </summary>
- /// <param name="username">The username of the remote user.</param>
- /// <param name="filename">The filename to enqueue.</param>
- void Enqueue(string username, string filename);
+ /// <param name="transfer">The Transfer to enqueue.</param>
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
+ void Enqueue(Transfer transfer);
/// <summary>
- /// Computes the estimated queue position of the specified <paramref name="filename"/> for the specified <paramref name="username"/>.
+ /// Computes the estimated queue position of the specified <paramref name="transfer"/>.
/// </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 associated with the file.</param>
- /// <param name="filename">The filename of the file for which the position is to be estimated.</param>
+ /// <param name="transfer">The Tranfer for which to estimate the position.</param>
/// <returns>The estimated queue position of the file.</returns>
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
/// <exception cref="NotFoundException">Thrown if the specified filename is not enqueued.</exception>
- int EstimatePosition(string username, string filename);
+ int EstimatePosition(Transfer transfer);
/// <summary>
/// Computes the estimated queue position of the specified <paramref name="username"/> if they were to enqueue a file,
@@ -108,6 +108,7 @@ namespace slskd.Transfers.Uploads
/// <returns>
/// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file.
/// </returns>
+ /// <exception cref="ArgumentException">Thrown if the specified username is null or consists only of whitespace.</exception>
(string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username);
}
@@ -144,38 +145,34 @@ namespace slskd.Transfers.Uploads
private IUserService Users { get; }
/// <summary>
- /// Returns a <see cref="Task"/> that, when complete, signals the underlying Soulseek.NET logic that
- /// the 'wait for a free slot' permissive has been obtained. When the <see cref="Process"/> method is
- /// ready, it will complete the Task and the upload will start.
+ /// Awaits the start of an upload by returning a <see cref="Task"/> that is completed when the queue initiates it.
/// </summary>
- /// <remarks>
- /// This is the mechanism we use to control the queue; the Task we return here is completed in the
- /// <see cref="Process"/> method once we have determined that this transfer is next to go.
- /// </remarks>
- /// <param name="username">The username of the remote user.</param>
- /// <param name="filename">The filename for which to await the start.</param>
+ /// <param name="transfer">The Transfer to await.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>The operation context.</returns>
- public Task AwaitStartAsync(string username, string filename, CancellationToken cancellationToken = default)
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
+ public Task AwaitStartAsync(Transfer transfer, CancellationToken cancellationToken = default)
{
- SyncRoot.Wait();
+ ArgumentNullException.ThrowIfNull(transfer);
+
+ SyncRoot.Wait(cancellationToken);
try
{
- if (!UploadDictionary.TryGetValue(username, out var list))
+ if (!UploadDictionary.TryGetValue(transfer.Username, out var list))
{
- throw new SlskdException($"No enqueued uploads for user {username}");
+ throw new SlskdException($"No enqueued uploads for user {transfer.Username}");
}
- var upload = list.FirstOrDefault(e => e.Filename == filename);
+ var upload = list.FirstOrDefault(e => e.Id == transfer.Id);
if (upload == default)
{
- throw new SlskdException($"File {filename} is not enqueued for user {username}");
+ throw new SlskdException($"Transfer {transfer.Id} is not enqueued for user {transfer.Username}");
}
upload.Ready = DateTime.UtcNow;
- Log.Debug("Ready: {File} for {User} at {Time}", Path.GetFileName(upload.Filename), upload.Username, upload.Enqueued);
+ Log.Debug("Ready: {File} for {User} at {Time} (id: {Id})", Path.GetFileName(upload.Filename), upload.Username, upload.Enqueued, upload.Id);
return upload.TaskCompletionSource.Task;
}
@@ -190,14 +187,16 @@ namespace slskd.Transfers.Uploads
/// Gracefully attempts to signal the completion of an upload, returning false if a problem is encountered
/// (such as the upload not being tracked currently).
/// </summary>
- /// <param name="username">The username of the remote user.</param>
- /// <param name="filename">The completed filename.</param>
+ /// <param name="transfer">The Transfer to complete.</param>
/// <returns>A value indicating whether a problem was encountered.</returns>
- public bool TryComplete(string username, string filename)
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
+ public bool TryComplete(Transfer transfer)
{
+ ArgumentNullException.ThrowIfNull(transfer);
+
try
{
- Complete(username, filename);
+ Complete(transfer);
return true;
}
catch (SlskdException)
@@ -209,31 +208,31 @@ namespace slskd.Transfers.Uploads
/// <summary>
/// Signals the completion of an upload.
/// </summary>
- /// <remarks>
- /// Will not throw on repeated attempts.
- /// </remarks>
- /// <param name="username">The username of the remote user.</param>
- /// <param name="filename">The completed filename.</param>
- public void Complete(string username, string filename)
+ /// <param name="transfer">The Transfer to complete.</param>
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
+ public void Complete(Transfer transfer)
{
+ ArgumentNullException.ThrowIfNull(transfer);
+
SyncRoot.Wait();
try
{
- if (!UploadDictionary.TryGetValue(username, out var list))
+ if (!UploadDictionary.TryGetValue(transfer.Username, out var list))
{
- throw new SlskdException($"No enqueued uploads for user {username}");
+ throw new SlskdException($"No enqueued uploads for user {transfer.Username}");
}
- var upload = list.FirstOrDefault(e => e.Filename == filename);
+ var upload = list.FirstOrDefault(e => e.Id == transfer.Id);
if (upload == default)
{
- throw new SlskdException($"File {filename} is not enqueued for user {username}");
+ throw new SlskdException($"Transfer {transfer.Id} is not enqueued for user {transfer.Username}");
}
list.Remove(upload);
+ // todo: deprecate this with improved cancellation handling; it shouldn't be needed
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));
@@ -248,13 +247,13 @@ namespace slskd.Transfers.Uploads
{
var group = Groups[upload.Group];
- group.UsedSlots.Remove((username, filename));
+ group.UsedSlots.Remove(transfer.Id);
Log.Debug("Group {Group} slots: {Used}/{Available}", group.Name, group.UsedSlots.Count, group.Slots);
}
- if (list.Count == 0 && UploadDictionary.TryRemove(username, out _))
+ if (list.Count == 0 && UploadDictionary.TryRemove(transfer.Username, out _))
{
- Log.Debug("Cleaned up tracking list for {User}; no more queued uploads to track", username);
+ Log.Debug("Cleaned up tracking list for {User}; no more queued uploads to track", transfer.Username);
}
EmitMetrics();
@@ -269,18 +268,26 @@ namespace slskd.Transfers.Uploads
/// <summary>
/// Enqueues an upload.
/// </summary>
- /// <param name="username">The username of the remote user.</param>
- /// <param name="filename">The filename to enqueue.</param>
- public void Enqueue(string username, string filename)
+ /// <param name="transfer">The Transfer to enqueue.</param>
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
+ public void Enqueue(Transfer transfer)
{
+ ArgumentNullException.ThrowIfNull(transfer);
+
SyncRoot.Wait();
try
{
- var upload = new Upload() { Username = username, Filename = filename, Enqueued = DateTime.UtcNow };
+ var upload = new Upload()
+ {
+ Id = transfer.Id,
+ Username = transfer.Username,
+ Filename = transfer.Filename,
+ Enqueued = DateTime.UtcNow,
+ };
UploadDictionary.AddOrUpdate(
- key: username,
+ key: transfer.Username,
addValue: [upload],
updateValueFactory: (key, list) =>
{
@@ -289,7 +296,7 @@ namespace slskd.Transfers.Uploads
});
EmitMetrics();
- Log.Debug("Enqueued: {File} for {User} at {Time}", Path.GetFileName(upload.Filename), upload.Username, upload.Enqueued);
+ Log.Debug("Enqueued: {File} for {User} at {Time} (id: {Id})", Path.GetFileName(upload.Filename), upload.Username, upload.Enqueued, upload.Id);
}
finally
{
@@ -299,20 +306,22 @@ namespace slskd.Transfers.Uploads
}
/// <summary>
- /// Computes the estimated queue position of the specified <paramref name="filename"/> for the specified <paramref name="username"/>.
+ /// Computes the estimated queue position of the specified <paramref name="transfer"/>.
/// </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 associated with the file.</param>
- /// <param name="filename">The filename of the file for which the position is to be estimated.</param>
+ /// <param name="transfer">The Tranfer for which to estimate the position.</param>
/// <returns>The estimated queue position of the file.</returns>
+ /// <exception cref="ArgumentNullException">Thrown if the specified Transfer is null.</exception>
/// <exception cref="NotFoundException">Thrown if the specified filename is not enqueued.</exception>
- public int EstimatePosition(string username, string filename)
+ public int EstimatePosition(Transfer transfer)
{
- var groupName = Users.GetGroup(username);
+ ArgumentNullException.ThrowIfNull(transfer);
+
+ var groupName = Users.GetGroup(transfer.Username);
if (!Groups.TryGetValue(groupName, out var groupRecord))
{
@@ -328,9 +337,9 @@ namespace slskd.Transfers.Uploads
try
{
// find this user's uploads
- if (!UploadDictionary.TryGetValue(username, out var uploadsForUser))
+ if (!UploadDictionary.TryGetValue(transfer.Username, out var uploadsForUser))
{
- throw new NotFoundException($"File {filename} is not enqueued for user {username}");
+ throw new NotFoundException($"File {transfer.Filename} is not enqueued for user {transfer.Username}");
}
// the RoundRobin queue implementation is not strictly fair to all users; only uploads that are ready are candidates
@@ -342,11 +351,11 @@ namespace slskd.Transfers.Uploads
// find the position of the requested file in the user's queue
// note: backed by List<T>, which is stable and already ordered by enqueue time ASC
var localPosition = uploadsForUser
- .FindIndex(u => u.Username == username && u.Filename == filename);
+ .FindIndex(u => u.Id == transfer.Id);
if (localPosition < 0)
{
- throw new NotFoundException($"File {filename} is not enqueued for user {username}");
+ throw new NotFoundException($"File {transfer.Filename} is not enqueued for user {transfer.Username}");
}
// start the position to the local position within this user's queue; the user's own files must be completed
@@ -365,7 +374,7 @@ namespace slskd.Transfers.Uploads
// if we want the position of the file over the carat above, first find the position of it
// within its own queue (= 4). assume uploads will process top down, left to right until reaching
// this one. that's the 4 ahead of it from d, plus 4 from a, 2 from b, and 4 from c in earlier rounds (= 14).
- foreach (var group in uploadsForGroup.Where(group => group.Key != username))
+ foreach (var group in uploadsForGroup.Where(group => group.Key != transfer.Username))
{
position += Math.Min(localPosition, group.Value.Count);
}
@@ -374,17 +383,17 @@ namespace slskd.Transfers.Uploads
// goes first. in the example, a and c have a 5th file and b doesn't, putting the file over the carat
// at position 16.
var usersWithAtLeastAsManyFiles = uploadsForGroup
- .Count(g => g.Key != username && g.Value.Count() >= localPosition + 1); // localPosition is zero-based
+ .Count(g => g.Key != transfer.Username && g.Value.Count() >= localPosition + 1); // localPosition is zero-based
return position + usersWithAtLeastAsManyFiles;
}
// find the upload
- var upload = uploadsForUser.SingleOrDefault(u => u.Username == username && u.Filename == filename);
+ var upload = uploadsForUser.SingleOrDefault(u => u.Id == transfer.Id);
if (upload is null)
{
- throw new NotFoundException($"File {filename} is not enqueued for user {username}");
+ throw new NotFoundException($"File {transfer.Filename} is not enqueued for user {transfer.Username}");
}
// the place in queue is simply the sum of all uploads across all other users in the group that were
@@ -412,8 +421,11 @@ namespace slskd.Transfers.Uploads
/// <returns>
/// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file.
/// </returns>
+ /// <exception cref="ArgumentException">Thrown if the specified username is null or consists only of whitespace.</exception>
public (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username)
{
+ ArgumentException.ThrowIfNullOrWhiteSpace(username);
+
var groupName = Users.GetGroup(username);
if (!Groups.TryGetValue(groupName, out var groupRecord))
@@ -448,7 +460,7 @@ namespace slskd.Transfers.Uploads
private void Configure(Options options)
{
- HashSet<(string Username, string Filename)> GetExistingUsedSlotsOrDefault(string group)
+ HashSet<Guid> GetExistingUsedSlotsOrDefault(string group)
=> Groups.ContainsKey(group) ? Groups[group].UsedSlots : [];
SyncRoot.Wait();
@@ -577,7 +589,7 @@ namespace slskd.Transfers.Uploads
// returned to the proper place upon completion
upload.Started = DateTime.UtcNow;
upload.Group = group.Name;
- group.UsedSlots.Add((upload.Username, upload.Filename));
+ group.UsedSlots.Add(upload.Id);
// release the upload
upload.TaskCompletionSource.SetResult();
diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs
index b480e764..bda47641 100644
--- a/src/slskd/Transfers/Uploads/UploadService.cs
+++ b/src/slskd/Transfers/Uploads/UploadService.cs
@@ -429,14 +429,14 @@ namespace slskd.Transfers.Uploads
disposeInputStreamOnCompletion: true, // note: don't set this to false!
governor: (tx, req, ct) => Governor.GetBytesAsync(tx.Username, req, ct),
reporter: (tx, att, grant, act) => Governor.ReturnBytes(tx.Username, att, grant, act),
- slotAwaiter: (tx, ct) => Queue.AwaitStartAsync(tx.Username, tx.Filename, ct),
- slotReleased: (tx) => Queue.Complete(tx.Username, tx.Filename));
+ slotAwaiter: (tx, ct) => Queue.AwaitStartAsync(transfer, ct),
+ slotReleased: (tx) => Queue.Complete(transfer));
// register the cancellation token
CancellationTokens.TryAdd(transfer.Id, cts);
// add the transfer to the UploadQueue so that it can become eligible for selection
- Queue.Enqueue(transfer.Username, transfer.Filename);
+ Queue.Enqueue(transfer);
transfer.EnqueuedAt = DateTime.UtcNow;
SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: cts.Token);
@@ -598,7 +598,7 @@ namespace slskd.Transfers.Uploads
// if for some reason this logic exits without the slotReleased delegate and Complete() being invoked,
// the file will get stuck in the queue and prevent any further uploads to the user. be extra cautious
// and ensure it gets removed
- Queue.TryComplete(username: transfer.Username, filename: transfer.Filename);
+ Queue.TryComplete(transfer);
}
catch (Exception ex)
{