aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Transfers
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-07-17 17:19:53 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-07-17 17:19:53 -0500
commit36ba3ab11afa871a2fa34a2383b455686989566d (patch)
tree75ec936095c5c1c344cc16f2611ae9ff30309c22 /src/slskd/Transfers
parenta8528fd90b0a5636efd859bc841fa28d267ef84f (diff)
downloadslskd-36ba3ab11afa871a2fa34a2383b455686989566d.tar.gz
simplify interface of UploadGovernor and UploadQueue
Diffstat (limited to 'src/slskd/Transfers')
-rw-r--r--src/slskd/Transfers/Uploads/UploadGovernor.cs25
-rw-r--r--src/slskd/Transfers/Uploads/UploadQueue.cs55
2 files changed, 42 insertions, 38 deletions
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) =>
{