Clean up some of protocol abstractions (#2311)

* Clean up some of protocol abstractions
- Renamed PipeConnection to DuplexPipe
- Removed MemoryPool from ConnectionContext
- Work around value tuple issue on net471
This commit is contained in:
David Fowler 2018-02-10 10:39:21 -08:00 committed by GitHub
parent 7098c8d07d
commit 2156030460
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 77 additions and 78 deletions

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
public void Setup()
{
var memoryPool = new MemoryPool();
var pair = PipeFactory.CreateConnectionPair(memoryPool);
var pair = DuplexPipe.CreateConnectionPair(memoryPool);
var serviceContext = new ServiceContext
{

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
private static readonly Func<object, Task> _psuedoAsyncTaskFunc = (obj) => _psuedoAsyncTask;
private readonly TestHttp1Connection _http1Connection;
private (IDuplexPipe Transport, IDuplexPipe Application) _pair;
private DuplexPipe.DuplexPipePair _pair;
private readonly byte[] _writeData;
@ -95,7 +95,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
{
using (var memoryPool = new MemoryPool())
{
var pair = PipeFactory.CreateConnectionPair(memoryPool);
var pair = DuplexPipe.CreateConnectionPair(memoryPool);
_pair = pair;
var serviceContext = new ServiceContext

View File

@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
public HttpProtocolFeatureCollection()
{
var memoryPool = new MemoryPool();
var pair = PipeFactory.CreateConnectionPair(memoryPool);
var pair = DuplexPipe.CreateConnectionPair(memoryPool);
var serviceContext = new ServiceContext
{

View File

@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
public void Setup()
{
var memoryPool = new MemoryPool();
var pair = PipeFactory.CreateConnectionPair(memoryPool);
var pair = DuplexPipe.CreateConnectionPair(memoryPool);
var serviceContext = new ServiceContext
{

View File

@ -171,7 +171,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
public void Setup()
{
var memoryPool = new MemoryPool();
var pair = PipeFactory.CreateConnectionPair(memoryPool);
var pair = DuplexPipe.CreateConnectionPair(memoryPool);
var serviceContext = new ServiceContext
{

View File

@ -111,7 +111,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
public void Setup()
{
var memoryPool = new MemoryPool();
var pair = PipeFactory.CreateConnectionPair(memoryPool);
var pair = DuplexPipe.CreateConnectionPair(memoryPool);
var serviceContext = new ServiceContext
{

View File

@ -36,10 +36,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
// REVIEW: Unfortunately, we still need to use the service context to create the pipes since the settings
// for the scheduler and limits are specified here
var inputOptions = GetInputPipeOptions(_serviceContext, connectionContext.MemoryPool, transportFeature.InputWriterScheduler);
var outputOptions = GetOutputPipeOptions(_serviceContext, connectionContext.MemoryPool, transportFeature.OutputReaderScheduler);
var inputOptions = GetInputPipeOptions(_serviceContext, transportFeature.MemoryPool, transportFeature.InputWriterScheduler);
var outputOptions = GetOutputPipeOptions(_serviceContext, transportFeature.MemoryPool, transportFeature.OutputReaderScheduler);
var pair = PipeFactory.CreateConnectionPair(inputOptions, outputOptions);
var pair = DuplexPipe.CreateConnectionPair(inputOptions, outputOptions);
// Set the transport and connection id
connectionContext.ConnectionId = CorrelationIdGenerator.GetNextId();

View File

@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
Protocols = _protocols,
ServiceContext = _serviceContext,
ConnectionFeatures = connectionContext.Features,
MemoryPool = connectionContext.MemoryPool,
MemoryPool = transportFeature.MemoryPool,
ConnectionAdapters = _connectionAdapters,
Transport = connectionContext.Transport,
Application = transportFeature.Application

View File

@ -1,8 +1,4 @@
using System;
using System.Buffers;
using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
using System.IO.Pipelines;
using Microsoft.AspNetCore.Http.Features;
namespace Microsoft.AspNetCore.Protocols
@ -14,7 +10,5 @@ namespace Microsoft.AspNetCore.Protocols
public abstract IFeatureCollection Features { get; }
public abstract IDuplexPipe Transport { get; set; }
public abstract MemoryPool MemoryPool { get; }
}
}

View File

@ -28,8 +28,6 @@ namespace Microsoft.AspNetCore.Protocols
public override IFeatureCollection Features => _features.Collection;
public override MemoryPool MemoryPool => ConnectionTransportFeature.MemoryPool;
public override IDuplexPipe Transport
{
get => ConnectionTransportFeature.Transport;

View File

@ -0,0 +1,53 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Text;
namespace System.IO.Pipelines
{
public class DuplexPipe : IDuplexPipe
{
public DuplexPipe(PipeReader reader, PipeWriter writer)
{
Input = reader;
Output = writer;
}
public PipeReader Input { get; }
public PipeWriter Output { get; }
public void Dispose()
{
}
public static DuplexPipePair CreateConnectionPair(MemoryPool memoryPool)
{
return CreateConnectionPair(new PipeOptions(memoryPool), new PipeOptions(memoryPool));
}
public static DuplexPipePair CreateConnectionPair(PipeOptions inputOptions, PipeOptions outputOptions)
{
var input = new Pipe(inputOptions);
var output = new Pipe(outputOptions);
var transportToApplication = new DuplexPipe(output.Reader, input.Writer);
var applicationToTransport = new DuplexPipe(input.Reader, output.Writer);
return new DuplexPipePair(applicationToTransport, transportToApplication);
}
// This class exists to work around issues with value tuple on .NET Framework
public readonly struct DuplexPipePair
{
public IDuplexPipe Transport { get; }
public IDuplexPipe Application { get; }
public DuplexPipePair(IDuplexPipe transport, IDuplexPipe application)
{
Transport = transport;
Application = application;
}
}
}
}

View File

@ -1,23 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace System.IO.Pipelines
{
public class PipeConnection : IDuplexPipe
{
public PipeConnection(PipeReader reader, PipeWriter writer)
{
Input = reader;
Output = writer;
}
public PipeReader Input { get; }
public PipeWriter Output { get; }
public void Dispose()
{
}
}
}

View File

@ -1,23 +0,0 @@
using System.Buffers;
namespace System.IO.Pipelines
{
public static class PipeFactory
{
public static (IDuplexPipe Transport, IDuplexPipe Application) CreateConnectionPair(MemoryPool memoryPool)
{
return CreateConnectionPair(new PipeOptions(memoryPool), new PipeOptions(memoryPool));
}
public static (IDuplexPipe Transport, IDuplexPipe Application) CreateConnectionPair(PipeOptions inputOptions, PipeOptions outputOptions)
{
var input = new Pipe(inputOptions);
var output = new Pipe(outputOptions);
var transportToApplication = new PipeConnection(output.Reader, input.Writer);
var applicationToTransport = new PipeConnection(input.Reader, output.Writer);
return (applicationToTransport, transportToApplication);
}
}
}

View File

@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
public Http1ConnectionTests()
{
_pipelineFactory = new MemoryPool();
var pair = PipeFactory.CreateConnectionPair(_pipelineFactory);
var pair = DuplexPipe.CreateConnectionPair(_pipelineFactory);
_transport = pair.Transport;
_application = pair.Application;

View File

@ -94,7 +94,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
private static readonly byte[] _maxData = Encoding.ASCII.GetBytes(new string('a', Http2Frame.MinAllowedMaxFrameSize));
private readonly MemoryPool _memoryPool = new MemoryPool();
private readonly (IDuplexPipe Transport, IDuplexPipe Application) _pair;
private readonly DuplexPipe.DuplexPipePair _pair;
private readonly TestApplicationErrorLogger _logger;
private readonly Http2ConnectionContext _connectionContext;
private readonly Http2Connection _connection;
@ -122,7 +122,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
public Http2ConnectionTests()
{
_pair = PipeFactory.CreateConnectionPair(_memoryPool);
_pair = DuplexPipe.CreateConnectionPair(_memoryPool);
_noopApplication = context => Task.CompletedTask;

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
public HttpConnectionTests()
{
_memoryPool = new MemoryPool();
var pair = PipeFactory.CreateConnectionPair(_memoryPool);
var pair = DuplexPipe.CreateConnectionPair(_memoryPool);
_httpConnectionContext = new HttpConnectionContext
{

View File

@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
{
using (var memoryPool = new MemoryPool())
{
var pair = PipeFactory.CreateConnectionPair(memoryPool);
var pair = DuplexPipe.CreateConnectionPair(memoryPool);
var http1ConnectionContext = new Http1ConnectionContext
{
ServiceContext = new TestServiceContext(),

View File

@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
public TestInput()
{
_memoryPool = new MemoryPool();
var pair = PipeFactory.CreateConnectionPair(_memoryPool);
var pair = DuplexPipe.CreateConnectionPair(_memoryPool);
Transport = pair.Transport;
Application = pair.Application;

View File

@ -695,7 +695,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
private Http1OutputProducer CreateOutputProducer(PipeOptions pipeOptions, CancellationTokenSource cts = null)
{
var pair = PipeFactory.CreateConnectionPair(pipeOptions, pipeOptions);
var pair = DuplexPipe.CreateConnectionPair(pipeOptions, pipeOptions);
var logger = new TestApplicationErrorLogger();
var serviceContext = new TestServiceContext

View File

@ -20,13 +20,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests.TestHelpers
{
var connectionContext = new DefaultConnectionContext(features);
Input = new Pipe(InputOptions(connectionContext.MemoryPool));
Output = new Pipe(OutputOptions(connectionContext.MemoryPool));
var feature = connectionContext.Features.Get<IConnectionTransportFeature>();
connectionContext.Transport = new PipeConnection(Input.Reader, Output.Writer);
feature.Application = new PipeConnection(Output.Reader, Input.Writer);
Input = new Pipe(InputOptions(feature.MemoryPool));
Output = new Pipe(OutputOptions(feature.MemoryPool));
connectionContext.Transport = new DuplexPipe(Input.Reader, Output.Writer);
feature.Application = new DuplexPipe(Output.Reader, Input.Writer);
}
public Pipe Input { get; private set; }