Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the ResilienceHandler type. #4858

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
geeknoid marked this conversation as resolved.
Show resolved Hide resolved
{
_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)
geeknoid marked this conversation as resolved.
Show resolved Hide resolved
{
_ = 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