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

Commit ea91e1a

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 1c320d7 commit ea91e1a

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ namespace Microsoft.AspNet.Server.Kestrel
1818
/// </summary>
1919
public class KestrelThread
2020
{
21+
<<<<<<< 1c320d7a743b2448d6e96b1ddc14c7ca865261a5
2122
private static Action<object, object> _threadCallbackAdapter = (callback, state) => ((Action<KestrelThread>)callback).Invoke((KestrelThread)state);
23+
=======
24+
// maximum times the work queues swapped and are processed in a single pass
25+
// as completing a task may immediately have write data to put on the network
26+
// otherwise it needs to wait till the next pass of the libuv loop
27+
private const int _maxLoops = 8;
28+
29+
private static Action<object, object> _objectCallbackAdapter = (callback, state) => ((Action<object>)callback).Invoke(state);
30+
>>>>>>> Process cascaded work immediately
2231
private KestrelEngine _engine;
2332
private readonly IApplicationLifetime _appLifetime;
2433
private Thread _thread;
@@ -249,11 +258,17 @@ private void ThreadStart(object parameter)
249258

250259
private void OnPost()
251260
{
252-
DoPostWork();
253-
DoPostCloseHandle();
261+
var loopsRemaining = _maxLoops;
262+
bool wasWork;
263+
do
264+
{
265+
wasWork = DoPostWork();
266+
wasWork = DoPostCloseHandle() || wasWork;
267+
loopsRemaining--;
268+
} while (wasWork && loopsRemaining > 0);
254269
}
255270

256-
private void DoPostWork()
271+
private bool DoPostWork()
257272
{
258273
Queue<Work> queue;
259274
lock (_workSync)
@@ -262,6 +277,9 @@ private void DoPostWork()
262277
_workAdding = _workRunning;
263278
_workRunning = queue;
264279
}
280+
281+
bool wasWork = queue.Count > 0;
282+
265283
while (queue.Count != 0)
266284
{
267285
var work = queue.Dequeue();
@@ -286,8 +304,10 @@ private void DoPostWork()
286304
}
287305
}
288306
}
307+
308+
return wasWork;
289309
}
290-
private void DoPostCloseHandle()
310+
private bool DoPostCloseHandle()
291311
{
292312
Queue<CloseHandle> queue;
293313
lock (_workSync)
@@ -296,6 +316,9 @@ private void DoPostCloseHandle()
296316
_closeHandleAdding = _closeHandleRunning;
297317
_closeHandleRunning = queue;
298318
}
319+
320+
bool wasWork = queue.Count > 0;
321+
299322
while (queue.Count != 0)
300323
{
301324
var closeHandle = queue.Dequeue();
@@ -309,6 +332,8 @@ private void DoPostCloseHandle()
309332
throw;
310333
}
311334
}
335+
336+
return wasWork;
312337
}
313338

314339
private struct Work

0 commit comments

Comments
 (0)