Skip to content
This repository was archived by the owner on Mar 19, 2019. It is now read-only.

Commit

Permalink
#298 Add a timeout for draining requests on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Jan 24, 2017
1 parent e19dea2 commit 56bd85a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Microsoft.AspNetCore.Server.HttpSys/HttpSysOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public long RequestQueueLimit
}
}

/// <summary>
/// The amount of time to wait for active requests to drain while the server is shutting down.
/// New requests will receive a 503 response in this time period. The default is 5 seconds.
/// </summary>
public TimeSpan ShutdownTimeout { get; set; } = TimeSpan.FromSeconds(5);

internal void SetRequestQueueLimit(RequestQueue requestQueue)
{
_requestQueue = requestQueue;
Expand Down
10 changes: 9 additions & 1 deletion src/Microsoft.AspNetCore.Server.HttpSys/MessagePump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,15 @@ public void Dispose()
if (_outstandingRequests > 0)
{
LogHelper.LogInfo(_logger, "Stopping, waiting for " + _outstandingRequests + " request(s) to drain.");
_shutdownSignal.WaitOne();
var drained = _shutdownSignal.WaitOne(Listener.Options.ShutdownTimeout);
if (drained)
{
LogHelper.LogInfo(_logger, "All requests drained successfully.");
}
else
{
LogHelper.LogInfo(_logger, "Timed out, terminating " + _outstandingRequests + " request(s).");
}
}
// All requests are finished
_listener.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task Server_EchoHelloWorld_Success()
}

[ConditionalFact]
public async Task Server_ShutdownDurringRequest_Success()
public async Task Server_ShutdownDuringRequest_Success()
{
Task<string> responseTask;
ManualResetEvent received = new ManualResetEvent(false);
Expand All @@ -87,6 +87,30 @@ public async Task Server_ShutdownDurringRequest_Success()
Assert.Equal("Hello World", response);
}

[ConditionalFact]
public async Task Server_ShutdownDuringLongRunningRequest_TimesOut()
{
Task<string> responseTask;
var received = new ManualResetEvent(false);
bool? shutdown = null;
var waitForShutdown = new ManualResetEvent(false);
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
received.Set();
shutdown = waitForShutdown.WaitOne(TimeSpan.FromSeconds(15));
httpContext.Response.ContentLength = 11;
return httpContext.Response.WriteAsync("Hello World");
}))
{
responseTask = SendRequestAsync(address);
Assert.True(received.WaitOne(TimeSpan.FromSeconds(10)));
}
Assert.False(shutdown.HasValue);
waitForShutdown.Set();
await Assert.ThrowsAsync<HttpRequestException>(async () => await responseTask);
}

[ConditionalFact]
public void Server_AppException_ClientReset()
{
Expand Down

0 comments on commit 56bd85a

Please sign in to comment.