Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Use SocketsHttpHandler and EnableMultipleHttp2Connections as default handler #251

Merged
merged 5 commits into from
May 7, 2024

Conversation

ghelyar
Copy link
Contributor

@ghelyar ghelyar commented May 7, 2024

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 #239

@ghelyar ghelyar requested a review from a team as a code owner May 7, 2024 09:27
Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contributions!
Can you please:

  • update the version in the csproj (patch)
  • add an entry to the changelog (today's date, changed)

This will help ensure a swift release!
Thanks!

@ghelyar
Copy link
Contributor Author

ghelyar commented May 7, 2024

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

Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the changes!

@baywet baywet enabled auto-merge May 7, 2024 16:11
@baywet baywet merged commit a1aa04c into microsoft:main May 7, 2024
7 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

SocketsHttpHandler as final handler
2 participants