From 36ba3ab11afa871a2fa34a2383b455686989566d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Jul 2022 17:19:53 -0500 Subject: simplify interface of UploadGovernor and UploadQueue --- src/slskd/Application.cs | 10 ++--- src/slskd/Transfers/Uploads/UploadGovernor.cs | 25 ++++++------ src/slskd/Transfers/Uploads/UploadQueue.cs | 55 +++++++++++++++------------ 3 files changed, 47 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index a307a291..249376fd 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -636,14 +636,14 @@ namespace slskd if (e.Transfer.State.HasFlag(TransferStates.Queued)) { - Transfers.Uploads.Queue.Enqueue(e.Transfer); + Transfers.Uploads.Queue.Enqueue(e.Transfer.Username, e.Transfer.Filename); } }, progressUpdated: (e) => tracker.AddOrUpdate(e.Transfer, cts), - governor: (tx, req, ct) => Transfers.Uploads.Governor.GetBytesAsync(tx, req, ct), - reporter: (tx, att, grant, act) => Transfers.Uploads.Governor.ReturnBytes(tx, att, grant, act), - slotAwaiter: (tx, ct) => Transfers.Uploads.Queue.AwaitStartAsync(tx), - slotReleased: (tx) => Transfers.Uploads.Queue.Complete(tx)); + governor: (tx, req, ct) => Transfers.Uploads.Governor.GetBytesAsync(tx.Username, req, ct), + reporter: (tx, att, grant, act) => Transfers.Uploads.Governor.ReturnBytes(tx.Username, att, grant, act), + slotAwaiter: (tx, ct) => Transfers.Uploads.Queue.AwaitStartAsync(tx.Username, tx.Filename), + slotReleased: (tx) => Transfers.Uploads.Queue.Complete(tx.Username, tx.Filename)); // accept all download requests, and begin the upload immediately. normally there would be an internal queue, and // uploads would be handled separately. diff --git a/src/slskd/Transfers/Uploads/UploadGovernor.cs b/src/slskd/Transfers/Uploads/UploadGovernor.cs index a806815f..0f481e69 100644 --- a/src/slskd/Transfers/Uploads/UploadGovernor.cs +++ b/src/slskd/Transfers/Uploads/UploadGovernor.cs @@ -24,7 +24,6 @@ namespace slskd.Transfers using System.Threading; using System.Threading.Tasks; using slskd.Users; - using Soulseek; /// /// Governs upload transfer speed. @@ -32,26 +31,26 @@ namespace slskd.Transfers public interface IUploadGovernor { /// - /// Asynchronously obtains a grant of for the specified . + /// Asynchronously obtains a grant of for the requesting . /// /// /// This operation completes when any number of bytes can be granted. The amount returned may be smaller than the /// requested amount. /// - /// The transfer for which the grant is requested. + /// The username of the requesting user. /// The number of requested bytes. /// The token to monitor for cancellation. /// The operation context, including the number of bytes granted. - Task GetBytesAsync(Transfer transfer, int requestedBytes, CancellationToken cancellationToken); + Task GetBytesAsync(string username, int requestedBytes, CancellationToken cancellationToken); /// /// Returns wasted bytes for redistribution. /// - /// The transfer which generated the waste. + /// The username of the user that generated the waste. /// The number of bytes that were attempted to be transferred. /// The number of bytes granted by all governors in the system. /// The actual number of bytes transferred. - public void ReturnBytes(Transfer transfer, int attemptedBytes, int grantedBytes, int actualBytes); + public void ReturnBytes(string username, int attemptedBytes, int grantedBytes, int actualBytes); } /// @@ -83,19 +82,19 @@ namespace slskd.Transfers private IUserService Users { get; } /// - /// Asynchronously obtains a grant of for the specified . + /// Asynchronously obtains a grant of for the requesting . /// /// /// This operation completes when any number of bytes can be granted. The amount returned may be smaller than the /// requested amount. /// - /// The transfer for which the grant is requested. + /// The username of the requesting user. /// The number of requested bytes. /// The token to monitor for cancellation. /// The operation context, including the number of bytes granted. - public Task GetBytesAsync(Transfer transfer, int requestedBytes, CancellationToken cancellationToken) + public Task GetBytesAsync(string username, int requestedBytes, CancellationToken cancellationToken) { - var group = Users.GetGroup(transfer.Username); + var group = Users.GetGroup(username); var bucket = TokenBuckets.GetValueOrDefault(group ?? string.Empty, TokenBuckets[Application.DefaultGroup]); return bucket.GetAsync(requestedBytes, cancellationToken); @@ -104,11 +103,11 @@ namespace slskd.Transfers /// /// Returns wasted bytes for redistribution. /// - /// The transfer which generated the waste. + /// The username of the user that generated the waste. /// The number of bytes that were attempted to be transferred. /// The number of bytes granted by all governors in the system. /// The actual number of bytes transferred. - public void ReturnBytes(Transfer transfer, int attemptedBytes, int grantedBytes, int actualBytes) + public void ReturnBytes(string username, int attemptedBytes, int grantedBytes, int actualBytes) { var waste = Math.Max(0, grantedBytes - actualBytes); @@ -117,7 +116,7 @@ namespace slskd.Transfers return; } - var group = Users.GetGroup(transfer.Username); + var group = Users.GetGroup(username); var bucket = TokenBuckets.GetValueOrDefault(group ?? string.Empty, TokenBuckets[Application.DefaultGroup]); // we don't have enough information to tell whether grantedBytes was reduced by the global limiter within diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index f694a887..9ac6b385 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -28,7 +28,6 @@ namespace slskd.Transfers using System.Threading.Tasks; using Serilog; using slskd.Users; - using Soulseek; /// /// Orchestrates uploads. @@ -38,21 +37,24 @@ namespace slskd.Transfers /// /// Awaits the start of an upload. /// - /// The upload for which to wait. + /// The username of the remote user. + /// The filename for which to await the start. /// The operation context. - Task AwaitStartAsync(Transfer transfer); + Task AwaitStartAsync(string username, string filename); /// /// Signals the completion of an upload. /// - /// The completed upload. - void Complete(Transfer transfer); + /// The username of the remote user. + /// The completed filename. + void Complete(string username, string filename); /// /// Enqueues an upload. /// - /// The upload to enqueue. - void Enqueue(Transfer transfer); + /// The username of the remote user. + /// The filename to enqueue. + void Enqueue(string username, string filename); /// /// Computes the estimated queue position of the specified for the specified . @@ -109,24 +111,25 @@ namespace slskd.Transfers /// /// Awaits the start of an upload. /// - /// The upload for which to wait. + /// The username of the remote user. + /// The filename for which to await the start. /// The operation context. - public Task AwaitStartAsync(Transfer transfer) + public Task AwaitStartAsync(string username, string filename) { SyncRoot.Wait(); try { - if (!Uploads.TryGetValue(transfer.Username, out var list)) + if (!Uploads.TryGetValue(username, out var list)) { - throw new SlskdException($"No enqueued uploads for user {transfer.Username}"); + throw new SlskdException($"No enqueued uploads for user {username}"); } - var upload = list.FirstOrDefault(e => e.Filename == transfer.Filename); + var upload = list.FirstOrDefault(e => e.Filename == filename); if (upload == default) { - throw new SlskdException($"File {transfer.Filename} is not enqueued for user {transfer.Username}"); + throw new SlskdException($"File {filename} is not enqueued for user {username}"); } upload.Ready = DateTime.UtcNow; @@ -144,23 +147,24 @@ namespace slskd.Transfers /// /// Signals the completion of an upload. /// - /// The completed upload. - public void Complete(Transfer transfer) + /// The username of the remote user. + /// The completed filename. + public void Complete(string username, string filename) { SyncRoot.Wait(); try { - if (!Uploads.TryGetValue(transfer.Username, out var list)) + if (!Uploads.TryGetValue(username, out var list)) { - throw new SlskdException($"No enqueued uploads for user {transfer.Username}"); + throw new SlskdException($"No enqueued uploads for user {username}"); } - var upload = list.FirstOrDefault(e => e.Filename == transfer.Filename); + var upload = list.FirstOrDefault(e => e.Filename == filename); if (upload == default) { - throw new SlskdException($"File {transfer.Filename} is not enqueued for user {transfer.Username}"); + throw new SlskdException($"File {filename} is not enqueued for user {username}"); } list.Remove(upload); @@ -176,9 +180,9 @@ namespace slskd.Transfers Log.Debug("Group {Group} slots: {Used}/{Available}", group.Name, group.UsedSlots, group.Slots); } - if (!list.Any() && Uploads.TryRemove(transfer.Username, out _)) + if (!list.Any() && Uploads.TryRemove(username, out _)) { - Log.Debug("Cleaned up tracking list for {User}; no more queued uploads to track", transfer.Username); + Log.Debug("Cleaned up tracking list for {User}; no more queued uploads to track", username); } } finally @@ -191,17 +195,18 @@ namespace slskd.Transfers /// /// Enqueues an upload. /// - /// The upload to enqueue. - public void Enqueue(Transfer transfer) + /// The username of the remote user. + /// The filename to enqueue. + public void Enqueue(string username, string filename) { SyncRoot.Wait(); try { - var upload = new Upload() { Username = transfer.Username, Filename = transfer.Filename }; + var upload = new Upload() { Username = username, Filename = filename }; Uploads.AddOrUpdate( - key: transfer.Username, + key: username, addValue: new List(new[] { upload }), updateValueFactory: (key, list) => { -- cgit v1.2.3