| | | 1 | | using Elsa.Http.Extensions; |
| | | 2 | | using Elsa.Resilience; |
| | | 3 | | using Polly; |
| | | 4 | | using Polly.Retry; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Http.Resilience; |
| | | 7 | | |
| | | 8 | | [ResilienceCategory("HTTP")] |
| | | 9 | | public class HttpResilienceStrategy : IResilienceStrategy |
| | | 10 | | { |
| | 3 | 11 | | public string Id { get; set; } = null!; |
| | 3 | 12 | | public string DisplayName { get; set; } = null!; |
| | 4 | 13 | | public int MaxRetryAttempts { get; set; } = 3; |
| | 2 | 14 | | public bool UseJitter { get; set; } |
| | 4 | 15 | | public TimeSpan Delay { get; set; } = TimeSpan.FromSeconds(1); |
| | 3 | 16 | | public DelayBackoffType BackoffType { get; set; } = DelayBackoffType.Exponential; |
| | | 17 | | |
| | | 18 | | public Task ConfigurePipeline<T>(ResiliencePipelineBuilder<T> pipelineBuilder, ResilienceContext context) |
| | | 19 | | { |
| | 1 | 20 | | if (typeof(T) != typeof(HttpResponseMessage)) |
| | 0 | 21 | | throw new NotSupportedException($"{nameof(HttpResilienceStrategy)} only supports HttpResponseMessage."); |
| | | 22 | | |
| | 1 | 23 | | var options = new RetryStrategyOptions<T> |
| | 1 | 24 | | { |
| | 1 | 25 | | ShouldHandle = new PredicateBuilder<T>() |
| | 1 | 26 | | .Handle<TimeoutException>() |
| | 1 | 27 | | .Handle<HttpRequestException>() |
| | 3 | 28 | | .HandleResult(response => ((HttpResponseMessage)(object)response!).StatusCode.IsTransientStatusCode()), |
| | 1 | 29 | | MaxRetryAttempts = MaxRetryAttempts, |
| | 1 | 30 | | Delay = Delay, |
| | 1 | 31 | | UseJitter = UseJitter, |
| | 1 | 32 | | BackoffType = BackoffType, |
| | 1 | 33 | | Name = DisplayName |
| | 1 | 34 | | }; |
| | | 35 | | |
| | 1 | 36 | | pipelineBuilder.AddRetry(options); |
| | 1 | 37 | | return Task.CompletedTask; |
| | | 38 | | } |
| | | 39 | | } |