Skip to content

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

Open
demolaf wants to merge 34 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 34 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?

@@ -0,0 +1,34 @@
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file

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.

Let's make this example more useful for the user. Maybe copy the example from package:cupertino_http or something.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

okay, i'll look into this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I added an example showing 100 concurrent requests but it's not really a great example since http/1.1 can handle that faster (if uncapped). The only notable difference would be memory usage since http/1.1 would open TCP+TLS connections per request.

/// `true` accepts a certificate that failed normal verification (expired,
/// self-signed, wrong host, ...). It exists for tests and trusted private
/// networks - do not use it to accept arbitrary certificates in production.
class Http2Client extends BaseClient {

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

that makes sense to do since we still have more pending feature work to do per the conformance tests


List<int>? bodyBytes;

Future<StreamedResponse> attempt() async {

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.

This is awkward to read. Do we need to define this function, which is only used once?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

yeah i agree, it's actually used twice, the initial attempt and then in the .catchError block.

Comment thread pkgs/http2/lib/src/client_pool.dart Outdated
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

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.

Maybe provide an example on how to use this library. But I like the approach.

Comment thread pkgs/http2/lib/src/client_pool.dart Outdated

class _PooledResource<T> {
_PooledResource(this.future);
final Future<T> future;

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 think that these need to be better named/documented:
future refers to the creation of the resource, right?
inFlight refers to the number of concurrent requests of the resource, right?
failed indicates that creating the resource failed, right?

Maybe:
create
inFlightCount
createFailed

And a comment?

This would be more clear if T were ClientConnection but maybe the tests would then be too hard to write.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done, renamed all three. also dropped the generic — ClientPool is ClientConnection-specific now, and the pool tests use a generated MockClientConnection.

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
/// its most recent SETTINGS_MAX_CONCURRENT_STREAMS (RFC 7540 6.5.2), or
/// `null` if it hasn't advertised a limit.
///
/// Deliberately not on [ClientTransportConnection]: that class can only be

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.

@mosuem maybe we should add it there anyway - there are no "implements ClientTransportConnection" on GitHub but we could also bump semver.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, just add it there and bump semver.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done — moved it to ClientTransportConnection and bumped to 3.1.0-wip

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