diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-07-02 08:26:14 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-02 08:26:14 -0500 |
| commit | 6e4166314c7cb9ce51f5c6ecff72258be26524b0 (patch) | |
| tree | 3f67ea09cfb52e4108bf2f00dc8b40744b66964e /src/slskd/Common | |
| parent | 2ea8ed330d13c972c4cc36b13327701e6f4762d9 (diff) | |
| parent | 32cd804dd3e13967a14316ae76ae2ce71c3de936 (diff) | |
| download | slskd-6e4166314c7cb9ce51f5c6ecff72258be26524b0.tar.gz | |
Merge pull request #109 from slskd/ftp
Add the ability to upload files to an FTP after downloads complete
Diffstat (limited to 'src/slskd/Common')
| -rw-r--r-- | src/slskd/Common/Retry.cs | 99 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/EnumAttribute.cs | 47 |
2 files changed, 146 insertions, 0 deletions
diff --git a/src/slskd/Common/Retry.cs b/src/slskd/Common/Retry.cs new file mode 100644 index 00000000..eccb6b62 --- /dev/null +++ b/src/slskd/Common/Retry.cs @@ -0,0 +1,99 @@ +// <copyright file="Retry.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + using System; + using System.Collections.Generic; + using System.Threading.Tasks; + + /// <summary> + /// Retry logic. + /// </summary> + public static class Retry + { + /// <summary> + /// Executes logic with the specified retry parameters. + /// </summary> + /// <param name="task">The logic to execute.</param> + /// <param name="isRetryable">A function returning a value indicating whether the last Exception is retryable.</param> + /// <param name="onFailure">An action to execute on failure.</param> + /// <param name="maxAttempts">The maximum number of retry attempts.</param> + /// <param name="maxDelayInMilliseconds">The maximum delay in milliseconds.</param> + /// <returns>The execution context.</returns> + public static async Task Do(Func<Task> task, Func<int, Exception, bool> isRetryable = null, Action<int, Exception> onFailure = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue) + { + await Do<object>(async () => + { + await task(); + return Task.FromResult<object>(null); + }, isRetryable, onFailure, maxAttempts, maxDelayInMilliseconds); + } + + /// <summary> + /// Executes logic with the specified retry parameters. + /// </summary> + /// <param name="task">The logic to execute.</param> + /// <param name="isRetryable">A function returning a value indicating whether the last Exception is retryable.</param> + /// <param name="onFailure">An action to execute on failure.</param> + /// <param name="maxAttempts">The maximum number of retry attempts.</param> + /// <param name="maxDelayInMilliseconds">The maximum delay in miliseconds.</param> + /// <typeparam name="T">The Type of the logic return value.</typeparam> + /// <returns>The execution context.</returns> + public static async Task<T> Do<T>(Func<Task<T>> task, Func<int, Exception, bool> isRetryable = null, Action<int, Exception> onFailure = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue) + { + isRetryable ??= (_, _) => true; + onFailure ??= (_, _) => { }; + + var exceptions = new List<Exception>(); + + for (int attempts = 0; attempts < maxAttempts; attempts++) + { + try + { + if (attempts > 0) + { + var (delay, jitter) = Compute.ExponentialBackoffDelay(attempts, maxDelayInMilliseconds); + await Task.Delay(delay + jitter); + } + + return await task(); + } + catch (Exception ex) + { + exceptions.Add(ex); + + try + { + onFailure(attempts + 1, ex); + + if (!isRetryable(attempts + 1, ex)) + { + break; + } + } + catch (Exception retryEx) + { + throw new RetryException($"Failed to retry operation: {retryEx.Message}", ex); + } + } + } + + throw new AggregateException(exceptions); + } + } +}
\ No newline at end of file diff --git a/src/slskd/Common/Validation/EnumAttribute.cs b/src/slskd/Common/Validation/EnumAttribute.cs new file mode 100644 index 00000000..ad9b393c --- /dev/null +++ b/src/slskd/Common/Validation/EnumAttribute.cs @@ -0,0 +1,47 @@ +// <copyright file="EnumAttribute.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd.Validation +{ + using System; + using System.ComponentModel.DataAnnotations; + + /// <summary> + /// Validates that the value is a valid nember of the specified <see cref="Enum"/>. + /// </summary> + public class EnumAttribute : ValidationAttribute + { + public EnumAttribute(Type targetType, bool ignoreCase = true) + { + TargetType = targetType; + IgnoreCase = ignoreCase; + } + + private Type TargetType { get; set; } + private bool IgnoreCase { get; set; } + + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if (value != null && !Enum.TryParse(TargetType, value.ToString(), IgnoreCase, out _)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be one of: {string.Join(", ", Enum.GetNames(TargetType))}. Case {(IgnoreCase ? "insensitive" : "sensitive")}."); + } + + return ValidationResult.Success; + } + } +}
\ No newline at end of file |
