Skip to content

net/http: port ClientConn and Transport.NewClientConn - #721

Merged
tamnd merged 1 commit into
mainfrom
clientconn
Oct 27, 2026
Merged

tamnd merged 1 commit into
mainfrom
clientconn

Conversation

@tamnd

@tamnd tamnd commented Oct 27, 2026

Copy link
Copy Markdown
Owner

What this is

A port of Go 1.27's clientconn.go. http_transport_new_client_conn dials a single HTTP/1 or HTTP/2 connection that the caller manages, outside the transport's pool. Every method of Go's ClientConn is here, along with the six tests from clientconn_test.go. This is part of #6.

Go burrow
(*Transport).NewClientConn http_transport_new_client_conn
(*ClientConn).RoundTrip http_client_conn_round_trip
Available, InFlight http_client_conn_available, http_client_conn_in_flight
Reserve, Release http_client_conn_reserve, http_client_conn_release
Err, Close http_client_conn_err, http_client_conn_close
SetStateHook http_client_conn_set_state_hook

How it fits together

Go keeps a small genericClientConn interface between ClientConn and the two protocols, and this port uses a vtable for it.

  • HTTP/1: the connection is a persistConn dialed in client conn mode, so it never goes back to the idle pool. The new methods are Go's http1ClientConn ones (availability through availch, reserved, inFlight).
  • HTTP/2: when ALPN picks h2, the conn becomes Go's NetHTTPClientConn, made by burrow__http2_transport_new_client_conn with strictMaxConcurrentStreams set. Go calls the internal state hook from four places (closeConn, cleanupWriteRequest after donec closes, settings processing, and the ping ack), and so does this port.
  • State hook: maybeRunStateHook and shouldRunStateHook keep Go's debounce. The first call is synchronous, and any changes after it are picked up by one goroutine.

Not in Go

C has no garbage collector, so the caller frees the conn with http_client_conn_free. That has to happen before the transport is freed, and never from inside the state hook. The HttpClientConn is refcounted:

  • one ref for the caller;
  • one for the connection, dropped when the connection is freed;
  • one while a hook goroutine is running.

Free waits for any running hook to finish before it closes the connection. If no goroutine can be started, the hook runs inline. When request validation fails, round trip closes the body, the same way a RoundTripper does.

tools/burrow-coverage gets a func(*ClientConn) entry so that HttpClientConnStateHook counts as SetStateHook's parameter type.

Tests

tests/http_clientconn_test.c has all six tests. Each one runs in h1 and h2 mode, and the five that use runSynctest in Go use synctest here. The test server is the in-memory httptest one, so mode_addr is a fixed example.tld address and not a listener's. That part is marked Not in Go.

I ran these on a Linux box with clang, building http_clientconn_test, http_transport_test, http2_transport_test, http_client_test and httptest_test:

Build Result
plain, -test.short all 5 PASS
ASan+UBSan, -test.short all 5 PASS
TSan, -test.short all 5 PASS
plain, -test.count=5 all 5 PASS
TSan, http_clientconn_test -test.count=5, 3 times PASS each time

The static checks (banned, headers, annotations, alloc, globals, statics, fips-module) passed there too. On the Mac, coverage now reports net/http with no signature differences, and doc --check and the cheatsheet check pass.

Port Go 1.27's clientconn.go: Transport.NewClientConn dials one HTTP/1
or HTTP/2 connection the caller manages, with Available, InFlight,
Reserve, Release, Err, Close and a state hook. The HTTP/1 side is a
persistConn with the clientConn flag, the HTTP/2 side is Go's
NetHTTPClientConn, and both sit behind one small vtable.

The tests are clientconn_test.go's six, in h1 and h2 modes.
@tamnd tamnd added port Porting a standard library package, or part of one area: runtime Scheduler, goroutines, channels, select, defer, panic, netpoller labels Oct 27, 2026
@tamnd
tamnd merged commit 9767c3d into main Oct 27, 2026
0 of 44 checks passed
@tamnd
tamnd deleted the clientconn branch October 27, 2026 15:15
@tamnd tamnd mentioned this pull request Oct 27, 2026
23 of 27 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: runtime Scheduler, goroutines, channels, select, defer, panic, netpoller port Porting a standard library package, or part of one

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant