Skip to content

Commit b24508e

Browse files
Add IMemoryPoolFactory and cleanup memory pool while idle (#61554)
1 parent 1415d9b commit b24508e

File tree

82 files changed

+1138
-121
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1138
-121
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Buffers;
5+
6+
namespace Microsoft.AspNetCore.Connections;
7+
8+
/// <summary>
9+
/// Interface for creating memory pools.
10+
/// </summary>
11+
public interface IMemoryPoolFactory<T>
12+
{
13+
/// <summary>
14+
/// Creates a new instance of a memory pool.
15+
/// </summary>
16+
/// <returns>A new memory pool instance.</returns>
17+
MemoryPool<T> Create();
18+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>
3+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create() -> System.Buffers.MemoryPool<T>!
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>
3+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create() -> System.Buffers.MemoryPool<T>!
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>
3+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create() -> System.Buffers.MemoryPool<T>!
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>
3+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create() -> System.Buffers.MemoryPool<T>!

src/Servers/HttpSys/src/HttpSysListener.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Buffers;
55
using System.Diagnostics;
6+
using Microsoft.AspNetCore.Connections;
67
using Microsoft.AspNetCore.Http;
78
using Microsoft.AspNetCore.HttpSys.Internal;
89
using Microsoft.AspNetCore.WebUtilities;
@@ -33,7 +34,7 @@ internal sealed partial class HttpSysListener : IDisposable
3334
// 0.5 seconds per request. Respond with a 400 Bad Request.
3435
private const int UnknownHeaderLimit = 1000;
3536

36-
internal MemoryPool<byte> MemoryPool { get; } = PinnedBlockMemoryPoolFactory.Create();
37+
internal MemoryPool<byte> MemoryPool { get; }
3738

3839
private volatile State _state; // m_State is set only within lock blocks, but often read outside locks.
3940

@@ -44,7 +45,7 @@ internal sealed partial class HttpSysListener : IDisposable
4445

4546
private readonly object _internalLock;
4647

47-
public HttpSysListener(HttpSysOptions options, ILoggerFactory loggerFactory)
48+
public HttpSysListener(HttpSysOptions options, IMemoryPoolFactory<byte> memoryPoolFactory, ILoggerFactory loggerFactory)
4849
{
4950
ArgumentNullException.ThrowIfNull(options);
5051
ArgumentNullException.ThrowIfNull(loggerFactory);
@@ -54,6 +55,8 @@ public HttpSysListener(HttpSysOptions options, ILoggerFactory loggerFactory)
5455
throw new PlatformNotSupportedException();
5556
}
5657

58+
MemoryPool = memoryPoolFactory.Create();
59+
5760
Options = options;
5861

5962
Logger = loggerFactory.CreateLogger<HttpSysListener>();

src/Servers/HttpSys/src/MessagePump.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.Linq;
66
using Microsoft.AspNetCore.Authentication;
7+
using Microsoft.AspNetCore.Connections;
78
using Microsoft.AspNetCore.Hosting.Server;
89
using Microsoft.AspNetCore.Hosting.Server.Features;
910
using Microsoft.AspNetCore.Http.Features;
@@ -27,12 +28,13 @@ internal sealed partial class MessagePump : IServer, IServerDelegationFeature
2728

2829
private readonly ServerAddressesFeature _serverAddresses;
2930

30-
public MessagePump(IOptions<HttpSysOptions> options, ILoggerFactory loggerFactory, IAuthenticationSchemeProvider authentication)
31+
public MessagePump(IOptions<HttpSysOptions> options, IMemoryPoolFactory<byte> memoryPoolFactory,
32+
ILoggerFactory loggerFactory, IAuthenticationSchemeProvider authentication)
3133
{
3234
ArgumentNullException.ThrowIfNull(options);
3335
ArgumentNullException.ThrowIfNull(loggerFactory);
3436
_options = options.Value;
35-
Listener = new HttpSysListener(_options, loggerFactory);
37+
Listener = new HttpSysListener(_options, memoryPoolFactory, loggerFactory);
3638
_logger = loggerFactory.CreateLogger<MessagePump>();
3739

3840
if (_options.Authentication.Schemes != AuthenticationSchemes.None)

src/Servers/HttpSys/src/WebHostBuilderHttpSysExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.Versioning;
5+
using Microsoft.AspNetCore.Connections;
56
using Microsoft.AspNetCore.Hosting.Server;
67
using Microsoft.AspNetCore.Server.HttpSys;
78
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.DependencyInjection.Extensions;
810
using Microsoft.Extensions.Options;
911

1012
namespace Microsoft.AspNetCore.Hosting;
@@ -45,6 +47,8 @@ public static IWebHostBuilder UseHttpSys(this IWebHostBuilder hostBuilder)
4547
};
4648
});
4749
services.AddAuthenticationCore();
50+
51+
services.TryAddSingleton<IMemoryPoolFactory<byte>, DefaultMemoryPoolFactory>();
4852
});
4953
}
5054

src/Servers/HttpSys/test/FunctionalTests/Listener/ServerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Buffers;
56
using System.IO;
67
using System.Net;
78
using System.Net.Http;
@@ -132,7 +133,7 @@ public void Server_RegisterUnavailablePrefix_ThrowsActionableHttpSysException()
132133

133134
var options = new HttpSysOptions();
134135
options.UrlPrefixes.Add(address1);
135-
using var listener = new HttpSysListener(options, new LoggerFactory());
136+
using var listener = new HttpSysListener(options, new DefaultMemoryPoolFactory(), new LoggerFactory());
136137

137138
var exception = Assert.Throws<HttpSysException>(() => listener.Start());
138139

src/Servers/HttpSys/test/FunctionalTests/Listener/Utilities.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Buffers;
56
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.HttpSys.Internal;
78
using Microsoft.Extensions.Logging;
@@ -47,7 +48,7 @@ internal static HttpSysListener CreateDynamicHttpServer(string basePath, out str
4748
var options = new HttpSysOptions();
4849
options.UrlPrefixes.Add(prefix);
4950
options.RequestQueueName = prefix.Port; // Convention for use with CreateServerOnExistingQueue
50-
var listener = new HttpSysListener(options, new LoggerFactory());
51+
var listener = new HttpSysListener(options, new DefaultMemoryPoolFactory(), new LoggerFactory());
5152
try
5253
{
5354
listener.Start();
@@ -76,7 +77,7 @@ internal static HttpSysListener CreateHttpsServer()
7677

7778
internal static HttpSysListener CreateServer(string scheme, string host, int port, string path)
7879
{
79-
var listener = new HttpSysListener(new HttpSysOptions(), new LoggerFactory());
80+
var listener = new HttpSysListener(new HttpSysOptions(), new DefaultMemoryPoolFactory(), new LoggerFactory());
8081
listener.Options.UrlPrefixes.Add(UrlPrefix.Create(scheme, host, port, path));
8182
listener.Start();
8283
return listener;
@@ -86,7 +87,7 @@ internal static HttpSysListener CreateServer(Action<HttpSysOptions> configureOpt
8687
{
8788
var options = new HttpSysOptions();
8889
configureOptions(options);
89-
var listener = new HttpSysListener(options, new LoggerFactory());
90+
var listener = new HttpSysListener(options, new DefaultMemoryPoolFactory(), new LoggerFactory());
9091
listener.Start();
9192
return listener;
9293
}

0 commit comments

Comments
 (0)