This repository was archived by the owner on Mar 19, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +40
-2
lines changed
src/Microsoft.AspNetCore.Server.HttpSys
test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,12 @@ public long RequestQueueLimit
81
81
}
82
82
}
83
83
84
+ /// <summary>
85
+ /// The amount of time to wait for active requests to drain while the server is shutting down.
86
+ /// New requests will receive a 503 response in this time period. The default is 5 seconds.
87
+ /// </summary>
88
+ public TimeSpan ShutdownTimeout { get ; set ; } = TimeSpan . FromSeconds ( 5 ) ;
89
+
84
90
internal void SetRequestQueueLimit ( RequestQueue requestQueue )
85
91
{
86
92
_requestQueue = requestQueue ;
Original file line number Diff line number Diff line change @@ -221,7 +221,15 @@ public void Dispose()
221
221
if ( _outstandingRequests > 0 )
222
222
{
223
223
LogHelper . LogInfo ( _logger , "Stopping, waiting for " + _outstandingRequests + " request(s) to drain." ) ;
224
- _shutdownSignal . WaitOne ( ) ;
224
+ var drained = _shutdownSignal . WaitOne ( Listener . Options . ShutdownTimeout ) ;
225
+ if ( drained )
226
+ {
227
+ LogHelper . LogInfo ( _logger , "All requests drained successfully." ) ;
228
+ }
229
+ else
230
+ {
231
+ LogHelper . LogInfo ( _logger , "Timed out, terminating " + _outstandingRequests + " request(s)." ) ;
232
+ }
225
233
}
226
234
// All requests are finished
227
235
_listener . Dispose ( ) ;
Original file line number Diff line number Diff line change @@ -68,7 +68,7 @@ public async Task Server_EchoHelloWorld_Success()
68
68
}
69
69
70
70
[ ConditionalFact ]
71
- public async Task Server_ShutdownDurringRequest_Success ( )
71
+ public async Task Server_ShutdownDuringRequest_Success ( )
72
72
{
73
73
Task < string > responseTask ;
74
74
ManualResetEvent received = new ManualResetEvent ( false ) ;
@@ -87,6 +87,30 @@ public async Task Server_ShutdownDurringRequest_Success()
87
87
Assert . Equal ( "Hello World" , response ) ;
88
88
}
89
89
90
+ [ ConditionalFact ]
91
+ public async Task Server_ShutdownDuringLongRunningRequest_TimesOut ( )
92
+ {
93
+ Task < string > responseTask ;
94
+ var received = new ManualResetEvent ( false ) ;
95
+ bool ? shutdown = null ;
96
+ var waitForShutdown = new ManualResetEvent ( false ) ;
97
+ string address ;
98
+ using ( Utilities . CreateHttpServer ( out address , httpContext =>
99
+ {
100
+ received . Set ( ) ;
101
+ shutdown = waitForShutdown . WaitOne ( TimeSpan . FromSeconds ( 15 ) ) ;
102
+ httpContext . Response . ContentLength = 11 ;
103
+ return httpContext . Response . WriteAsync ( "Hello World" ) ;
104
+ } ) )
105
+ {
106
+ responseTask = SendRequestAsync ( address ) ;
107
+ Assert . True ( received . WaitOne ( TimeSpan . FromSeconds ( 10 ) ) ) ;
108
+ }
109
+ Assert . False ( shutdown . HasValue ) ;
110
+ waitForShutdown . Set ( ) ;
111
+ await Assert . ThrowsAsync < HttpRequestException > ( async ( ) => await responseTask ) ;
112
+ }
113
+
90
114
[ ConditionalFact ]
91
115
public void Server_AppException_ClientReset ( )
92
116
{
You can’t perform that action at this time.
0 commit comments