diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-07-17 17:19:53 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-07-17 17:19:53 -0500 |
| commit | 36ba3ab11afa871a2fa34a2383b455686989566d (patch) | |
| tree | 75ec936095c5c1c344cc16f2611ae9ff30309c22 /src | |
| parent | a8528fd90b0a5636efd859bc841fa28d267ef84f (diff) | |
| download | slskd-36ba3ab11afa871a2fa34a2383b455686989566d.tar.gz | |
simplify interface of UploadGovernor and UploadQueue
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 10 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadGovernor.cs | 25 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadQueue.cs | 55 |
3 files changed, 47 insertions, 43 deletions
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; /// <summary> /// Governs upload transfer speed. @@ -32,26 +31,26 @@ namespace slskd.Transfers public interface IUploadGovernor { /// <summary> - /// Asynchronously obtains a grant of <paramref name="requestedBytes"/> for the specified <paramref name="transfer"/>. + /// Asynchronously obtains a grant of <paramref name="requestedBytes"/> for the requesting <paramref name="username"/>. /// </summary> /// <remarks> /// This operation completes when any number of bytes can be granted. The amount returned may be smaller than the /// requested amount. /// </remarks> - /// <param name="transfer">The transfer for which the grant is requested.</param> + /// <param name="username">The username of the requesting user.</param> /// <param name="requestedBytes">The number of requested bytes.</param> /// <param name="cancellationToken">The token to monitor for cancellation.</param> /// <returns>The operation context, including the number of bytes granted.</returns> - Task<int> GetBytesAsync(Transfer transfer, int requestedBytes, CancellationToken cancellationToken); + Task<int> GetBytesAsync(string username, int requestedBytes, CancellationToken cancellationToken); /// <summary> /// Returns wasted bytes for redistribution. /// </summary> - /// <param name="transfer">The transfer which generated the waste.</param> + /// <param name="username">The username of the user that generated the waste.</param> /// <param name="attemptedBytes">The number of bytes that were attempted to be transferred.</param> /// <param name="grantedBytes">The number of bytes granted by all governors in the system.</param> /// <param name="actualBytes">The actual number of bytes transferred.</param> - public void ReturnBytes(Transfer transfer, int attemptedBytes, int grantedBytes, int actualBytes); + public void ReturnBytes(string username, int attemptedBytes, int grantedBytes, int actualBytes); } /// <summary> @@ -83,19 +82,19 @@ namespace slskd.Transfers private IUserService Users { get; } /// <summary> - /// Asynchronously obtains a grant of <paramref name="requestedBytes"/> for the specified <paramref name="transfer"/>. + /// Asynchronously obtains a grant of <paramref name="requestedBytes"/> for the requesting <paramref name="username"/>. /// </summary> /// <remarks> /// This operation completes when any number of bytes can be granted. The amount returned may be smaller than the /// requested amount. /// </remarks> - /// <param name="transfer">The transfer for which the grant is requested.</param> + /// <param name="username">The username of the requesting user.</param> /// <param name="requestedBytes">The number of requested bytes.</param> /// <param name="cancellationToken">The token to monitor for cancellation.</param> /// <returns>The operation context, including the number of bytes granted.</returns> - public Task<int> GetBytesAsync(Transfer transfer, int requestedBytes, CancellationToken cancellationToken) + public Task<int> 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 /// <summary> /// Returns wasted bytes for redistribution. /// </summary> - /// <param name="transfer">The transfer which generated the waste.</param> + /// <param name="username">The username of the user that generated the waste.</param> /// <param name="attemptedBytes">The number of bytes that were attempted to be transferred.</param> /// <param name="grantedBytes">The number of bytes granted by all governors in the system.</param> /// <param name="actualBytes">The actual number of bytes transferred.</param> - 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; /// <summary> /// Orchestrates uploads. @@ -38,21 +37,24 @@ namespace slskd.Transfers /// <summary> /// Awaits the start of an upload. /// </summary> - /// <param name="transfer">The upload for which to wait.</param> + /// <param name="username">The username of the remote user.</param> + /// <param name="filename">The filename for which to await the start.</param> /// <returns>The operation context.</returns> - Task AwaitStartAsync(Transfer transfer); + Task AwaitStartAsync(string username, string filename); /// <summary> /// Signals the completion of an upload. /// </summary> - /// <param name="transfer">The completed upload.</param> - void Complete(Transfer transfer); + /// <param name="username">The username of the remote user.</param> + /// <param name="filename">The completed filename.</param> + void Complete(string username, string filename); /// <summary> /// Enqueues an upload. /// </summary> - /// <param name="transfer">The upload to enqueue.</param> - void Enqueue(Transfer transfer); + /// <param name="username">The username of the remote user.</param> + /// <param name="filename">The filename to enqueue.</param> + void Enqueue(string username, string filename); /// <summary> /// Computes the estimated queue position of the specified <paramref name="filename"/> for the specified <paramref name="username"/>. @@ -109,24 +111,25 @@ namespace slskd.Transfers /// <summary> /// Awaits the start of an upload. /// </summary> - /// <param name="transfer">The upload for which to wait.</param> + /// <param name="username">The username of the remote user.</param> + /// <param name="filename">The filename for which to await the start.</param> /// <returns>The operation context.</returns> - 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 /// <summary> /// Signals the completion of an upload. /// </summary> - /// <param name="transfer">The completed upload.</param> - public void Complete(Transfer transfer) + /// <param name="username">The username of the remote user.</param> + /// <param name="filename">The completed filename.</param> + 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 /// <summary> /// Enqueues an upload. /// </summary> - /// <param name="transfer">The upload to enqueue.</param> - public void Enqueue(Transfer transfer) + /// <param name="username">The username of the remote user.</param> + /// <param name="filename">The filename to enqueue.</param> + 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<Upload>(new[] { upload }), updateValueFactory: (key, list) => { |
