Skip to content

Commit 2707ee2

Browse files
committed
internal/http3: implement HTTP/3 clientConn methods
Implement Err, Reserve, Release, Available, and InFlight methods in our HTTP/3 implementation. This allows clientConn to implement the genericClientConn interface in net/http. Given that we have no good understanding yet of when one might want to create multiple HTTP/3 connections to the same server, Available will always return MaxInt. For golang/go#70914 Change-Id: Ib1316b33195b6f5d584c5a371d60cddf6a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/784500 Reviewed-by: Nicholas Husin <[email protected]> Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]>
1 parent 31358cc commit 2707ee2

3 files changed

Lines changed: 192 additions & 11 deletions

File tree

‎internal/http3/roundtrip.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ type roundTripState struct {
4141
func (rt *roundTripState) abort(err error) error {
4242
rt.errOnce.Do(func() {
4343
rt.err = err
44+
45+
rt.cc.mu.Lock()
46+
rt.cc.active--
47+
rt.cc.mu.Unlock()
48+
rt.cc.maybeCallStateHook()
49+
4450
switch e := err.(type) {
4551
case *connectionError:
4652
rt.cc.abort(e)
@@ -88,9 +94,20 @@ func (rt *roundTripState) maybeCallWait100Continue() {
8894

8995
// RoundTrip sends a request on the connection.
9096
func (cc *clientConn) RoundTrip(req *http.Request) (_ *http.Response, err error) {
97+
cc.mu.Lock()
98+
if cc.reserved > 0 {
99+
cc.reserved--
100+
}
101+
cc.active++
102+
cc.mu.Unlock()
103+
91104
// Each request gets its own QUIC stream.
92105
st, err := newConnStream(req.Context(), cc.qconn, streamTypeRequest)
93106
if err != nil {
107+
cc.mu.Lock()
108+
cc.active--
109+
cc.mu.Unlock()
110+
cc.maybeCallStateHook()
94111
return nil, err
95112
}
96113
rt := &roundTripState{

‎internal/http3/transport.go‎

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package http3
66

77
import (
88
"context"
9+
"errors"
910
"fmt"
11+
"math"
1012
"net/http"
1113
"net/url"
1214
"sync"
@@ -51,8 +53,8 @@ func (t netHTTPTransport) RoundTrip(*http.Request) (*http.Response, error) {
5153
panic("netHTTPTransport.RoundTrip should never be called")
5254
}
5355

54-
func (t netHTTPTransport) DialClientConn(ctx context.Context, addr string, _ *url.URL, _ func()) (http.RoundTripper, error) {
55-
return t.transport.dial(ctx, addr)
56+
func (t netHTTPTransport) DialClientConn(ctx context.Context, addr string, _ *url.URL, stateHook func()) (http.RoundTripper, error) {
57+
return t.transport.dial(ctx, addr, stateHook)
5658
}
5759

5860
type TransportOpts struct {
@@ -138,7 +140,7 @@ func (tr *transport) initEndpoint() (err error) {
138140
}
139141

140142
// dial creates a new HTTP/3 client connection.
141-
func (tr *transport) dial(ctx context.Context, target string) (*clientConn, error) {
143+
func (tr *transport) dial(ctx context.Context, target string, stateHook func()) (*clientConn, error) {
142144
tr.incInFlightDials()
143145
defer tr.decInFlightDials()
144146

@@ -149,7 +151,7 @@ func (tr *transport) dial(ctx context.Context, target string) (*clientConn, erro
149151
if err != nil {
150152
return nil, err
151153
}
152-
return tr.newClientConn(ctx, qconn)
154+
return tr.newClientConn(ctx, qconn, stateHook)
153155
}
154156

155157
// CloseIdleConnections is called by net/http.Transport.CloseIdleConnections
@@ -177,6 +179,13 @@ type clientConn struct {
177179

178180
enc qpackEncoder
179181
dec qpackDecoder
182+
183+
// Guarded by genericConn.mu
184+
reserved int
185+
active int
186+
closed bool
187+
188+
stateHook func()
180189
}
181190

182191
func (tr *transport) registerConn(cc *clientConn) {
@@ -191,9 +200,10 @@ func (tr *transport) unregisterConn(cc *clientConn) {
191200
delete(tr.activeConns, cc)
192201
}
193202

194-
func (tr *transport) newClientConn(ctx context.Context, qconn *quic.Conn) (*clientConn, error) {
203+
func (tr *transport) newClientConn(ctx context.Context, qconn *quic.Conn, stateHook func()) (*clientConn, error) {
195204
cc := &clientConn{
196-
qconn: qconn,
205+
qconn: qconn,
206+
stateHook: stateHook,
197207
}
198208
tr.registerConn(cc)
199209
cc.enc.init()
@@ -209,12 +219,15 @@ func (tr *transport) newClientConn(ctx context.Context, qconn *quic.Conn) (*clie
209219

210220
go func() {
211221
cc.acceptStreams(qconn, cc)
222+
cc.mu.Lock()
223+
cc.closed = true
224+
cc.mu.Unlock()
212225
tr.unregisterConn(cc)
226+
cc.maybeCallStateHook()
213227
}()
214228
return cc, nil
215229
}
216230

217-
// TODO: implement the rest of net/http.ClientConn methods beyond Close.
218231
func (cc *clientConn) Close() error {
219232
// We need to use Close rather than Abort on the QUIC connection.
220233
// Otherwise, when a net/http.Transport.CloseIdleConnections is called, it
@@ -226,22 +239,63 @@ func (cc *clientConn) Close() error {
226239
}
227240

228241
func (cc *clientConn) Err() error {
242+
cc.mu.Lock()
243+
defer cc.mu.Unlock()
244+
if cc.closed {
245+
return errors.New("connection closed")
246+
}
229247
return nil
230248
}
231249

232250
func (cc *clientConn) Reserve() error {
251+
cc.mu.Lock()
252+
defer cc.mu.Unlock()
253+
if cc.closed {
254+
return errors.New("connection closed")
255+
}
256+
cc.reserved++
233257
return nil
234258
}
235259

236260
func (cc *clientConn) Release() {
261+
cc.mu.Lock()
262+
defer cc.mu.Unlock()
263+
// This is consistent with RoundTrip: both Release and RoundTrip will
264+
// consume a reservation iff one exists.
265+
if cc.reserved > 0 {
266+
cc.reserved--
267+
}
237268
}
238269

239270
func (cc *clientConn) Available() int {
240-
return 0
271+
cc.mu.Lock()
272+
defer cc.mu.Unlock()
273+
if cc.closed {
274+
return 0
275+
}
276+
// The general recommendation for HTTP/3 is to reuse the same connection
277+
// for multiple requests rather than creating new connections. As of now,
278+
// we don't have a good understanding of when one might want to create
279+
// multiple HTTP/3 connections to the same server.
280+
// Therefore, for ClientConn API, let HTTP/3 connections have no limit.
281+
// Starting a new RoundTrip when we are at the connection limit will just
282+
// block until a new max stream limit is received.
283+
return math.MaxInt
241284
}
242285

243286
func (cc *clientConn) InFlight() int {
244-
return 0
287+
cc.mu.Lock()
288+
defer cc.mu.Unlock()
289+
if cc.closed {
290+
return 0
291+
}
292+
return cc.reserved + cc.active
293+
}
294+
295+
func (cc *clientConn) maybeCallStateHook() {
296+
if cc.stateHook != nil {
297+
cc.stateHook()
298+
}
245299
}
246300

247301
func (cc *clientConn) handleControlStream(st *stream) error {

‎internal/http3/transport_test.go‎

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"io"
1313
"maps"
14+
"math"
1415
"net/http"
1516
"reflect"
1617
"slices"
@@ -34,6 +35,111 @@ func TestTransportServerCreatesBidirectionalStream(t *testing.T) {
3435
})
3536
}
3637

38+
func TestClientConnMethods(t *testing.T) {
39+
synctest.Test(t, func(t *testing.T) {
40+
var called bool
41+
hook := func() {
42+
if called {
43+
t.Error("state hook was unexpectedly called")
44+
}
45+
called = true
46+
}
47+
verifyHookWasCalled := func() {
48+
if !called {
49+
t.Error("state hook was unexpectedly not called")
50+
}
51+
called = false
52+
}
53+
tc := newTestClientConnWithHook(t, hook)
54+
tc.greet()
55+
56+
// Initial state after establishing connection.
57+
if err := tc.cc.Err(); err != nil {
58+
t.Errorf("cc.Err() = %v, want nil", err)
59+
}
60+
if avail := tc.cc.Available(); avail != math.MaxInt {
61+
t.Errorf("cc.Available() = %v, want %v", avail, math.MaxInt)
62+
}
63+
if inFlight := tc.cc.InFlight(); inFlight != 0 {
64+
t.Errorf("cc.InFlight() = %v, want 0", inFlight)
65+
}
66+
67+
// Release, with Reserve before and without.
68+
for _, reserveBefore := range []bool{true, false} {
69+
if reserveBefore {
70+
if err := tc.cc.Reserve(); err != nil {
71+
t.Fatalf("cc.Reserve() failed: %v", err)
72+
}
73+
if avail := tc.cc.Available(); avail != math.MaxInt {
74+
t.Errorf("after Reserve, cc.Available() = %v, want %v", avail, math.MaxInt)
75+
}
76+
if inFlight := tc.cc.InFlight(); inFlight != 1 {
77+
t.Errorf("after Reserve, cc.InFlight() = %v, want 1", inFlight)
78+
}
79+
}
80+
tc.cc.Release()
81+
if avail := tc.cc.Available(); avail != math.MaxInt {
82+
t.Errorf("after Release, cc.Available() = %v, want %v", avail, math.MaxInt)
83+
}
84+
if inFlight := tc.cc.InFlight(); inFlight != 0 {
85+
t.Errorf("after Release, cc.InFlight() = %v, want 0", inFlight)
86+
}
87+
}
88+
89+
// RoundTrip, with Reserve before and without.
90+
for _, reserveBefore := range []bool{true, false} {
91+
if reserveBefore {
92+
if err := tc.cc.Reserve(); err != nil {
93+
t.Fatalf("cc.Reserve() failed: %v", err)
94+
}
95+
if avail := tc.cc.Available(); avail != math.MaxInt {
96+
t.Errorf("after Reserve, cc.Available() = %v, want %v", avail, math.MaxInt)
97+
}
98+
if inFlight := tc.cc.InFlight(); inFlight != 1 {
99+
t.Errorf("after Reserve, cc.InFlight() = %v, want 1", inFlight)
100+
}
101+
}
102+
req, _ := http.NewRequest("GET", "https://example.com/", nil)
103+
rt := tc.roundTrip(req)
104+
if avail := tc.cc.Available(); avail != math.MaxInt {
105+
t.Errorf("after RoundTrip, cc.Available() = %v, want %v", avail, math.MaxInt)
106+
}
107+
st := tc.wantStream(streamTypeRequest)
108+
st.wantHeaders(nil)
109+
st.writeHeaders(http.Header{":status": []string{"200"}})
110+
resp := rt.response()
111+
if resp.StatusCode != 200 {
112+
t.Errorf("resp.StatusCode = %v, want 200", resp.StatusCode)
113+
}
114+
if inFlight := tc.cc.InFlight(); inFlight != 1 { // InFlight should decrement only after the body is closed.
115+
t.Errorf("before body close, cc.InFlight() = %v, want 1", inFlight)
116+
}
117+
resp.Body.Close()
118+
if inFlight := tc.cc.InFlight(); inFlight != 0 {
119+
t.Errorf("after body close, cc.InFlight() = %v, want 0", inFlight)
120+
}
121+
verifyHookWasCalled()
122+
}
123+
124+
// Connection closure.
125+
if err := tc.cc.Reserve(); err != nil {
126+
t.Fatalf("cc.Reserve() failed: %v", err)
127+
}
128+
tc.cc.Close()
129+
synctest.Wait()
130+
verifyHookWasCalled()
131+
if err := tc.cc.Err(); err == nil {
132+
t.Error("after connection is closed, cc.Err() = nil, want err")
133+
}
134+
if avail := tc.cc.Available(); avail != 0 {
135+
t.Errorf("after connection is closed, cc.Available() = %v, want 0", avail)
136+
}
137+
if inFlight := tc.cc.InFlight(); inFlight != 0 {
138+
t.Errorf("after connection is closed, cc.InFlight() = %v, want 0", inFlight)
139+
}
140+
})
141+
}
142+
37143
// A testQUICConn wraps a *quic.Conn and provides methods for inspecting it.
38144
type testQUICConn struct {
39145
t testing.TB
@@ -440,7 +546,7 @@ type testClientConn struct {
440546
control *testQUICStream
441547
}
442548

443-
func newTestClientConn(t testing.TB) *testClientConn {
549+
func newTestClientConnWithHook(t testing.TB, stateHook func()) *testClientConn {
444550
e1, e2 := newQUICEndpointPair(t)
445551
tr := &transport{
446552
endpoint: e1,
@@ -450,7 +556,7 @@ func newTestClientConn(t testing.TB) *testClientConn {
450556
activeConns: make(map[*clientConn]struct{}),
451557
}
452558

453-
cc, err := tr.dial(t.Context(), e2.LocalAddr().String())
559+
cc, err := tr.dial(t.Context(), e2.LocalAddr().String(), stateHook)
454560
if err != nil {
455561
t.Fatal(err)
456562
}
@@ -471,6 +577,10 @@ func newTestClientConn(t testing.TB) *testClientConn {
471577
return tc
472578
}
473579

580+
func newTestClientConn(t testing.TB) *testClientConn {
581+
return newTestClientConnWithHook(t, nil)
582+
}
583+
474584
// greet performs initial connection handshaking with the client.
475585
func (tc *testClientConn) greet() {
476586
// Client creates a control stream.

0 commit comments

Comments
 (0)