From 480996433e5f21bd37d7c51ae2e68af52c44cc2e Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 5 Jan 2016 11:33:29 +0000 Subject: [PATCH 1/2] Only queue write when not queued --- .../Http/SocketOutput.cs | 31 +++++-------------- 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs index 5e3f8f5a68..ac79ee0983 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs @@ -16,7 +16,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http { public const int MaxPooledWriteReqs = 1024; - private const int _maxPendingWrites = 3; private const int _maxBytesPreCompleted = 65536; private const int _initialTaskQueues = 64; private const int _maxPooledWriteContexts = 32; @@ -44,7 +43,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http // The number of write operations that have been scheduled so far // but have not completed. - private int _writesPending = 0; + private bool _writePending = false; private int _numBytesPreCompleted = 0; private Exception _lastWriteError; private WriteContext _nextWriteContext; @@ -139,10 +138,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Http _tasksPending.Enqueue(tcs); } - if (_writesPending < _maxPendingWrites && immediate) + if (!_writePending && immediate) { + _writePending = true; scheduleWrite = true; - _writesPending++; } } @@ -258,6 +257,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Http lock (_contextLock) { + _writePending = false; + if (_nextWriteContext != null) { writingContext = _nextWriteContext; @@ -265,26 +266,11 @@ namespace Microsoft.AspNet.Server.Kestrel.Http } else { - _writesPending--; return; } } - try - { - writingContext.DoWriteIfNeeded(); - } - catch - { - lock (_contextLock) - { - // Lock instead of using Interlocked.Decrement so _writesSending - // doesn't change in the middle of executing other synchronized code. - _writesPending--; - } - - throw; - } + writingContext.DoWriteIfNeeded(); } // This is called on the libuv event loop @@ -311,10 +297,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http if (_nextWriteContext != null) { scheduleWrite = true; - } - else - { - _writesPending--; + _writePending = true; } // _numBytesPreCompleted can temporarily go negative in the event there are From e6bc0bc3351ec1bca33b3a6018c043253d6f9a36 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 5 Jan 2016 11:42:41 +0000 Subject: [PATCH 2/2] Only scheduleWrites on the threadpool --- .../Http/SocketOutput.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs index ac79ee0983..42dd1300c0 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs @@ -289,16 +289,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http _connection.Abort(); } - bool scheduleWrite = false; - lock (_contextLock) { PoolWriteContext(writeContext); - if (_nextWriteContext != null) - { - scheduleWrite = true; - _writePending = true; - } // _numBytesPreCompleted can temporarily go negative in the event there are // completed writes that we haven't triggered callbacks for yet. @@ -334,12 +327,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http } _log.ConnectionWriteCallback(_connectionId, status); - - if (scheduleWrite) - { - ScheduleWrite(); - } - _tasksCompleted.Clear(); }