diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-10-03 19:34:01 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-10-03 19:34:01 -0500 |
| commit | efc33e19a7e33c38d59f7d3d26a0b93318af9a26 (patch) | |
| tree | 066e93e8643f44727a976ed2975f738a4a425801 /src | |
| parent | 4e40b1935624a1ff4d436544cc44cdf64aefbaf9 (diff) | |
| download | slskd-efc33e19a7e33c38d59f7d3d26a0b93318af9a26.tar.gz | |
synchronize EstimatePosition, reduce memory pressure
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadQueue.cs | 108 |
1 files changed, 59 insertions, 49 deletions
diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index fa3bb68c..441bc368 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -91,6 +91,11 @@ namespace slskd.Transfers.Uploads /// <summary> /// Computes the estimated queue position of the specified <paramref name="filename"/> for the specified <paramref name="username"/>. /// </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> /// <returns>The estimated queue position of the file.</returns> @@ -323,11 +328,9 @@ namespace slskd.Transfers.Uploads // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user var uploadsForGroup = UploadDictionary.Where(kvp => Users.GetGroup(kvp.Key) == groupName); - // the RoundRobin queue implementation is not strictly fair to all users; only uploads that are ready are candidates - // for selection. this means that if Bob downloads files twice as fast as Alice, Bob is going to advance through the - // queue twice as fast, too. assume everyone downloads at equal speed for this estimate. also assume that all files - // are of equal length. - if (groupRecord.Strategy == QueueStrategy.RoundRobin) + SyncRoot.Wait(); + + try { // find this user's uploads if (!UploadDictionary.TryGetValue(username, out var uploadsForUser)) @@ -335,59 +338,66 @@ namespace slskd.Transfers.Uploads throw new NotFoundException($"File {filename} is not enqueued for user {username}"); } - // find the position of the requested file in the user's queue - var localPosition = uploadsForUser - .OrderBy(upload => upload.Enqueued) - .ToList() - .FindIndex(upload => upload.Username == username && upload.Filename == filename); - - if (localPosition < 0) + // the RoundRobin queue implementation is not strictly fair to all users; only uploads that are ready are candidates + // for selection. this means that if Bob downloads files twice as fast as Alice, Bob is going to advance through the + // queue twice as fast, too. assume everyone downloads at equal speed for this estimate. also assume that all files + // are of equal length. + if (groupRecord.Strategy == QueueStrategy.RoundRobin) { - throw new NotFoundException($"File {filename} is not enqueued for user {username}"); - } + // 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); - // start the position to the local position within this user's queue; the user's own files must be completed - // before this one can start. - var position = localPosition; - - // for each other user, add either localPosition or the count of that user's uploads, whichever is less - // example: - // - // aaaaa - // bb - // cccccccccccc - // ddddddd - // ^ - // - // if we want the position of the file over the carat above, first find the position of it - // within its own queue (= 5). assume uploads will process top down, left to right until reaching - // this one. that's 5 files from a, 2 from b, 5 from c, and the other 4 from d, putting the file over - // the carat at position 16. the actual number will vary due to many factors, including where in the - // round-robin ordering d is actually positioned (so +/- number of users downloading). - foreach (var group in uploadsForGroup.Where(group => group.Key != username)) - { - position += Math.Min(localPosition, group.Value.Count); + if (localPosition < 0) + { + throw new NotFoundException($"File {filename} is not enqueued for user {username}"); + } + + // start the position to the local position within this user's queue; the user's own files must be completed + // before this one can start. + var position = localPosition; + + // for each other user, add either localPosition or the count of that user's uploads, whichever is less + // example: + // + // aaaaa + // bb + // cccccccccccc + // ddddddd + // ^ + // + // if we want the position of the file over the carat above, first find the position of it + // within its own queue (= 5). assume uploads will process top down, left to right until reaching + // this one. that's 5 files from a, 2 from b, 5 from c, and the other 4 from d, putting the file over + // the carat at position 16. the actual number will vary due to many factors, including where in the + // round-robin ordering d is actually positioned (so +/- number of users downloading). + foreach (var group in uploadsForGroup.Where(group => group.Key != username)) + { + position += Math.Min(localPosition, group.Value.Count); + } + + return position; } - return position; - } + // find the upload + var upload = uploadsForUser.SingleOrDefault(u => u.Username == username && u.Filename == filename); - // for FIFO queues, files are uploaded in the order they are enqueued, so the position should be pretty good estimate. - // List ordering is guaranteed, so we are getting an accurate portrayal of where this file is in the queue by order of - // time enqueued. this includes uploads that are in progress. - var flattenedSortedUploadsForGroup = uploadsForGroup - .SelectMany(group => group.Value) - .OrderBy(upload => upload.Enqueued) - .ToList(); + if (upload is null) + { + throw new NotFoundException($"File {filename} is not enqueued for user {username}"); + } - var globalPosition = flattenedSortedUploadsForGroup.FindIndex(upload => upload.Username == username && upload.Filename == filename); + // the place in queue is simply the sum of all uploads across all other users in the group that were + // enqueued before this upload. + var globalPosition = uploadsForGroup.Sum(kvp => kvp.Value.Count(c => c.Enqueued < upload.Enqueued)); - if (globalPosition < 0) + return globalPosition; + } + finally { - throw new NotFoundException($"File {filename} is not enqueued for user {username}"); + SyncRoot.Release(); } - - return globalPosition; } /// <summary> |
