microsoft / kiota-http-dotnet

Kiota http provider implementation for dotnet with HttpClient
https://aka.ms/kiota/docs
MIT License
35 stars 15 forks source link

Use SocketsHttpHandler and EnableMultipleHttp2Connections as default handler #251

Closed ghelyar closed 6 months ago

ghelyar commented 6 months ago

SocketsHttpHandler has been the default handler of HttpClientHandler since .NET 5. This change uses SocketsHttpHandler directly from Kiota, without going through HttpClientHandler.

This change also enables EnableMultipleHttp2Connections, which improves performance when there are over 100 concurrent requests to the same server.

This can be performance tested by starting a HTTP/2 server that holds connections open for some time, and a client which makes over 100 parallel requests. e.g.

server:

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(k =>
    k.ConfigureEndpointDefaults(e =>
        e.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1AndHttp2));
var app = builder.Build();
app.MapGet("/", () => Task.Delay(100));
app.Run();

client:

var client = new ApiSdk.ApiClient(new HttpClientRequestAdapter(new AnonymousAuthenticationProvider())
{ BaseUrl = "https://localhost:5001" });

await client.GetAsync(); // warm up

var stopwatch = Stopwatch.StartNew();
await Parallel.ForEachAsync(
    Enumerable.Range(0, 10_000),
    new ParallelOptions { MaxDegreeOfParallelism = 200 },
    async (_, cancel) => await client.GetAsync(cancellationToken: cancel));
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed);

This causes performance to continue to scale when there are over 100 connections rather than stop scaling at 100 connections. There doesn't seem to be any performance impact, positive or negative, below 100 connections.

fixes https://github.com/microsoft/kiota-http-dotnet/issues/239

ghelyar commented 6 months ago

I have updated the version number to 1.4.1 and updated the changelog.