diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 11 | ||||
| -rw-r--r-- | src/slskd/Common/TokenBucket.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Transfers/Types/Upload.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Transfers/Types/UploadGroup.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadQueue.cs | 91 |
6 files changed, 52 insertions, 60 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 8de4fe53..c34cd316 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); @@ -1757,7 +1757,7 @@ namespace slskd Client.Username, token, uploadSpeed: State.CurrentValue.User.Statistics.AverageSpeed, - hasFreeUploadSlot: forecastedPosition == 0, + hasFreeUploadSlot: freeSlots > 0, queueLength: forecastedPosition, fileList: results); } @@ -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,9 +1970,9 @@ 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, + hasFreeUploadSlot: freeSlots > 0, picture: pictureBytes); return info; diff --git a/src/slskd/Common/TokenBucket.cs b/src/slskd/Common/TokenBucket.cs index 11d64b88..440f25ae 100644 --- a/src/slskd/Common/TokenBucket.cs +++ b/src/slskd/Common/TokenBucket.cs @@ -92,7 +92,7 @@ namespace slskd /// </summary> public class TokenBucket : ITokenBucket, IDisposable { - private TaskCompletionSource<bool> waitForReset = new(); + private TaskCompletionSource<bool> waitForReset = new(TaskCreationOptions.RunContinuationsAsynchronously); /// <summary> /// Initializes a new instance of the <see cref="TokenBucket"/> class. @@ -243,6 +243,6 @@ namespace slskd } private void Reset() - => Interlocked.Exchange(ref waitForReset, new TaskCompletionSource<bool>()).SetResult(true); + => Interlocked.Exchange(ref waitForReset, new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously)).SetResult(true); } } 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) |
