diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-10-04 17:58:00 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-10-04 17:58:00 -0500 |
| commit | c8e43f739b967470900072530ca7138c3b18b106 (patch) | |
| tree | 9e37f60ff26d8a49f7b73b80a6715b3eff6db48f /src | |
| parent | 5c5318b6f1f3aac1a0eda59090c89ad0dd27ca46 (diff) | |
| download | slskd-c8e43f739b967470900072530ca7138c3b18b106.tar.gz | |
move checks inside try/catch
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 50 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 78 |
2 files changed, 64 insertions, 64 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index bf501c33..eafbf388 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -213,31 +213,6 @@ namespace slskd.Transfers.Downloads Log.Debug("Acquired lock {LockName}", lockName); - /* - fetch an updated copy of the transfer record from the database; now that we are locked, we *should* - have exclusive access to this record - - we're not using DbContext and tracked changes here because we don't want to hold a connection - open for the duration of the download - */ - transfer = Find(t => t.Id == transfer.Id) - ?? throw new TransferNotFoundException($"Transfer with ID {transfer.Id} not found"); - - if (transfer.State != (TransferStates.Queued | TransferStates.Locally)) - { - throw new InvalidOperationException($"Invalid starting state for download; expected {TransferStates.Queued | TransferStates.Locally}, encountered {transfer.State}"); - } - - /* - Soulseek.NET keeps an internal dictionary of all transfers for the duration of the transfer logic; - check that list to see if there's already an instance of this download in it, just in case we've gotten - out of sync somehow - */ - if (Client.Downloads.Any(u => u.Username == transfer.Username && u.Filename == transfer.Filename)) - { - throw new DuplicateTransferException("A duplicate download of the same file to the same user is already registered"); - } - var cts = new CancellationTokenSource(); var syncRoot = new SemaphoreSlim(1, 1); @@ -265,6 +240,31 @@ namespace slskd.Transfers.Downloads */ try { + /* + fetch an updated copy of the transfer record from the database; now that we are locked, we *should* + have exclusive access to this record + + we're not using DbContext and tracked changes here because we don't want to hold a connection + open for the duration of the download + */ + transfer = Find(t => t.Id == transfer.Id) + ?? throw new TransferNotFoundException($"Transfer with ID {transfer.Id} not found"); + + if (transfer.State != (TransferStates.Queued | TransferStates.Locally)) + { + throw new InvalidOperationException($"Invalid starting state for download; expected {TransferStates.Queued | TransferStates.Locally}, encountered {transfer.State}"); + } + + /* + Soulseek.NET keeps an internal dictionary of all transfers for the duration of the transfer logic; + check that list to see if there's already an instance of this download in it, just in case we've gotten + out of sync somehow + */ + if (Client.Downloads.Any(u => u.Username == transfer.Username && u.Filename == transfer.Filename)) + { + throw new DuplicateTransferException("A duplicate download of the same file to the same user is already registered"); + } + Log.Information("Initializing download of {Filename} from {Username}", transfer.Filename, transfer.Username); using var rateLimiter = new RateLimiter(250, flushOnDispose: true); diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 8fa08cea..bd167a57 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -230,45 +230,6 @@ namespace slskd.Transfers.Uploads Log.Debug("Acquired lock {LockName}", lockName); - /* - fetch an updated copy of the transfer record from the database; now that we are locked, we *should* - have exclusive access to this record - - we're not using DbContext and tracked changes here because we don't want to hold a connection - open for the duration of the upload - */ - transfer = Find(t => t.Id == transfer.Id) - ?? throw new TransferNotFoundException($"Transfer with ID {transfer.Id} not found"); - - if (transfer.State != (TransferStates.Queued | TransferStates.Locally)) - { - throw new InvalidOperationException($"Invalid starting state for upload; expected {TransferStates.Queued | TransferStates.Locally}, encountered {transfer.State}"); - } - - /* - Soulseek.NET keeps an internal dictionary of all transfers for the duration of the transfer logic; - check that list to see if there's already an instance of this upload in it, just in case we've gotten - out of sync somehow - */ - if (Client.Uploads.Any(u => u.Username == transfer.Username && u.Filename == transfer.Filename)) - { - throw new DuplicateTransferException("A duplicate upload of the same file to the same user is already registered"); - } - - // users with uploads must be watched so that we can keep informed of their online status, privileges, and - // statistics. this is so that we can accurately determine their effective group. - try - { - if (!Users.IsWatched(transfer.Username)) - { - await Users.WatchAsync(transfer.Username); - } - } - catch (Exception ex) - { - Log.Warning(ex, "Failed to watch user {Username}", transfer.Username); - } - var cts = new CancellationTokenSource(); var syncRoot = new SemaphoreSlim(1, 1); @@ -300,6 +261,45 @@ namespace slskd.Transfers.Uploads */ try { + /* + fetch an updated copy of the transfer record from the database; now that we are locked, we *should* + have exclusive access to this record + + we're not using DbContext and tracked changes here because we don't want to hold a connection + open for the duration of the upload + */ + transfer = Find(t => t.Id == transfer.Id) + ?? throw new TransferNotFoundException($"Transfer with ID {transfer.Id} not found"); + + if (transfer.State != (TransferStates.Queued | TransferStates.Locally)) + { + throw new InvalidOperationException($"Invalid starting state for upload; expected {TransferStates.Queued | TransferStates.Locally}, encountered {transfer.State}"); + } + + /* + Soulseek.NET keeps an internal dictionary of all transfers for the duration of the transfer logic; + check that list to see if there's already an instance of this upload in it, just in case we've gotten + out of sync somehow + */ + if (Client.Uploads.Any(u => u.Username == transfer.Username && u.Filename == transfer.Filename)) + { + throw new DuplicateTransferException("A duplicate upload of the same file to the same user is already registered"); + } + + // users with uploads must be watched so that we can keep informed of their online status, privileges, and + // statistics. this is so that we can accurately determine their effective group. + try + { + if (!Users.IsWatched(transfer.Username)) + { + await Users.WatchAsync(transfer.Username); + } + } + catch (Exception ex) + { + Log.Warning(ex, "Failed to watch user {Username}", transfer.Username); + } + Log.Information("Initializing upload of {Filename} to {Username}", transfer.Filename, transfer.Username); // locate the file on disk. we checked this once already when enqueueing, but it may have moved since |
