Skip to content

Commit e33f2d6

Browse files
committed
More WIP
- Added shared options between server and client - Null memory pool
1 parent c8a49d0 commit e33f2d6

File tree

4 files changed

+60
-22
lines changed

4 files changed

+60
-22
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
7+
{
8+
public class SharedSocketOptions
9+
{
10+
/// <summary>
11+
/// Set to false to enable Nagle's algorithm for all connections.
12+
/// </summary>
13+
/// <remarks>
14+
/// Defaults to true.
15+
/// </remarks>
16+
public bool NoDelay { get; set; } = true;
17+
18+
/// <summary>
19+
/// The maximum size before the transport will stop proactively reading from the transport.
20+
/// </summary>
21+
public long? MaxReadBufferSize { get; set; } = 1024 * 1024;
22+
23+
/// <summary>
24+
/// The maximum size the transport will buffer outside of OS buffers before pausing writes.
25+
/// </summary>
26+
public long? MaxWriteBufferSize { get; set; } = 64 * 1024;
27+
28+
internal Func<MemoryPool<byte>> MemoryPoolFactory { get; set; } = System.Buffers.SlabMemoryPoolFactory.Create;
29+
}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Collections.Generic;
4+
using System.IO.Pipelines;
5+
using System.Text;
6+
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal;
7+
8+
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
9+
{
10+
public class SocketClientOptions : SharedSocketOptions
11+
{
12+
/// <summary>
13+
/// Determines where read and write callbacks run.
14+
/// </summary>
15+
public PipeScheduler Scheduler { get; set; } = new IOQueue();
16+
}
17+
}

src/Servers/Kestrel/Transport.Sockets/src/SocketConnectionFactory.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO.Pipelines;
41
using System.Net;
52
using System.Net.Sockets;
6-
using System.Text;
73
using System.Threading;
84
using System.Threading.Tasks;
95
using Microsoft.AspNetCore.Connections;
106
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal;
117
using Microsoft.Extensions.Logging;
8+
using Microsoft.Extensions.Options;
129

1310
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
1411
{
@@ -18,13 +15,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
1815
public class SocketConnectionFactory : IConnectionFactory
1916
{
2017
private readonly ILogger _logger;
18+
private readonly SocketClientOptions _options;
2119

2220
/// <summary>
2321
/// Creates the <see cref="SocketConnectionFactory"/>.
2422
/// </summary>
23+
/// <param name="options">The options for this transport</param>
2524
/// <param name="loggerFactory">The logger factory</param>
26-
public SocketConnectionFactory(ILoggerFactory loggerFactory)
25+
public SocketConnectionFactory(IOptions<SocketClientOptions> options, ILoggerFactory loggerFactory)
2726
{
27+
_options = options.Value;
2828
_logger = loggerFactory.CreateLogger<SocketConnectionFactory>();
2929
}
3030

@@ -42,9 +42,15 @@ public async ValueTask<ConnectionContext> ConnectAsync(EndPoint endPoint, Cancel
4242

4343
var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, protocolType);
4444

45+
// Only apply no delay to Tcp based endpoints
46+
if (protocolType == ProtocolType.Tcp)
47+
{
48+
socket.NoDelay = _options.NoDelay;
49+
}
50+
4551
await socket.ConnectAsync(endPoint);
4652

47-
var connection = new SocketConnection(socket, memoryPool: null, PipeScheduler.ThreadPool, new SocketsTrace(_logger));
53+
var connection = new SocketConnection(socket, memoryPool: null, _options.Scheduler, new SocketsTrace(_logger), _options.MaxReadBufferSize, _options.MaxWriteBufferSize);
4854
connection.Start();
4955

5056
return connection;

src/Servers/Kestrel/Transport.Sockets/src/SocketTransportOptions.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Buffers;
65

76
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
87
{
9-
public class SocketTransportOptions
8+
public class SocketTransportOptions : SharedSocketOptions
109
{
1110
/// <summary>
1211
/// The number of I/O queues used to process requests. Set to 0 to directly schedule I/O to the ThreadPool.
@@ -15,19 +14,5 @@ public class SocketTransportOptions
1514
/// Defaults to <see cref="Environment.ProcessorCount" /> rounded down and clamped between 1 and 16.
1615
/// </remarks>
1716
public int IOQueueCount { get; set; } = Math.Min(Environment.ProcessorCount, 16);
18-
19-
/// <summary>
20-
/// Set to false to enable Nagle's algorithm for all connections.
21-
/// </summary>
22-
/// <remarks>
23-
/// Defaults to true.
24-
/// </remarks>
25-
public bool NoDelay { get; set; } = true;
26-
27-
public long? MaxReadBufferSize { get; set; } = 1024 * 1024;
28-
29-
public long? MaxWriteBufferSize { get; set; } = 64 * 1024;
30-
31-
internal Func<MemoryPool<byte>> MemoryPoolFactory { get; set; } = System.Buffers.SlabMemoryPoolFactory.Create;
3217
}
3318
}

0 commit comments

Comments
 (0)