From f98f2d9ae5807547ca612ede9da1047f2cd4a4d6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 6 Oct 2026 20:52:30 -0500 Subject: update enqueue to handle transfer param --- src/slskd/Transfers/Uploads/UploadQueue.cs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'src/slskd/Transfers/Uploads/UploadQueue.cs') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index 5839fa73..eb4a2cf3 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -77,9 +77,8 @@ namespace slskd.Transfers.Uploads /// /// Enqueues an upload. /// - /// The username of the remote user. - /// The filename to enqueue. - void Enqueue(string username, string filename); + /// The upload to enqueue. + void Enqueue(Transfer transfer); /// /// Computes the estimated queue position of the specified for the specified . @@ -269,18 +268,23 @@ namespace slskd.Transfers.Uploads /// /// Enqueues an upload. /// - /// The username of the remote user. - /// The filename to enqueue. - public void Enqueue(string username, string filename) + /// The upload to enqueue. + public void Enqueue(Transfer 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 +293,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 { -- cgit v1.2.3 From a4be259b2551a35b9af24aa1b1137b2131f9f6e0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 6 Oct 2026 20:54:34 -0500 Subject: update await for transfer param --- src/slskd/Transfers/Uploads/UploadQueue.cs | 32 +++++++++++------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'src/slskd/Transfers/Uploads/UploadQueue.cs') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index eb4a2cf3..8a196718 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -50,13 +50,12 @@ namespace slskd.Transfers.Uploads public interface IUploadQueue { /// - /// Awaits the start of an upload. + /// Awaits the start of an upload by returning a that is completed when the queue initiates it. /// - /// The username of the remote user. - /// The filename for which to await the start. + /// The transfer to await. /// The token to monitor for cancellation requests. /// The operation context. - Task AwaitStartAsync(string username, string filename, CancellationToken cancellationToken = default); + Task AwaitStartAsync(Transfer transfer, CancellationToken cancellationToken = default); /// /// Signals the completion of an upload. @@ -143,38 +142,31 @@ namespace slskd.Transfers.Uploads private IUserService Users { get; } /// - /// Returns a that, when complete, signals the underlying Soulseek.NET logic that - /// the 'wait for a free slot' permissive has been obtained. When the method is - /// ready, it will complete the Task and the upload will start. + /// Awaits the start of an upload by returning a that is completed when the queue initiates it. /// - /// - /// This is the mechanism we use to control the queue; the Task we return here is completed in the - /// method once we have determined that this transfer is next to go. - /// - /// The username of the remote user. - /// The filename for which to await the start. + /// The transfer to await. /// The token to monitor for cancellation requests. /// The operation context. - public Task AwaitStartAsync(string username, string filename, CancellationToken cancellationToken = default) + public Task AwaitStartAsync(Transfer transfer, CancellationToken cancellationToken = default) { - SyncRoot.Wait(); + 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; } -- cgit v1.2.3 From e3586822fd83b05dcd55321f0981de9bf46a1c6b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 6 Oct 2026 20:56:38 -0500 Subject: pass transfer to complete --- src/slskd/Transfers/Uploads/UploadQueue.cs | 40 +++++++++++++----------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'src/slskd/Transfers/Uploads/UploadQueue.cs') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index 8a196718..d699ace1 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -60,18 +60,16 @@ namespace slskd.Transfers.Uploads /// /// Signals the completion of an upload. /// - /// The username of the remote user. - /// The completed filename. - void Complete(string username, string filename); + /// The transfer to complete. + void Complete(Transfer transfer); /// /// Gracefully attempts to signal the completion of an upload, returning false if a problem is encountered /// (such as the upload not being tracked currently). /// - /// The username of the remote user. - /// The completed filename. + /// The transfer to complete. /// A value indicating whether a problem was encountered. - bool TryComplete(string username, string filename); + bool TryComplete(Transfer transfer); /// /// Enqueues an upload. @@ -181,14 +179,13 @@ 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). /// - /// The username of the remote user. - /// The completed filename. + /// The transfer to complete. /// A value indicating whether a problem was encountered. - public bool TryComplete(string username, string filename) + public bool TryComplete(Transfer transfer) { try { - Complete(username, filename); + Complete(transfer); return true; } catch (SlskdException) @@ -200,31 +197,28 @@ namespace slskd.Transfers.Uploads /// /// Signals the completion of an upload. /// - /// - /// Will not throw on repeated attempts. - /// - /// The username of the remote user. - /// The completed filename. - public void Complete(string username, string filename) + /// The transfer to complete. + public void Complete(Transfer 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)); @@ -239,13 +233,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(); -- cgit v1.2.3 From ae4f6941b1bc288faa73261c10d70e7cf80f80a2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 6 Oct 2026 20:57:10 -0500 Subject: UsedSlots is a Guid now --- src/slskd/Transfers/Uploads/UploadQueue.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Transfers/Uploads/UploadQueue.cs') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index d699ace1..8cf41dbd 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -438,7 +438,7 @@ namespace slskd.Transfers.Uploads private void Configure(Options options) { - HashSet<(string Username, string Filename)> GetExistingUsedSlotsOrDefault(string group) + HashSet GetExistingUsedSlotsOrDefault(string group) => Groups.ContainsKey(group) ? Groups[group].UsedSlots : []; SyncRoot.Wait(); @@ -567,7 +567,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(); -- cgit v1.2.3 From 87c97465f987496b94755e064028dc3d148226c3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 7 Oct 2026 09:20:46 -0500 Subject: pass transfer to estimate --- src/slskd/Transfers/Uploads/UploadQueue.cs | 36 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'src/slskd/Transfers/Uploads/UploadQueue.cs') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index 8cf41dbd..de5a613d 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -78,18 +78,18 @@ namespace slskd.Transfers.Uploads void Enqueue(Transfer transfer); /// - /// Computes the estimated queue position of the specified for the specified . + /// Computes the estimated queue position of the specified . /// /// /// 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. /// - /// The username associated with the file. - /// The filename of the file for which the position is to be estimated. + /// The Tranfer for which to estimate the position. /// The estimated queue position of the file. + /// Thrown if the specified Transfer is null. /// Thrown if the specified filename is not enqueued. - int EstimatePosition(string username, string filename); + int EstimatePosition(Transfer transfer); /// /// Computes the estimated queue position of the specified if they were to enqueue a file, @@ -289,20 +289,22 @@ namespace slskd.Transfers.Uploads } /// - /// Computes the estimated queue position of the specified for the specified . + /// Computes the estimated queue position of the specified . /// /// /// 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. /// - /// The username associated with the file. - /// The filename of the file for which the position is to be estimated. + /// The Tranfer for which to estimate the position. /// The estimated queue position of the file. + /// Thrown if the specified Transfer is null. /// Thrown if the specified filename is not enqueued. - 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)) { @@ -318,9 +320,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 @@ -332,11 +334,11 @@ namespace slskd.Transfers.Uploads // find the position of the requested file in the user's queue // note: backed by List, which is stable and already ordered by enqueue time ASC var localPosition = uploadsForUser - .FindIndex(u => u.Username == username && u.Filename == filename); + .FindIndex(u => u.Username == transfer.Username && u.Filename == transfer.Filename); 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 @@ -355,7 +357,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); } @@ -364,17 +366,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.Username == transfer.Username && u.Filename == transfer.Filename); 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 -- cgit v1.2.3 From cda4cd2a73ce6b9dfb27d8e1bec61005ca4e6195 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 7 Oct 2026 09:21:07 -0500 Subject: guard clauses and docs --- src/slskd/Transfers/Uploads/UploadQueue.cs | 38 +++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'src/slskd/Transfers/Uploads/UploadQueue.cs') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index de5a613d..6e7e73df 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -52,29 +52,33 @@ namespace slskd.Transfers.Uploads /// /// Awaits the start of an upload by returning a that is completed when the queue initiates it. /// - /// The transfer to await. + /// The Transfer to await. /// The token to monitor for cancellation requests. /// The operation context. + /// Thrown if the specified Transfer is null. Task AwaitStartAsync(Transfer transfer, CancellationToken cancellationToken = default); /// /// Signals the completion of an upload. /// - /// The transfer to complete. + /// The Transfer to complete. + /// Thrown if the specified Transfer is null. void Complete(Transfer transfer); /// /// Gracefully attempts to signal the completion of an upload, returning false if a problem is encountered /// (such as the upload not being tracked currently). /// - /// The transfer to complete. - /// A value indicating whether a problem was encountered. + /// The Transfer to complete. + /// A value indicating whether a problem was encountered. + /// Thrown if the specified Transfer is null. bool TryComplete(Transfer transfer); /// /// Enqueues an upload. /// - /// The upload to enqueue. + /// The Transfer to enqueue. + /// Thrown if the specified Transfer is null. void Enqueue(Transfer transfer); /// @@ -104,6 +108,7 @@ namespace slskd.Transfers.Uploads /// /// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file. /// + /// Thrown if the specified username is null or consists only of whitespace. (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username); } @@ -142,11 +147,14 @@ namespace slskd.Transfers.Uploads /// /// Awaits the start of an upload by returning a that is completed when the queue initiates it. /// - /// The transfer to await. + /// The Transfer to await. /// The token to monitor for cancellation requests. /// The operation context. + /// Thrown if the specified Transfer is null. public Task AwaitStartAsync(Transfer transfer, CancellationToken cancellationToken = default) { + ArgumentNullException.ThrowIfNull(transfer); + SyncRoot.Wait(cancellationToken); try @@ -179,10 +187,13 @@ 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). /// - /// The transfer to complete. + /// The Transfer to complete. /// A value indicating whether a problem was encountered. + /// Thrown if the specified Transfer is null. public bool TryComplete(Transfer transfer) { + ArgumentNullException.ThrowIfNull(transfer); + try { Complete(transfer); @@ -197,9 +208,12 @@ namespace slskd.Transfers.Uploads /// /// Signals the completion of an upload. /// - /// The transfer to complete. + /// The Transfer to complete. + /// Thrown if the specified Transfer is null. public void Complete(Transfer transfer) { + ArgumentNullException.ThrowIfNull(transfer); + SyncRoot.Wait(); try @@ -254,9 +268,12 @@ namespace slskd.Transfers.Uploads /// /// Enqueues an upload. /// - /// The upload to enqueue. + /// The Transfer to enqueue. + /// Thrown if the specified Transfer is null. public void Enqueue(Transfer transfer) { + ArgumentNullException.ThrowIfNull(transfer); + SyncRoot.Wait(); try @@ -404,8 +421,11 @@ namespace slskd.Transfers.Uploads /// /// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file. /// + /// Thrown if the specified username is null or consists only of whitespace. 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)) -- cgit v1.2.3 From 7c9e5e025a7ebb716dd6321370f035f2a223f8fe Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 7 Oct 2026 09:51:15 -0500 Subject: straggling lookup --- src/slskd/Transfers/Uploads/UploadQueue.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Transfers/Uploads/UploadQueue.cs') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index 6e7e73df..9aff5195 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -351,7 +351,7 @@ namespace slskd.Transfers.Uploads // find the position of the requested file in the user's queue // note: backed by List, which is stable and already ordered by enqueue time ASC var localPosition = uploadsForUser - .FindIndex(u => u.Username == transfer.Username && u.Filename == transfer.Filename); + .FindIndex(u => u.Id == transfer.Id); if (localPosition < 0) { @@ -389,7 +389,7 @@ namespace slskd.Transfers.Uploads } // find the upload - var upload = uploadsForUser.SingleOrDefault(u => u.Username == transfer.Username && u.Filename == transfer.Filename); + var upload = uploadsForUser.SingleOrDefault(u => u.Id == transfer.Id); if (upload is null) { -- cgit v1.2.3