diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-28 17:49:10 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-11-28 17:49:10 -0600 |
| commit | 3d994c5f54e3b74f182b2b6e565d35fee4acab01 (patch) | |
| tree | 81450540782e352415bba6e8dd765c941ab06d1f /src/slskd | |
| parent | 7dfd9311deb30b38856a8af92a809026c90a389d (diff) | |
| parent | 7450257b3d0522b9a3e0aa326148e3491d43ce52 (diff) | |
| download | slskd-3d994c5f54e3b74f182b2b6e565d35fee4acab01.tar.gz | |
Merge remote-tracking branch 'origin' into statistics
Diffstat (limited to 'src/slskd')
| -rw-r--r-- | src/slskd/Core/Options.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 5 | ||||
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 54 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 137 |
4 files changed, 124 insertions, 80 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index ec94afcb..254adcab 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -472,14 +472,6 @@ namespace slskd public bool OptimisticRelayFileInfo { get; init; } = false; /// <summary> - /// Gets a value indicating whether SQLite cache sharing should be disabled (set to Private). - /// </summary> - [Argument(default, "no-sqlite-cache-sharing")] - [EnvironmentVariable("NO_SQLITE_CACHE_SHARING")] - [Description("disable SQLite cache sharing")] - public bool NoSqliteCacheSharing { get; init; } = false; - - /// <summary> /// Gets a value indicating whether SQLite pooling should be disabled. /// </summary> [Argument(default, "no-sqlite-pooling")] diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index edfe06bc..abd39251 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -611,12 +611,11 @@ namespace slskd var connectionStringDictionary = new ConnectionStringDictionary(Database.List .Select(database => { - var caching = OptionsAtStartup.Flags.NoSqliteCacheSharing ? "Private" : "Shared"; var pooling = OptionsAtStartup.Flags.NoSqlitePooling ? "False" : "True"; // don't invert and ToString this it is confusing var connStr = OptionsAtStartup.Flags.Volatile - ? $"Data Source=file:{database}?mode=memory;Cache={caching};Pooling={pooling};" - : $"Data Source={Path.Combine(DataDirectory, $"{database}.db")};Cache={caching};Pooling={pooling}"; + ? $"Data Source=file:{database}?mode=memory;Pooling={pooling};" + : $"Data Source={Path.Combine(DataDirectory, $"{database}.db")};Pooling={pooling}"; return new KeyValuePair<Database, ConnectionString>(database, connStr); }) diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 65b5977f..9bc25373 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -108,6 +108,20 @@ namespace slskd.Transfers.Downloads bool TryCancel(Guid id); /// <summary> + /// Fails the download matching the specified <paramref name="id"/> with the specified <paramref name="exception"/>, + /// and sets the final state accordingly. + /// </summary> + /// <remarks> + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// </remarks> + /// <param name="id">The unique identifier for the download.</param> + /// <param name="exception">The exception that caused the failure.</param> + /// <returns>A value indicating whether the download was successfully failed.</returns> + bool TryFail(Guid id, Exception exception); + + /// <summary> /// Updates the specified <paramref name="transfer"/>. /// </summary> /// <param name="transfer">The transfer to update.</param> @@ -785,30 +799,38 @@ namespace slskd.Transfers.Downloads { var t = Find(t => t.Id == id); - if (t is not null) + if (t is null) { - t.EndedAt ??= DateTime.UtcNow; + return false; + } + + t.EndedAt ??= DateTime.UtcNow; - // Soulseek.NET will include the filename and username in some messages; this is useful for many things but not tracking - // exceptions in a database. when we encounter one of these, drop the first segment. - var m = exception.Message; - t.Exception ??= m.Contains(':') ? m.Substring(m.IndexOf(':') + 1).Trim() : m; + // Soulseek.NET will include the filename and username in some messages; this is useful for many things but not tracking + // exceptions in a database. when we encounter one of these, drop the first segment. + var m = exception.Message; + t.Exception ??= m.Contains(':') ? m.Substring(m.IndexOf(':') + 1).Trim() : m; - if (!t.State.HasFlag(TransferStates.Completed)) + if (!t.State.HasFlag(TransferStates.Completed)) + { + t.State = TransferStates.Completed | exception switch { - t.State = TransferStates.Completed | exception switch - { - OperationCanceledException => TransferStates.Cancelled, - TimeoutException => TransferStates.TimedOut, - _ => TransferStates.Errored, - }; - } + OperationCanceledException => TransferStates.Cancelled, + TimeoutException => TransferStates.TimedOut, + _ => TransferStates.Errored, + }; + } + try + { Update(t); return true; } - - return false; + catch (Exception ex) + { + Log.Error(ex, "Failed to update database: {Message}", ex.Message); + return false; + } } /// <summary> diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 1aa97603..c96d1131 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -126,6 +126,20 @@ namespace slskd.Transfers.Uploads bool TryCancel(Guid id); /// <summary> + /// Fails the upload matching the specified <paramref name="id"/> with the specified <paramref name="exception"/>, + /// and sets the final state accordingly. + /// </summary> + /// <remarks> + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// </remarks> + /// <param name="id">The unique identifier for the upload.</param> + /// <param name="exception">The exception that caused the failure.</param> + /// <returns>A value indicating whether the upload was successfully failed.</returns> + bool TryFail(Guid id, Exception exception); + + /// <summary> /// Synchronously updates the specified <paramref name="transfer"/>. /// </summary> /// <param name="transfer">The transfer to update.</param> @@ -418,28 +432,22 @@ namespace slskd.Transfers.Uploads return transfer; } - catch (NotFoundException) + catch (NotFoundException ex) { - transfer.EndedAt = DateTime.Now; - transfer.Exception = "File could not be found"; - transfer.State = TransferStates.Completed | TransferStates.Aborted; + Log.Error(ex, "Upload of {Filename} to user {Username} failed: {Message}", transfer.Filename, transfer.Username, ex.Message); + + TryFail(transfer.Id, "File could not be found", TransferStates.Completed | TransferStates.Aborted); + // todo: broadcast SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: CancellationToken.None); throw; } catch (Exception ex) when (ex is OperationCanceledException || ex is TimeoutException) { - transfer.EndedAt = DateTime.UtcNow; - transfer.Exception = ex.Message; - transfer.State = TransferStates.Completed; + Log.Error(ex, "Upload of {Filename} to user {Username} failed: {Message}", transfer.Filename, transfer.Username, ex.Message); - transfer.State |= ex switch - { - OperationCanceledException => TransferStates.Cancelled, - TimeoutException => TransferStates.TimedOut, - _ => TransferStates.Errored, - }; + TryFail(transfer.Id, exception: ex); // todo: broadcast SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: CancellationToken.None); @@ -450,9 +458,7 @@ namespace slskd.Transfers.Uploads { Log.Error(ex, "Upload of {Filename} to user {Username} failed: {Message}", transfer.Filename, transfer.Username, ex.Message); - transfer.EndedAt = DateTime.UtcNow; - transfer.Exception = ex.Message; - transfer.State = TransferStates.Completed | TransferStates.Errored; + TryFail(transfer.Id, exception: ex); // todo: broadcast SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: CancellationToken.None); @@ -488,7 +494,6 @@ namespace slskd.Transfers.Uploads catch (Exception ex) { Log.Error(ex, "Failed to finalize upload of {Filename} to {Username}: {Message}", transfer.Filename, transfer.Username, ex.Message); - throw; } } } @@ -651,23 +656,9 @@ namespace slskd.Transfers.Uploads */ Log.Error(task.Exception, "Task for upload of {Filename} to {Username} did not complete successfully: {Error}", filename, username, task.Exception.Message); - try + if (!TryFail(id, task.Exception)) { - var transfer = Find(t => t.Id == id); - - if (transfer is not null) - { - transfer.EndedAt ??= DateTime.UtcNow; - transfer.Exception ??= task.Exception.Message; - transfer.State = TransferStates.Completed | transfer.State; - - Update(transfer); - } - } - catch (Exception ex) - { - Log.Error(ex, "Failed to clean up transfer {Id} after failed execution: {Message}", id, ex.Message); - throw; + Log.Error(task.Exception, "Failed to clean up transfer {Id} after failed execution: {Message}", id, task.Exception.Message); } }); @@ -677,27 +668,9 @@ namespace slskd.Transfers.Uploads { Log.Error(ex, "Failed to enqueue upload of {Filename} to {Username}: {Message}", filename, username, ex.Message); - try - { - var transfer = Find(t => t.Id == id); - - if (transfer is not null) - { - transfer.EndedAt ??= DateTime.UtcNow; - - // Soulseek.NET will include the filename and username in some messages; this is useful for many things but not tracking - // exceptions in a database. when we encounter one of these, drop the first segment. - var m = ex.Message; - transfer.Exception ??= m.Contains(':') ? m.Substring(m.IndexOf(':') + 1).Trim() : m; - - transfer.State = TransferStates.Completed | transfer.State; - - Update(transfer); - } - } - catch (Exception innerEx) + if (!TryFail(id, exception: ex)) { - Log.Error(innerEx, "Failed to clean up transfer {Id} after failed enqueue: {Message}", id, innerEx.Message); + Log.Error(ex, "Failed to clean up transfer {Id} after failed enqueue: {Message}", id, ex.Message); throw; } @@ -904,6 +877,31 @@ namespace slskd.Transfers.Uploads } /// <summary> + /// Fails the upload matching the specified <paramref name="id"/> with the specified <paramref name="exception"/>, + /// and sets the final state accordingly. + /// </summary> + /// <remarks> + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// </remarks> + /// <param name="id">The unique identifier for the upload.</param> + /// <param name="exception">The exception that caused the failure.</param> + /// <returns>A value indicating whether the upload was successfully failed.</returns> + public bool TryFail(Guid id, Exception exception) + { + var state = exception switch + { + NotFoundException => TransferStates.Aborted, + OperationCanceledException => TransferStates.Cancelled, + TimeoutException => TransferStates.TimedOut, + _ => TransferStates.Errored, + }; + + return TryFail(id, exception.Message, state); + } + + /// <summary> /// Synchronously updates the specified <paramref name="transfer"/>. /// </summary> /// <param name="transfer">The transfer to update.</param> @@ -915,6 +913,39 @@ namespace slskd.Transfers.Uploads context.SaveChanges(); } + private bool TryFail(Guid id, string exception, TransferStates state) + { + var t = Find(t => t.Id == id); + + if (t is null) + { + return false; + } + + t.EndedAt ??= DateTime.UtcNow; + + // Soulseek.NET will include the filename and username in some messages; this is useful for many things but not tracking + // exceptions in a database. when we encounter one of these, drop the first segment. + var m = exception; + t.Exception ??= m.Contains(':') ? m.Substring(m.IndexOf(':') + 1).Trim() : m; + + if (!t.State.HasFlag(TransferStates.Completed)) + { + t.State = TransferStates.Completed | state; + } + + try + { + Update(t); + return true; + } + catch (Exception ex) + { + Log.Error(ex, "Failed to update database: {Message}", ex.Message); + return false; + } + } + /// <summary> /// Resolves a remote file's location and size from shares and potentially disk. /// </summary> |
