diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-04-27 20:36:53 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-27 20:36:53 -0500 |
| commit | 8c611f627b1859a1d9963388fb40f7dfeaeda652 (patch) | |
| tree | 6564d337d0fe73a3c2f93b601e3e782e42f964c1 /src/slskd/Common | |
| parent | cb4d254099cf846f918214c2be07341d93d90900 (diff) | |
| parent | 2b97e3ef7f65b57c3cbe2f3298aef4f856a47985 (diff) | |
| download | slskd-8c611f627b1859a1d9963388fb40f7dfeaeda652.tar.gz | |
Merge branch 'master' into docker-volume
Diffstat (limited to 'src/slskd/Common')
| -rw-r--r-- | src/slskd/Common/CommonExtensions.cs | 27 | ||||
| -rw-r--r-- | src/slskd/Common/Retry.cs | 15 |
2 files changed, 36 insertions, 6 deletions
diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index 88e6d3c8..77db91ef 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -111,20 +111,41 @@ namespace slskd /// <returns>A list of differences between the two objects.</returns> public static IEnumerable<(PropertyInfo Property, string FQN, object Left, object Right)> DiffWith(this object left, object right, string parentFqn = null) { - if (left?.GetType() != right?.GetType()) + if (left is null || right is null) { - throw new InvalidCastException($"Unable to diff types {left?.GetType()} and {right?.GetType()}"); + if (left is null == right is null) + { + return []; + } + + return [(null, parentFqn ?? string.Empty, left, right)]; + } + + if (left.GetType() != right.GetType()) + { + throw new InvalidCastException($"Unable to diff types {left.GetType()} and {right.GetType()}"); } var differences = new List<(PropertyInfo Property, string FQN, object Left, object Right)>(); - foreach (var prop in left?.GetType().GetProperties()) + foreach (var prop in left.GetType().GetProperties()) { var leftVal = prop.GetValue(left); var rightVal = prop.GetValue(right); var propType = prop.PropertyType; var fqn = string.IsNullOrEmpty(parentFqn) ? prop.Name : string.Join(".", parentFqn, prop.Name); + if (leftVal is null || rightVal is null) + { + if (leftVal is null == rightVal is null) + { + continue; + } + + differences.Add((prop, fqn, leftVal, rightVal)); + continue; + } + if (propType.IsArray || (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Dictionary<,>))) { if (leftVal.ToJson() != rightVal.ToJson()) diff --git a/src/slskd/Common/Retry.cs b/src/slskd/Common/Retry.cs index 53aa59db..a14d2def 100644 --- a/src/slskd/Common/Retry.cs +++ b/src/slskd/Common/Retry.cs @@ -52,6 +52,7 @@ namespace slskd /// <param name="maxAttempts">The maximum number of retry attempts.</param> /// <param name="baseDelayInMilliseconds">The base delay in milliseconds.</param> /// <param name="maxDelayInMilliseconds">The maximum delay in milliseconds.</param> + /// <param name="exceptionHistoryLimit">The maximum number of Exceptions to keep in history.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>The execution context.</returns> public static async Task Do( @@ -62,13 +63,14 @@ namespace slskd int maxAttempts = 3, int baseDelayInMilliseconds = 1000, int maxDelayInMilliseconds = int.MaxValue, + int exceptionHistoryLimit = 5, CancellationToken cancellationToken = default) { await Do<object>(async () => { await task(); return Task.FromResult<object>(null); - }, isRetryable, onRetry, onFailure, maxAttempts, baseDelayInMilliseconds, maxDelayInMilliseconds, cancellationToken); + }, isRetryable, onRetry, onFailure, maxAttempts, baseDelayInMilliseconds, maxDelayInMilliseconds, exceptionHistoryLimit, cancellationToken); } /// <summary> @@ -81,6 +83,7 @@ namespace slskd /// <param name="maxAttempts">The maximum number of retry attempts.</param> /// <param name="baseDelayInMilliseconds">The base delay in milliseconds.</param> /// <param name="maxDelayInMilliseconds">The maximum delay in milliseconds.</param> + /// <param name="exceptionHistoryLimit">The maximum number of Exceptions to keep in history.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <typeparam name="T">The Type of the logic return value.</typeparam> /// <returns>The execution context.</returns> @@ -92,11 +95,12 @@ namespace slskd int maxAttempts = 3, int baseDelayInMilliseconds = 1000, int maxDelayInMilliseconds = int.MaxValue, + int exceptionHistoryLimit = 5, CancellationToken cancellationToken = default) { isRetryable ??= (_, _) => true; - var exceptions = new List<Exception>(); + var exceptions = new Queue<Exception>(); for (int attempts = 0; attempts < maxAttempts; attempts++) { @@ -119,7 +123,12 @@ namespace slskd } catch (Exception ex) { - exceptions.Add(ex); + exceptions.Enqueue(ex); + + if (exceptions.Count > exceptionHistoryLimit) + { + exceptions.Dequeue(); + } try { |
