Skip to content

feat(http2): add a pooled, multiplexed HTTP/2 http.Client - #1956

Open
demolaf wants to merge 37 commits into
dart-lang:masterfrom
demolaf:pooled-http2-client
Open

feat(http2): add a pooled, multiplexed HTTP/2 http.Client#1956
demolaf wants to merge 37 commits into
dart-lang:masterfrom
demolaf:pooled-http2-client

Conversation

@demolaf

@demolaf demolaf commented Jul 30, 2026

Copy link
Copy Markdown

Closes #1385

Adds Http2Client, a pooled, multiplexed http.Client backed by HTTP/2 connections, plus the ClientPool it's built on — fixes dart:io's HttpClient opening one connection per concurrent request. Ported and generalized from a downstream implementation built for firebase/firebase-admin-dart#305.

  • ClientPool: most-full-first packing, idle GC, failure retirement, and a lease API so a slot can outlive the call that claimed it
  • Http2Client: pools per host:port, caps concurrent handshakes globally, multi-host safe (e.g. as googleapis_auth's baseClient)
  • Never opens more concurrent streams on a connection than the server's SETTINGS_MAX_CONCURRENT_STREAMS allows, and doesn't use a connection at all until that frame has arrived — see Http2Client.settingsTimeout
  • A request holds its pool slot until the response body ends or is cancelled, so the pool's view matches the number of streams actually open
  • Retries once when a pooled connection was closed by the peer before reuse
  • Adds ClientTransportConnection.peerMaxConcurrentStreams. This is a new member on an implementable class, hence the minor version bump

@demolaf
demolaf marked this pull request as draft July 30, 2026 09:43
@demolaf
demolaf marked this pull request as ready for review July 30, 2026 13:14
Comment thread pkgs/http2/lib/src/http2_client.dart
Comment thread pkgs/http2/lib/src/http2_client.dart Outdated
Comment thread pkgs/http2/lib/src/client_pool.dart Outdated
Comment thread pkgs/http2/lib/src/client_pool.dart Outdated
Comment thread pkgs/http2/lib/src/client_pool.dart Outdated
Comment thread pkgs/http2/lib/src/client_pool.dart Outdated
Comment thread pkgs/http2/lib/src/http2_client.dart
Comment thread pkgs/http2/lib/src/http2_client.dart Outdated
Comment thread pkgs/http2/lib/src/http2_client.dart Outdated
Comment thread pkgs/http2/test/http2_client_test.dart Outdated

@brianquinlan brianquinlan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'll have more review feedback tomorrow. Also, this PR adds (preliminary) conformance tests: #1960

You might want to add them to this PR.

Comment thread pkgs/http2/lib/src/http2_client.dart
@demolaf
demolaf force-pushed the pooled-http2-client branch from e8b8590 to 39b75f9 Compare August 6, 2026 14:08
@demolaf

demolaf commented Aug 6, 2026

Copy link
Copy Markdown
Author

@brianquinlan @mosuem I'm deferring the TODOs in the conformance tests to a later PR since this is already quite a lot, sounds good?

Comment thread pkgs/http2/example/pooled_client.dart
Comment thread pkgs/http2/lib/src/http2_client.dart
Comment thread pkgs/http2/lib/src/http2_client.dart
Comment thread pkgs/http2/lib/src/client_pool.dart Outdated
Comment thread pkgs/http2/lib/src/client_pool.dart
Comment thread pkgs/http2/lib/src/client_pool.dart Outdated
Comment thread pkgs/http2/lib/src/client_pool.dart Outdated
Comment thread pkgs/http2/lib/src/http2_client.dart Outdated
Comment thread pkgs/http2/lib/src/connection.dart Outdated
Comment thread pkgs/http2/lib/src/http2_client.dart
@demolaf
demolaf force-pushed the pooled-http2-client branch from 90b0813 to af53e7f Compare August 14, 2026 17:25
@demolaf
demolaf force-pushed the pooled-http2-client branch from a5cea15 to a05fb28 Compare August 14, 2026 18:30
@demolaf
demolaf requested review from brianquinlan and mosuem August 18, 2026 10:17
Comment thread pkgs/http2/example/pooled_client.dart
Comment thread pkgs/http2/lib/src/client_pool.dart Outdated
Comment thread pkgs/http2/lib/src/http2_client.dart Outdated
Comment thread pkgs/http2/lib/src/http2_client.dart
Comment thread pkgs/http2/lib/src/http2_client.dart Outdated
Comment thread pkgs/http2/README.md
is safe to reuse across requests to different hosts. Note that it speaks only
HTTP/2 and does not fall back to HTTP/1.1: a server that does not negotiate
`h2` is treated as an error. See the example
[here](example/pooled_client.dart).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you rename the example "main.dart" and update this link? Otherwise I think that we are good to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a http.Client compatible client

3 participants