Use a single lock for writing to ConcurrentPipeWriter (#12285)
This commit is contained in:
parent
cae31a55f9
commit
7bed5df4d0
|
|
@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
MemoryPool<byte> memoryPool)
|
MemoryPool<byte> memoryPool)
|
||||||
{
|
{
|
||||||
// Allow appending more data to the PipeWriter when a flush is pending.
|
// Allow appending more data to the PipeWriter when a flush is pending.
|
||||||
_pipeWriter = new ConcurrentPipeWriter(pipeWriter, memoryPool);
|
_pipeWriter = new ConcurrentPipeWriter(pipeWriter, memoryPool, _contextLock);
|
||||||
_connectionId = connectionId;
|
_connectionId = connectionId;
|
||||||
_connectionContext = connectionContext;
|
_connectionContext = connectionContext;
|
||||||
_log = log;
|
_log = log;
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2
|
||||||
IKestrelTrace log)
|
IKestrelTrace log)
|
||||||
{
|
{
|
||||||
// Allow appending more data to the PipeWriter when a flush is pending.
|
// Allow appending more data to the PipeWriter when a flush is pending.
|
||||||
_outputWriter = new ConcurrentPipeWriter(outputPipeWriter, memoryPool);
|
_outputWriter = new ConcurrentPipeWriter(outputPipeWriter, memoryPool, _writeLock);
|
||||||
_connectionContext = connectionContext;
|
_connectionContext = connectionContext;
|
||||||
_http2Connection = http2Connection;
|
_http2Connection = http2Connection;
|
||||||
_connectionOutputFlowControl = connectionOutputFlowControl;
|
_connectionOutputFlowControl = connectionOutputFlowControl;
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2
|
||||||
|
|
||||||
var pipe = CreateDataPipe(pool);
|
var pipe = CreateDataPipe(pool);
|
||||||
|
|
||||||
_pipeWriter = new ConcurrentPipeWriter(pipe.Writer, pool);
|
_pipeWriter = new ConcurrentPipeWriter(pipe.Writer, pool, _dataWriterLock);
|
||||||
_pipeReader = pipe.Reader;
|
_pipeReader = pipe.Reader;
|
||||||
|
|
||||||
// No need to pass in timeoutControl here, since no minDataRates are passed to the TimingPipeFlusher.
|
// No need to pass in timeoutControl here, since no minDataRates are passed to the TimingPipeFlusher.
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
|
||||||
|
|
||||||
private static readonly Exception _successfullyCompletedSentinel = new Exception();
|
private static readonly Exception _successfullyCompletedSentinel = new Exception();
|
||||||
|
|
||||||
private readonly object _sync = new object();
|
private readonly object _sync;
|
||||||
private readonly PipeWriter _innerPipeWriter;
|
private readonly PipeWriter _innerPipeWriter;
|
||||||
private readonly MemoryPool<byte> _pool;
|
private readonly MemoryPool<byte> _pool;
|
||||||
private readonly BufferSegmentStack _bufferSegmentPool = new BufferSegmentStack(InitialSegmentPoolSize);
|
private readonly BufferSegmentStack _bufferSegmentPool = new BufferSegmentStack(InitialSegmentPoolSize);
|
||||||
|
|
@ -51,15 +51,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
|
||||||
private bool _aborted;
|
private bool _aborted;
|
||||||
private Exception _completeException;
|
private Exception _completeException;
|
||||||
|
|
||||||
public ConcurrentPipeWriter(PipeWriter innerPipeWriter, MemoryPool<byte> pool)
|
public ConcurrentPipeWriter(PipeWriter innerPipeWriter, MemoryPool<byte> pool, object sync)
|
||||||
{
|
{
|
||||||
_innerPipeWriter = innerPipeWriter;
|
_innerPipeWriter = innerPipeWriter;
|
||||||
_pool = pool;
|
_pool = pool;
|
||||||
|
_sync = sync;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Memory<byte> GetMemory(int sizeHint = 0)
|
public override Memory<byte> GetMemory(int sizeHint = 0)
|
||||||
{
|
|
||||||
lock (_sync)
|
|
||||||
{
|
{
|
||||||
if (_currentFlushTcs == null && _head == null)
|
if (_currentFlushTcs == null && _head == null)
|
||||||
{
|
{
|
||||||
|
|
@ -69,11 +68,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
|
||||||
AllocateMemoryUnsynchronized(sizeHint);
|
AllocateMemoryUnsynchronized(sizeHint);
|
||||||
return _tailMemory;
|
return _tailMemory;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public override Span<byte> GetSpan(int sizeHint = 0)
|
public override Span<byte> GetSpan(int sizeHint = 0)
|
||||||
{
|
|
||||||
lock (_sync)
|
|
||||||
{
|
{
|
||||||
if (_currentFlushTcs == null && _head == null)
|
if (_currentFlushTcs == null && _head == null)
|
||||||
{
|
{
|
||||||
|
|
@ -83,11 +79,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
|
||||||
AllocateMemoryUnsynchronized(sizeHint);
|
AllocateMemoryUnsynchronized(sizeHint);
|
||||||
return _tailMemory.Span;
|
return _tailMemory.Span;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public override void Advance(int bytes)
|
public override void Advance(int bytes)
|
||||||
{
|
|
||||||
lock (_sync)
|
|
||||||
{
|
{
|
||||||
if (_currentFlushTcs == null && _head == null)
|
if (_currentFlushTcs == null && _head == null)
|
||||||
{
|
{
|
||||||
|
|
@ -105,11 +98,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
|
||||||
_tailMemory = _tailMemory.Slice(bytes);
|
_tailMemory = _tailMemory.Slice(bytes);
|
||||||
_bufferedWritePending = false;
|
_bufferedWritePending = false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public override ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default)
|
public override ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
|
||||||
lock (_sync)
|
|
||||||
{
|
{
|
||||||
if (_currentFlushTcs != null)
|
if (_currentFlushTcs != null)
|
||||||
{
|
{
|
||||||
|
|
@ -142,7 +132,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
|
||||||
_ = FlushAsyncAwaited(flushTask, cancellationToken);
|
_ = FlushAsyncAwaited(flushTask, cancellationToken);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private async Task FlushAsyncAwaited(ValueTask<FlushResult> flushTask, CancellationToken cancellationToken)
|
private async Task FlushAsyncAwaited(ValueTask<FlushResult> flushTask, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
|
@ -198,8 +187,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Complete(Exception exception = null)
|
public override void Complete(Exception exception = null)
|
||||||
{
|
|
||||||
lock (_sync)
|
|
||||||
{
|
{
|
||||||
// We store the complete exception or s sentinel exception instance in a field if a flush was ongoing.
|
// We store the complete exception or s sentinel exception instance in a field if a flush was ongoing.
|
||||||
// We call the inner Complete() method after the flush loop ended.
|
// We call the inner Complete() method after the flush loop ended.
|
||||||
|
|
@ -220,11 +207,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
|
||||||
_innerPipeWriter.Complete(exception);
|
_innerPipeWriter.Complete(exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void Abort()
|
public void Abort()
|
||||||
{
|
|
||||||
lock (_sync)
|
|
||||||
{
|
{
|
||||||
_aborted = true;
|
_aborted = true;
|
||||||
|
|
||||||
|
|
@ -234,7 +218,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
|
||||||
CleanupSegmentsUnsynchronized();
|
CleanupSegmentsUnsynchronized();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void CleanupSegmentsUnsynchronized()
|
private void CleanupSegmentsUnsynchronized()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
|
||||||
};
|
};
|
||||||
|
|
||||||
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
||||||
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool);
|
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, new object());
|
||||||
|
|
||||||
var memory = concurrentPipeWriter.GetMemory();
|
var memory = concurrentPipeWriter.GetMemory();
|
||||||
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
||||||
|
|
@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
|
||||||
};
|
};
|
||||||
|
|
||||||
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
||||||
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool);
|
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, new object());
|
||||||
|
|
||||||
var memory = concurrentPipeWriter.GetMemory();
|
var memory = concurrentPipeWriter.GetMemory();
|
||||||
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
||||||
|
|
@ -152,7 +152,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
|
||||||
};
|
};
|
||||||
|
|
||||||
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
||||||
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool);
|
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, new object());
|
||||||
|
|
||||||
var memory = concurrentPipeWriter.GetMemory();
|
var memory = concurrentPipeWriter.GetMemory();
|
||||||
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
||||||
|
|
@ -218,7 +218,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
|
||||||
};
|
};
|
||||||
|
|
||||||
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
||||||
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool);
|
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, new object());
|
||||||
|
|
||||||
var memory = concurrentPipeWriter.GetMemory();
|
var memory = concurrentPipeWriter.GetMemory();
|
||||||
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
||||||
|
|
@ -273,7 +273,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
|
||||||
};
|
};
|
||||||
|
|
||||||
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray);
|
||||||
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool);
|
var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, new object());
|
||||||
|
|
||||||
var memory = concurrentPipeWriter.GetMemory();
|
var memory = concurrentPipeWriter.GetMemory();
|
||||||
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
Assert.Equal(1, mockPipeWriter.GetMemoryCallCount);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue