Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 4beec24

Browse files
committed
Process cascaded work immediately
Without waiting for next libuv pass Fix for potential regression in #363 due to bug fix.
1 parent 07c0b41 commit 4beec24

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ namespace Microsoft.AspNet.Server.Kestrel
1818
/// </summary>
1919
public class KestrelThread
2020
{
21+
// maximum times the work queues swapped and are processed in a single pass
22+
// as completing a task may immediately have write data to put on the network
23+
// otherwise it needs to wait till the next pass of the libuv loop
24+
private const int _maxLoops = 8;
25+
2126
private static Action<object, object> _objectCallbackAdapter = (callback, state) => ((Action<object>)callback).Invoke(state);
2227
private KestrelEngine _engine;
2328
private readonly IApplicationLifetime _appLifetime;
@@ -262,11 +267,17 @@ private void ThreadStart(object parameter)
262267

263268
private void OnPost()
264269
{
265-
DoPostWork();
266-
DoPostCloseHandle();
270+
var loopsRemaining = _maxLoops;
271+
bool wasWork = true;
272+
do
273+
{
274+
wasWork = DoPostWork();
275+
wasWork = DoPostCloseHandle() || wasWork;
276+
loopsRemaining--;
277+
} while (wasWork && loopsRemaining > 0);
267278
}
268279

269-
private void DoPostWork()
280+
private bool DoPostWork()
270281
{
271282
Queue<Work> queue;
272283
lock (_workSync)
@@ -275,6 +286,9 @@ private void DoPostWork()
275286
_workAdding = _workRunning;
276287
_workRunning = queue;
277288
}
289+
290+
bool wasWork = queue.Count > 0;
291+
278292
while (queue.Count != 0)
279293
{
280294
var work = queue.Dequeue();
@@ -304,8 +318,10 @@ private void DoPostWork()
304318
}
305319
}
306320
}
321+
322+
return wasWork;
307323
}
308-
private void DoPostCloseHandle()
324+
private bool DoPostCloseHandle()
309325
{
310326
Queue<CloseHandle> queue;
311327
lock (_workSync)
@@ -314,6 +330,9 @@ private void DoPostCloseHandle()
314330
_closeHandleAdding = _closeHandleRunning;
315331
_closeHandleRunning = queue;
316332
}
333+
334+
bool wasWork = queue.Count > 0;
335+
317336
while (queue.Count != 0)
318337
{
319338
var closeHandle = queue.Dequeue();
@@ -327,6 +346,8 @@ private void DoPostCloseHandle()
327346
throw;
328347
}
329348
}
349+
350+
return wasWork;
330351
}
331352

332353
private struct Work

0 commit comments

Comments
 (0)