// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using System.Net.Http; using System.Threading.Tasks; using Microsoft.Extensions.Http.Resilience.Internal; using Polly; using Polly.Retry; namespace Microsoft.Extensions.Http.Resilience; /// /// Implementation of the for results. /// public class HttpRetryStrategyOptions : RetryStrategyOptions { private bool _shouldRetryAfterHeader; /// /// Initializes a new instance of the class. /// /// /// By default, the options are set to handle only transient failures, /// that is, timeouts, 5xx responses, and exceptions. /// public HttpRetryStrategyOptions() { ShouldHandle = args => new ValueTask(HttpClientResiliencePredicates.IsTransient(args.Outcome)); BackoffType = DelayBackoffType.Exponential; ShouldRetryAfterHeader = true; UseJitter = true; } /// /// Gets or sets a value indicating whether to use the Retry-After header for the retry delays. /// /// /// Defaults to . /// /// /// If the property is set to then the generator will resolve the delay /// based on the Retry-After header rules, otherwise it will return and the retry strategy /// delay will generate the delay based on the configured options. /// public bool ShouldRetryAfterHeader { get => _shouldRetryAfterHeader; set { _shouldRetryAfterHeader = value; if (_shouldRetryAfterHeader) { DelayGenerator = args => args.Outcome.Result switch { HttpResponseMessage response when RetryAfterHelper.TryParse(response, TimeProvider.System, out var retryAfter) => new ValueTask(retryAfter), _ => new ValueTask((TimeSpan?)null) }; } else { DelayGenerator = null; } } } }