Get number of connections in ASP .NET Core 3.1 / Kestrel

1.1k views Asked by At

I'm putting a limit on the number of concurrent connections to a webapp hosted by Kestrel using an approach like:

webBuilder.UseKestrel((context, options) =>
          {
            options.Configure(context.Configuration.GetSection("Kestrel"));
            options.AddServerHeader = false;

            // get limits
            var maxConnections = context.Configuration.GetValue<int>("Kestrel:Limits:MaxConcurrentConnections");
            var maxUpgradedConnections = context.Configuration.GetValue<int>("Kestrel:Limits:MaxConcurrentUpgradedConnections");
            options.Limits.MaxConcurrentConnections = maxConnections;
            options.Limits.MaxConcurrentUpgradedConnections = maxUpgradedConnections;
            ...
            }

When receiving new requests I would like to check the servers current number of connections and perform a graceful deny by showing an error message about the connection limit have been reached. One approach I've tried has been adding middleware for checking, something like this:

public void Configure(IApplicationBuilder app)
{
    app.Use(async (context, next) =>
    {
        // Insert limit check here

        // Call the next delegate/middleware in the pipeline
        await next();
    });
}

The problem is that I have not found a way of getting the number of connections in Kestrel. Anyone knows how to get it? Or if this even is a viable approach?

0

There are 0 answers