Skip to content

Commit

Permalink
Classify a ConnectTimeout as RequestTimedOut (#1653)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan authored Apr 19, 2022
1 parent 5464f14 commit 44a962f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/ReverseProxy/Forwarder/HttpForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,18 @@ private async ValueTask<ForwarderError> HandleRequestFailureAsync(HttpContext co
{
if (requestException is OperationCanceledException)
{
if (!context.RequestAborted.IsCancellationRequested && requestCancellationSource.IsCancellationRequested)
if (context.RequestAborted.IsCancellationRequested)
{
return await ReportErrorAsync(ForwarderError.RequestTimedOut, StatusCodes.Status504GatewayTimeout);
return await ReportErrorAsync(ForwarderError.RequestCanceled, StatusCodes.Status502BadGateway);
}
else
{
return await ReportErrorAsync(ForwarderError.RequestCanceled, StatusCodes.Status502BadGateway);
#if NET6_0_OR_GREATER
Debug.Assert(requestCancellationSource.IsCancellationRequested || requestException.ToString().Contains("ConnectTimeout"), requestException.ToString());
#else
Debug.Assert(requestCancellationSource.IsCancellationRequested || requestException.ToString().Contains("ConnectHelper"), requestException.ToString());
#endif
return await ReportErrorAsync(ForwarderError.RequestTimedOut, StatusCodes.Status504GatewayTimeout);
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,40 @@ public async Task RequestTimedOut_Returns504()
events.AssertContainProxyStages(new[] { ForwarderStage.SendAsyncStart });
}

[Fact]
public async Task RequestConnectTimedOut_Returns504()
{
var events = TestEventListener.Collect();

var httpContext = new DefaultHttpContext();
httpContext.Request.Method = "GET";
httpContext.Request.Host = new HostString("example.com:3456");

var proxyResponseStream = new MemoryStream();
httpContext.Response.Body = proxyResponseStream;

var destinationPrefix = "https://microsoft.com:123/"; // Port that doesn't accept connections
var sut = CreateProxy();

using var client = new HttpMessageInvoker(new SocketsHttpHandler
{
// Time out immediately
ConnectTimeout = TimeSpan.FromTicks(1)
});

var proxyError = await sut.SendAsync(httpContext, destinationPrefix, client);

Assert.Equal(ForwarderError.RequestTimedOut, proxyError);
Assert.Equal(StatusCodes.Status504GatewayTimeout, httpContext.Response.StatusCode);
Assert.Equal(0, proxyResponseStream.Length);
var errorFeature = httpContext.Features.Get<IForwarderErrorFeature>();
Assert.Equal(ForwarderError.RequestTimedOut, errorFeature.Error);
Assert.IsAssignableFrom<OperationCanceledException>(errorFeature.Exception);

AssertProxyStartFailedStop(events, destinationPrefix, httpContext.Response.StatusCode, errorFeature.Error);
events.AssertContainProxyStages(new[] { ForwarderStage.SendAsyncStart });
}

[Fact]
public async Task RequestCanceled_Returns502()
{
Expand Down

0 comments on commit 44a962f

Please sign in to comment.