Skip to content

Commit

Permalink
Disable HttpClient's timeout for Standard Resilience and Hedging (#4862)
Browse files Browse the repository at this point in the history
* Disable HttpClient's timeout for Standard Hedging and Standard Resilience
  • Loading branch information
martintmk authored Jan 8, 2024
1 parent 9dc33c2 commit 3654748
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Net.Http;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http.Resilience;
Expand Down Expand Up @@ -139,6 +140,9 @@ public static IStandardHedgingHandlerBuilder AddStandardHedgingHandler(this IHtt
})
.SelectPipelineByAuthority();

// Disable the HttpClient timeout to allow the timeout strategies to control the timeout.
_ = builder.ConfigureHttpClient(client => client.Timeout = Timeout.InfiniteTimeSpan);

return new StandardHedgingHandlerBuilder(builder.Name, builder.Services, routingBuilder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Threading;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http.Resilience;
Expand Down Expand Up @@ -86,6 +87,9 @@ public static IHttpStandardResiliencePipelineBuilder AddStandardResilienceHandle
.AddTimeout(options.AttemptTimeout);
});

// Disable the HttpClient timeout to allow the timeout strategies to control the timeout.
_ = builder.ConfigureHttpClient(client => client.Timeout = Timeout.InfiniteTimeSpan);

return new HttpStandardResiliencePipelineBuilder(optionsName, builder.Services);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ public async Task DynamicReloads_Ok()
AssertNoResponse();
}

[Fact]
public void AddStandardResilienceHandler_EnsureHttpClientTimeoutDisabled()
{
var client = CreateClientWithHandler();

client.Timeout.Should().Be(Timeout.InfiniteTimeSpan);
}

[Fact]
public async Task NoRouting_Ok()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -234,6 +235,16 @@ public async Task DynamicReloads_Ok()
requests.Should().HaveCount(11);
}

[Fact]
public void AddStandardResilienceHandler_EnsureHttpClientTimeoutDisabled()
{
var builder = new ServiceCollection().AddLogging().AddMetrics().AddHttpClient("test").AddStandardHedgingHandler();

using var client = builder.Services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>().CreateClient("test");

client.Timeout.Should().Be(Timeout.InfiniteTimeSpan);
}

private static void AddStandardResilienceHandler(
MethodArgs mode,
IHttpClientBuilder builder,
Expand Down

0 comments on commit 3654748

Please sign in to comment.