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 8, 2024
1 parent 1e5ad82 commit c39103c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 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,58 @@
// 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.
/// 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>
/// Initializes a new instance of the <see cref="ResilienceHandler"/> class.
/// </summary>
/// <param name="pipeline">The pipeline to use for the message.</param>
/// <exception cref="ArgumentNullException">If <paramref name="pipeline"/> is <see langword="null"/>.</exception>
public ResilienceHandler(ResiliencePipeline<HttpResponseMessage> pipeline)
{
_ = Throw.IfNull(pipeline);
_pipelineProvider = _ => pipeline;
}

/// <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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ResilienceHandlerTest
[Theory]
public async Task SendAsync_EnsureRequestMetadataFlows(bool resilienceContextSet)
{
using var handler = new ResilienceHandler(_ => ResiliencePipeline<HttpResponseMessage>.Empty);
using var handler = new ResilienceHandler(ResiliencePipeline<HttpResponseMessage>.Empty);
using var invoker = new HttpMessageInvoker(handler);
using var request = new HttpRequestMessage();

Expand Down

0 comments on commit c39103c

Please sign in to comment.