Skip to content

Commit

Permalink
Expose the ResilienceHandler type.
Browse files Browse the repository at this point in the history
Fixes #4759
  • Loading branch information
Martin Taillefer committed Jan 6, 2024
1 parent 1e5ad82 commit c5c956f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<InjectSharedDataValidation>true</InjectSharedDataValidation>
<InjectSharedPools>true</InjectSharedPools>
<InjectSharedDiagnosticIds>true</InjectSharedDiagnosticIds>
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,47 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Http.Diagnostics;
using Microsoft.Extensions.Http.Resilience.Internal;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;
using Polly;

namespace Microsoft.Extensions.Http.Resilience.Internal;
namespace Microsoft.Extensions.Http.Resilience;

/// <summary>
/// Base class for resilience handler, i.e. handlers that use resilience strategies to send the requests.
/// </summary>
internal sealed class ResilienceHandler : DelegatingHandler
[Experimental(diagnosticId: DiagnosticIds.Experiments.Resilience, UrlFormat = DiagnosticIds.UrlFormat)]
public class ResilienceHandler : DelegatingHandler
{
private readonly Func<HttpRequestMessage, ResiliencePipeline<HttpResponseMessage>> _pipelineProvider;

/// <summary>
/// Initializes a new instance of the <see cref="ResilienceHandler"/> class.
/// </summary>
/// <param name="pipelineProvider">The pipeline provider that supplies pipelines in response to an http message.</param>
/// <exception cref="ArgumentNullException">If <paramref name="pipelineProvider"/> is <see langword="null"/>.</exception>
public ResilienceHandler(Func<HttpRequestMessage, ResiliencePipeline<HttpResponseMessage>> pipelineProvider)
{
_pipelineProvider = pipelineProvider;
_pipelineProvider = Throw.IfNull(pipelineProvider);
}

/// <inheritdoc/>
/// <summary>
/// Sends an HTTP request to the inner handler to send to the server as an asynchronous operation.
/// </summary>
/// <param name="request">The HTTP request message to send to the server.</param>
/// <param name="cancellationToken">A cancellation token to cancel operation.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="request"/> is <see langword="null"/>.</exception>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
_ = Throw.IfNull(request);

var pipeline = _pipelineProvider(request);
var created = false;
if (request.GetResilienceContext() is not ResilienceContext context)
Expand Down

0 comments on commit c5c956f

Please sign in to comment.