From aa2ddbc6c77aab614e1cae1032edd1d625719a8c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 13 Jul 2022 10:22:01 -0500 Subject: forecast queue position correctly for different queue strategies --- src/slskd/Transfers/Uploads/UploadQueue.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index c3b2b3e6..f694a887 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -310,19 +310,29 @@ namespace slskd.Transfers /// public int ForecastPosition(string username) { - var group = Users.GetGroup(username); + var groupName = Users.GetGroup(username); // if there's a slot available, the user will enter the queue at position 0 (will start immediately) - if (Groups.TryGetValue(group, out var groupRecord) && groupRecord.SlotAvailable) + if (Groups.TryGetValue(groupName, out var groupRecord) && groupRecord.SlotAvailable) { return 0; } - // no slots are available for this group, so there's at least as many uploads in progress as there are slots. count all - // uploads (in progress or otherwise) - var uploadsForGroup = Uploads.Where(kvp => Users.GetGroup(kvp.Key) == group); + // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user + var uploadsForGroup = Uploads.Where(kvp => Users.GetGroup(kvp.Key) == groupName); + + // 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. + if (groupRecord.Strategy == QueueStrategy.RoundRobin) + { + return uploadsForGroup.Count() + 1; + } - return uploadsForGroup.Count() + 1; + // 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 + .SelectMany(group => group.Value) + .Count() + 1; } private void Configure(Options options) -- cgit v1.2.3