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

Detect connection timeouts in Hedging #5134

Merged
merged 7 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -2,7 +2,10 @@
// 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 Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;
using Polly;
using Polly.CircuitBreaker;
Expand All @@ -17,13 +20,26 @@ public static class HttpClientHedgingResiliencePredicates
/// <summary>
/// Determines whether an outcome should be treated by hedging as a transient failure.
/// </summary>
/// <param name="outcome">The outcome of the user-specified callback.</param>
/// <returns><see langword="true"/> if outcome is transient, <see langword="false"/> if not.</returns>
public static bool IsTransient(Outcome<HttpResponseMessage> outcome) => outcome switch
{
{ Result: { } response } when HttpClientResiliencePredicates.IsTransientHttpFailure(response) => true,
{ Exception: { } exception } when IsTransientHttpException(exception) => true,
_ => false,
};
public static bool IsTransient(Outcome<HttpResponseMessage> outcome)
xakep139 marked this conversation as resolved.
Show resolved Hide resolved
=> outcome switch
{
{ Result: { } response } when HttpClientResiliencePredicates.IsTransientHttpFailure(response) => true,
{ Exception: { } exception } when IsTransientHttpException(exception) => true,
_ => false,
};

/// <summary>
/// Determines whether an <see cref="HttpResponseMessage"/> should be treated by hedging as a transient failure.
/// </summary>
/// <param name="outcome">The outcome of the user-specified callback.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> associated with the execution.</param>
/// <returns><see langword="true"/> if outcome is transient, <see langword="false"/> if not.</returns>
[Experimental(diagnosticId: DiagnosticIds.Experiments.Resilience, UrlFormat = DiagnosticIds.UrlFormat)]
public static bool IsTransient(Outcome<HttpResponseMessage> outcome, CancellationToken cancellationToken)
=> HttpClientResiliencePredicates.IsHttpConnectionTimeout(outcome, cancellationToken)
|| IsTransient(outcome);

/// <summary>
/// Determines whether an exception should be treated by hedging as a transient failure.
Expand All @@ -35,8 +51,7 @@ internal static bool IsTransientHttpException(Exception exception)
return exception switch
{
BrokenCircuitException => true,
_ when HttpClientResiliencePredicates.IsTransientHttpException(exception) => true,
_ => false,
_ => HttpClientResiliencePredicates.IsTransientHttpException(exception),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public class HttpHedgingStrategyOptions : HedgingStrategyOptions<HttpResponseMes
/// Initializes a new instance of the <see cref="HttpHedgingStrategyOptions"/> class.
/// </summary>
/// <remarks>
/// By default the options is set to handle only transient failures,
/// By default, the options is set to handle only transient failures,
/// i.e. timeouts, 5xx responses and <see cref="HttpRequestException"/> exceptions.
/// </remarks>
public HttpHedgingStrategyOptions()
{
ShouldHandle = args => new ValueTask<bool>(HttpClientHedgingResiliencePredicates.IsTransient(args.Outcome));
ShouldHandle = args => new ValueTask<bool>(HttpClientHedgingResiliencePredicates.IsTransient(args.Outcome, args.Context.CancellationToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using System.Threading;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;
using Polly;
using Polly.Timeout;
Expand All @@ -26,17 +29,32 @@ public static class HttpClientResiliencePredicates
_ => false
};

/// <summary>
/// Determines whether an <see cref="HttpResponseMessage"/> should be treated by resilience strategies as a transient failure.
/// </summary>
/// <param name="outcome">The outcome of the user-specified callback.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> associated with the execution.</param>
/// <returns><see langword="true"/> if outcome is transient, <see langword="false"/> if not.</returns>
[Experimental(diagnosticId: DiagnosticIds.Experiments.Resilience, UrlFormat = DiagnosticIds.UrlFormat)]
public static bool IsTransient(Outcome<HttpResponseMessage> outcome, CancellationToken cancellationToken)
=> IsHttpConnectionTimeout(outcome, cancellationToken)
|| IsTransient(outcome);

/// <summary>
/// Determines whether an exception should be treated by resilience strategies as a transient failure.
/// </summary>
internal static bool IsTransientHttpException(Exception exception)
{
_ = Throw.IfNull(exception);

return exception is HttpRequestException ||
exception is TimeoutRejectedException;
return exception is HttpRequestException or TimeoutRejectedException;
}

internal static bool IsHttpConnectionTimeout(in Outcome<HttpResponseMessage> outcome, in CancellationToken cancellationToken)
=> !cancellationToken.IsCancellationRequested
&& outcome.Exception is OperationCanceledException { Source: "System.Private.CoreLib" }
&& outcome.Exception.InnerException is TimeoutException;

/// <summary>
/// Determines whether a response contains a transient failure.
/// </summary>
Expand All @@ -52,7 +70,6 @@ internal static bool IsTransientHttpFailure(HttpResponseMessage response)
return statusCode >= InternalServerErrorCode ||
response.StatusCode == HttpStatusCode.RequestTimeout ||
statusCode == TooManyRequests;

}

private const int InternalServerErrorCode = (int)HttpStatusCode.InternalServerError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class HttpRetryStrategyOptions : RetryStrategyOptions<HttpResponseMessage
/// </remarks>
public HttpRetryStrategyOptions()
{
ShouldHandle = args => new ValueTask<bool>(HttpClientResiliencePredicates.IsTransient(args.Outcome));
ShouldHandle = args => new ValueTask<bool>(HttpClientResiliencePredicates.IsTransient(args.Outcome, args.Context.CancellationToken));
BackoffType = DelayBackoffType.Exponential;
ShouldRetryAfterHeader = true;
UseJitter = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public abstract class HedgingTests<TBuilder> : IDisposable
private readonly Func<RequestRoutingStrategy> _requestRoutingStrategyFactory;
private readonly IServiceCollection _services;
private readonly Queue<HttpResponseMessage> _responses = new();
private ServiceProvider? _serviceProvider;
private bool _failure;

private protected HedgingTests(Func<IHttpClientBuilder, Func<RequestRoutingStrategy>, TBuilder> createDefaultBuilder)
Expand Down Expand Up @@ -63,6 +64,11 @@ public void Dispose()
_requestRoutingStrategyMock.VerifyAll();
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
_serviceProvider?.Dispose();
foreach (var response in _responses)
{
response.Dispose();
}
}

[Fact]
Expand Down Expand Up @@ -93,7 +99,7 @@ public async Task SendAsync_EnsureContextFlows()

using var client = CreateClientWithHandler();

await client.SendAsync(request, _cancellationTokenSource.Token);
using var _ = await client.SendAsync(request, _cancellationTokenSource.Token);

Assert.Equal(2, calls);
}
Expand All @@ -108,7 +114,7 @@ public async Task SendAsync_NoErrors_ShouldReturnSingleResponse()

AddResponse(HttpStatusCode.OK);

var response = await client.SendAsync(request, _cancellationTokenSource.Token);
using var _ = await client.SendAsync(request, _cancellationTokenSource.Token);
AssertNoResponse();

Assert.Single(Requests);
Expand Down Expand Up @@ -164,7 +170,7 @@ public async Task SendAsync_NoRoutesLeftAndSomeResultPresent_ShouldReturn()

using var client = CreateClientWithHandler();

var result = await client.SendAsync(request, _cancellationTokenSource.Token);
using var result = await client.SendAsync(request, _cancellationTokenSource.Token);
Assert.Equal(DefaultHedgingAttempts + 1, Requests.Count);
Assert.Equal(HttpStatusCode.ServiceUnavailable, result.StatusCode);
}
Expand All @@ -183,7 +189,7 @@ public async Task SendAsync_EnsureDistinctContextForEachAttempt()

