aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-09-27 16:29:16 -0500
committerGitHub <noreply@github.com>2025-09-27 16:29:16 -0500
commitcb39700951a693cf32e91a1aeaa8d7c57220c828 (patch)
tree3ff4562d15ab0d59de17c0a5444945b52e11d4e7 /src
parent48fc4a04c7c607e882f728c74b8bc002bbc592b5 (diff)
parent20fa256fb60cf37943da1e8187112682ecf24c0a (diff)
downloadslskd-cb39700951a693cf32e91a1aeaa8d7c57220c828.tar.gz
Merge pull request #1457 from slskd/downloads
Refactor transfer progress update logic
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs41
-rw-r--r--src/slskd/Transfers/Uploads/UploadService.cs33
2 files changed, 62 insertions, 12 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs
index 63b04394..ef8e84bf 100644
--- a/src/slskd/Transfers/Downloads/DownloadService.cs
+++ b/src/slskd/Transfers/Downloads/DownloadService.cs
@@ -155,11 +155,11 @@ namespace slskd.Transfers.Downloads
using var context = ContextFactory.CreateDbContext();
var existing = context.Transfers
- .Where(t => t.Direction == TransferDirection.Download)
- .Where(t => t.Username == transfer.Username)
- .Where(t => t.Filename == transfer.Filename)
- .Where(t => !t.Removed)
- .FirstOrDefault();
+ .Where(t => t.Direction == TransferDirection.Download)
+ .Where(t => t.Username == transfer.Username)
+ .Where(t => t.Filename == transfer.Filename)
+ .Where(t => !t.Removed)
+ .FirstOrDefault();
if (existing != default)
{
@@ -283,10 +283,35 @@ namespace slskd.Transfers.Downloads
},
progressUpdated: (args) => rateLimiter.Invoke(() =>
{
- transfer = transfer.WithSoulseekTransfer(args.Transfer);
+ // don't do anything unless the `transfer` within the outer scope is in the
+ // InProgress state; this will help prevent out-of-band updates that are sometimes
+ // being made after the transfer is completed, causing them to appear 'stuck'
+ if (transfer.State == TransferStates.InProgress)
+ {
+ syncRoot.Wait(cts.Token);
- // todo: broadcast to signalr hub
- SynchronizedUpdate(transfer);
+ try
+ {
+ // check again to see if the state changed while we were waiting to obtain the lock
+ if (transfer.State == TransferStates.InProgress)
+ {
+ // update only the properties that we expect to change between progress updates
+ // this helps prevent this update from 'stepping' on other updates
+ transfer.BytesTransferred = args.Transfer.BytesTransferred;
+ transfer.AverageSpeed = args.Transfer.AverageSpeed;
+
+ using var context = ContextFactory.CreateDbContext();
+
+ context.Transfers.Where(t => t.Id == transfer.Id).ExecuteUpdate(setter => setter
+ .SetProperty(t => t.BytesTransferred, transfer.BytesTransferred)
+ .SetProperty(t => t.AverageSpeed, transfer.AverageSpeed));
+ }
+ }
+ finally
+ {
+ syncRoot.Release();
+ }
+ }
}));
var completedTransfer = await Client.DownloadAsync(
diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs
index 6209f3da..6db9f6a5 100644
--- a/src/slskd/Transfers/Uploads/UploadService.cs
+++ b/src/slskd/Transfers/Uploads/UploadService.cs
@@ -329,10 +329,35 @@ namespace slskd.Transfers.Uploads
},
progressUpdated: (args) => rateLimiter.Invoke(() =>
{
- transfer = transfer.WithSoulseekTransfer(args.Transfer);
-
- // todo: broadcast
- SynchronizedUpdate(transfer);
+ // don't do anything unless the `transfer` within the outer scope is in the
+ // InProgress state; this will help prevent out-of-band updates that are sometimes
+ // being made after the transfer is completed, causing them to appear 'stuck'
+ if (transfer.State == TransferStates.InProgress)
+ {
+ syncRoot.Wait(cts.Token);
+
+ try
+ {
+ // update only the properties that we expect to change between progress updates
+ // this helps prevent this update from 'stepping' on other updates
+ transfer.BytesTransferred = args.Transfer.BytesTransferred;
+ transfer.AverageSpeed = args.Transfer.AverageSpeed;
+
+ // check again to see if the state changed while we were waiting to obtain the lock
+ if (transfer.State == TransferStates.InProgress)
+ {
+ using var context = ContextFactory.CreateDbContext();
+
+ context.Transfers.Where(t => t.Id == transfer.Id).ExecuteUpdate(setter => setter
+ .SetProperty(t => t.BytesTransferred, args.Transfer.BytesTransferred)
+ .SetProperty(t => t.AverageSpeed, args.Transfer.AverageSpeed));
+ }
+ }
+ finally
+ {
+ syncRoot.Release();
+ }
+ }
}),
seekInputStreamAutomatically: false,
disposeInputStreamOnCompletion: true, // note: don't set this to false!