aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-09-27 16:18:19 -0500
committerJP Dillingham <jp@dillingham.me>2025-09-27 16:18:19 -0500
commit20fa256fb60cf37943da1e8187112682ecf24c0a (patch)
tree3ff4562d15ab0d59de17c0a5444945b52e11d4e7 /src
parente7c37e1dba12e7a23f4bf0217be40afb2584e480 (diff)
downloadslskd-20fa256fb60cf37943da1e8187112682ecf24c0a.tar.gz
refactor progress updates
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs31
-rw-r--r--src/slskd/Transfers/Uploads/UploadService.cs33
2 files changed, 57 insertions, 7 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs
index 318c4e43..ef8e84bf 100644
--- a/src/slskd/Transfers/Downloads/DownloadService.cs
+++ b/src/slskd/Transfers/Downloads/DownloadService.cs
@@ -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!