using var client = CreateClientWithHandler();

await client.SendAsync(request, _cancellationTokenSource.Token);
using var _ = await client.SendAsync(request, _cancellationTokenSource.Token);

RequestContexts.Distinct().OfType<ResilienceContext>().Should().HaveCount(3);
}
Expand All @@ -204,7 +210,7 @@ public async Task SendAsync_EnsureContextReplacedInRequestMessage()

using var client = CreateClientWithHandler();

await client.SendAsync(request, _cancellationTokenSource.Token);
using var _ = await client.SendAsync(request, _cancellationTokenSource.Token);

RequestContexts.Distinct().OfType<ResilienceContext>().Should().HaveCount(3);

Expand All @@ -226,7 +232,7 @@ public async Task SendAsync_NoRoutesLeft_EnsureLessThanMaxHedgedAttempts()

using var client = CreateClientWithHandler();

var result = await client.SendAsync(request, _cancellationTokenSource.Token);
using var _ = await client.SendAsync(request, _cancellationTokenSource.Token);
Assert.Equal(2, Requests.Count);
}

Expand All @@ -244,7 +250,7 @@ public async Task SendAsync_FailedExecution_ShouldReturnResponseFromHedging()

using var client = CreateClientWithHandler();

var result = await client.SendAsync(request, _cancellationTokenSource.Token);
using var result = await client.SendAsync(request, _cancellationTokenSource.Token);
Assert.Equal(3, Requests.Count);
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal("https://enpoint-1:80/some-path?query", Requests[0]);
Expand All @@ -268,7 +274,12 @@ protected void AddResponse(HttpStatusCode statusCode, int count)

protected abstract void ConfigureHedgingOptions(Action<HttpHedgingStrategyOptions> configure);

protected HttpClient CreateClientWithHandler() => _services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>().CreateClient(ClientId);
protected HttpClient CreateClientWithHandler()
{
_serviceProvider?.Dispose();
_serviceProvider = _services.BuildServiceProvider();
return _serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(ClientId);
}

private Task<HttpResponseMessage> InnerHandlerFunction(HttpRequestMessage request, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.Extensions.Http.Resilience.Test.Hedging;

internal sealed class OperationCanceledExceptionMock : OperationCanceledException
{
public OperationCanceledExceptionMock(Exception innerException)
: base(null, innerException)
{
}

public override string? Source { get => "System.Private.CoreLib"; set => base.Source = value; }
}
Loading