Merged PR 11157: Run callbacks outside of locks

This commit is contained in:
Brennan Conroy 2020-12-10 00:19:53 +00:00
parent e47cbf6de6
commit 8e57b5e3b6
1 changed files with 18 additions and 8 deletions

View File

@ -89,6 +89,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
return; return;
} }
var timeout = false;
lock (_readTimingLock) lock (_readTimingLock)
{ {
if (!_readTimingEnabled) if (!_readTimingEnabled)
@ -105,10 +107,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
var elapsedSeconds = (double)_readTimingElapsedTicks / TimeSpan.TicksPerSecond; var elapsedSeconds = (double)_readTimingElapsedTicks / TimeSpan.TicksPerSecond;
var rate = _readTimingBytesRead / elapsedSeconds; var rate = _readTimingBytesRead / elapsedSeconds;
if (rate < _minReadRate.BytesPerSecond && !Debugger.IsAttached) timeout = rate < _minReadRate.BytesPerSecond && !Debugger.IsAttached;
{
_timeoutHandler.OnTimeout(TimeoutReason.ReadDataRate);
}
} }
// PauseTimingReads() cannot just set _timingReads to false. It needs to go through at least one tick // PauseTimingReads() cannot just set _timingReads to false. It needs to go through at least one tick
@ -120,10 +119,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
_readTimingPauseRequested = false; _readTimingPauseRequested = false;
} }
} }
if (timeout)
{
// Run callbacks outside of the lock
_timeoutHandler.OnTimeout(TimeoutReason.ReadDataRate);
}
} }
private void CheckForWriteDataRateTimeout(long timestamp) private void CheckForWriteDataRateTimeout(long timestamp)
{ {
var timeout = false;
lock (_writeTimingLock) lock (_writeTimingLock)
{ {
// Assume overly long tick intervals are the result of server resource starvation. // Assume overly long tick intervals are the result of server resource starvation.
@ -135,10 +142,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
_writeTimingTimeoutTimestamp += extraTimeForTick; _writeTimingTimeoutTimestamp += extraTimeForTick;
} }
if (_concurrentAwaitingWrites > 0 && timestamp > _writeTimingTimeoutTimestamp && !Debugger.IsAttached) timeout = _concurrentAwaitingWrites > 0 && timestamp > _writeTimingTimeoutTimestamp && !Debugger.IsAttached;
{ }
_timeoutHandler.OnTimeout(TimeoutReason.WriteDataRate);
} if (timeout)
{
// Run callbacks outside of the lock
_timeoutHandler.OnTimeout(TimeoutReason.WriteDataRate);
} }
} }