Skip to content

Add IMemoryPoolFactory to Connections.Abstractions #61003

Closed
@BrennanConroy

Description

@BrennanConroy

Background and Motivation

Kestrel (and other server impls) use the PinnedBlockMemoryPool for the core IO processing. The usage is purely internal today and also has the issue of never releasing its memory resulting in users thinking there is a memory leak. See: #55490

As part of wanting to reduce the memory when the pool is idle we want to use a timer, but creating a timer per pool is not ideal, so we want to put the cleanup on the kestrel heartbeat thread which requires sharing between Sockets, NamedPipes, and Kestrel Core. Ideally we want to avoid internals visible to, so in order to solve this we need a type that can be used by all dlls and shared via DI so we can wire up the pool usage everywhere.

Proposed API

Microsoft.AspNetCore.Connections.Abstractions.dll

namespace Microsoft.AspNetCore.Connections;

+ public interface IMemoryPoolFactory
+ {
+    MemoryPool<byte> CreatePool();
+ }

Usage Examples

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<IMemoryPoolFactory, MyMemoryPoolFactory>();

var app = builder.Build();

public sealed class MyMemoryPoolFactory : IMemoryPoolFactory
{
    public MemoryPool<byte> CreatePool()
    {
        return MemoryPool<byte>.Shared;
    }
}

Alternative Designs

Alternatively we can use [InternalsVisibleTo] on some of our assemblies.

Risks

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions