Skip to content

Commit 01e3d03

Browse files
neildgopherbot
authored andcommitted
http2: coalesce dials via wrapped Transport.RoundTrip
The http2.Transport has historically coalesced dials for requests sent via its RoundTrip (as opposed to via net/http). The wrapping implementation inadvertently removed dial coalescing. Add it back. Fixes golang/go#81646 Change-Id: Ib174d16f7cd9508efb0eca470d4dbf266a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/845005 Reviewed-by: Nicholas Husin <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Auto-Submit: Damien Neil <[email protected]> Reviewed-by: Nicholas Husin <[email protected]>
1 parent 2824783 commit 01e3d03

6 files changed

Lines changed: 1046 additions & 36 deletions

File tree

‎http2/clientconn_test.go‎

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ func TestTestTransport(t *testing.T) {
100100
})
101101
}
102102
func testTestTransport(t testing.TB, mode roundTripTestMode) {
103-
tt := newTestTransport(t)
103+
tt := newTestTransport(t, mode)
104104

105-
req := Must(http.NewRequest("GET", "https://dummy.tld/", nil))
105+
req := Must(http.NewRequest("GET", "http://dummy.tld/", nil))
106106
rt := tt.roundTrip(req)
107107
tc := tt.getConn()
108108
tc.wantFrameType(FrameSettings)
@@ -461,6 +461,16 @@ func (rt *testRoundTrip) err() error {
461461
return err
462462
}
463463

464+
// wantErr indicates the expected RoundTrip error (as compared with Is).
465+
func (rt *testRoundTrip) wantErr(want error) {
466+
t := rt.t
467+
t.Helper()
468+
_, got := rt.result()
469+
if !errors.Is(got, want) {
470+
t.Fatalf("got error:\n%v\nwant:\n%v", got, want)
471+
}
472+
}
473+
464474
// wantStatus indicates the expected response StatusCode.
465475
func (rt *testRoundTrip) wantStatus(want int) {
466476
t := rt.t
@@ -491,6 +501,21 @@ func (rt *testRoundTrip) wantBody(want []byte) {
491501
}
492502
}
493503

504+
// wantPartialBody indicates the next expected chunk of response body.
505+
func (rt *testRoundTrip) wantPartialBody(want []byte) {
506+
t := rt.t
507+
t.Helper()
508+
got := make([]byte, len(want))
509+
n, err := io.ReadFull(rt.response().Body, got)
510+
got = got[:n]
511+
if err != nil {
512+
t.Fatalf("unexpected error reading response body: %v", err)
513+
}
514+
if !bytes.Equal(got, want) {
515+
t.Fatalf("unexpected partial response body:\ngot: %q\nwant: %q", got, want)
516+
}
517+
}
518+
494519
// wantHeaders indicates the expected response headers.
495520
func (rt *testRoundTrip) wantHeaders(want http.Header) {
496521
t := rt.t
@@ -559,17 +584,30 @@ type testTransport struct {
559584
ccqueue []*testClientConn
560585
ccs map[*synctestNetConn]*testClientConn
561586

587+
controlledDials bool
588+
dialsMu sync.Mutex
589+
dials []*testPendingDial
590+
562591
ccpending []*testPendingClientConn
563592

564593
useTLS bool
565594
}
566595

596+
type testPendingDial struct {
597+
address string
598+
errc chan error
599+
}
600+
601+
func (d *testPendingDial) finish(err error) { d.errc <- err }
602+
567603
type testPendingClientConn struct {
568604
nc *synctestNetConn
569605
cc *ClientConn
570606
tc *testClientConn
571607
}
572608

609+
type testControlledDials bool
610+
573611
func newTestTransport(t testing.TB, opts ...any) *testTransport {
574612
tt := &testTransport{
575613
t: t,
@@ -593,6 +631,9 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
593631
case roundTripXNetHTTP2:
594632
tr = &Transport{
595633
DialTLSContext: func(ctx context.Context, network, address string, tlsConf *tls.Config) (net.Conn, error) {
634+
if err := tt.startDial(address); err != nil {
635+
return nil, err
636+
}
596637
if tt.useTLS {
597638
return tls.Client(tt.li.newConn(), tlsConf), nil
598639
}
@@ -607,6 +648,9 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
607648
}
608649

609650
tr1.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
651+
if err := tt.startDial(address); err != nil {
652+
return nil, err
653+
}
610654
return tt.li.newConn(), nil
611655
}
612656
tr1.TLSClientConfig = testTLSClientConfig
@@ -633,6 +677,8 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
633677
tr = o
634678
case roundTripTestMode:
635679
tt.mode = o
680+
case testControlledDials:
681+
tt.controlledDials = bool(o)
636682
case nil:
637683
default:
638684
t.Fatalf("unsupported option %T", o)
@@ -648,6 +694,11 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
648694
tt.maybeAddNewClientConnHook()
649695

650696
t.Cleanup(func() {
697+
tt.dialsMu.Lock()
698+
for _, dial := range tt.dials {
699+
dial.finish(errors.New("test is done"))
700+
}
701+
tt.dialsMu.Unlock()
651702
tt.li.Close()
652703
synctest.Wait()
653704
if len(tt.ccqueue) > 0 {
@@ -664,7 +715,7 @@ func (tt *testTransport) addPending(nc *synctestNetConn, cc *ClientConn, tc *tes
664715

665716
for i, p := range tt.ccpending {
666717
if p.nc != nc {
667-
break
718+
continue
668719
}
669720
if p.tc != nil {
670721
p.tc.cc = cc
@@ -696,11 +747,67 @@ func (tt *testTransport) accept() {
696747
}
697748
}
698749

750+
func (tt *testTransport) startDial(address string) error {
751+
if !tt.controlledDials {
752+
return nil
753+
}
754+
dial := &testPendingDial{
755+
address: address,
756+
errc: make(chan error, 1),
757+
}
758+
tt.dialsMu.Lock()
759+
tt.dials = append(tt.dials, dial)
760+
tt.dialsMu.Unlock()
761+
select {
762+
case err := <-dial.errc:
763+
return err
764+
case <-tt.t.Context().Done():
765+
return tt.t.Context().Err()
766+
}
767+
}
768+
769+
func (tt *testTransport) wantDial(address string) *testPendingDial {
770+
tt.t.Helper()
771+
synctest.Wait()
772+
tt.dialsMu.Lock()
773+
defer tt.dialsMu.Unlock()
774+
idx := slices.IndexFunc(tt.dials, func(dial *testPendingDial) bool {
775+
return dial.address == address
776+
})
777+
if idx < 0 {
778+
var dials []string
779+
for _, d := range tt.dials {
780+
dials = append(dials, d.address)
781+
}
782+
tt.t.Fatalf("no dial to %q in progress (have %q); wanted one", address, dials)
783+
}
784+
dial := tt.dials[idx]
785+
tt.dials = slices.Delete(tt.dials, idx, idx+1)
786+
return dial
787+
}
788+
789+
func (tt *testTransport) wantIdle() {
790+
tt.t.Helper()
791+
synctest.Wait()
792+
tt.dialsMu.Lock()
793+
defer tt.dialsMu.Unlock()
794+
if len(tt.dials) > 0 {
795+
tt.t.Fatalf("%v dials in progress, want idle", len(tt.dials))
796+
}
797+
}
798+
699799
func (tt *testTransport) hasConn() bool {
700800
return len(tt.ccqueue) > 0
701801
}
702802

703803
func (tt *testTransport) getConn() *testClientConn {
804+
tt.t.Helper()
805+
tc := tt.getConnNoPreface()
806+
tc.readClientPreface()
807+
return tc
808+
}
809+
810+
func (tt *testTransport) getConnNoPreface() *testClientConn {
704811
tt.t.Helper()
705812
synctest.Wait()
706813
tt.ccMu.Lock()
@@ -711,7 +818,6 @@ func (tt *testTransport) getConn() *testClientConn {
711818
tc := tt.ccqueue[0]
712819
tt.ccqueue = tt.ccqueue[1:]
713820
tt.ccMu.Unlock()
714-
tc.readClientPreface()
715821
return tc
716822
}
717823

‎http2/connframes_test.go‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,26 @@ type wantHeader struct {
145145
// and asserts that they contain the expected headers.
146146
func (tf *testConnFramer) wantHeaders(want wantHeader) {
147147
tf.t.Helper()
148-
149-
hf := readFrame[*HeadersFrame](tf.t, tf)
150-
if got, want := hf.StreamID, want.streamID; got != want {
148+
streamID, endStream, gotHeader := tf.readHeaders()
149+
if got, want := streamID, want.streamID; got != want {
151150
tf.t.Fatalf("got stream ID %v, want %v", got, want)
152151
}
153-
if got, want := hf.StreamEnded(), want.endStream; got != want {
152+
if got, want := endStream, want.endStream; got != want {
154153
tf.t.Fatalf("got stream ended %v, want %v", got, want)
155154
}
155+
for k, v := range want.header {
156+
if !reflect.DeepEqual(v, gotHeader[k]) {
157+
tf.t.Fatalf("got header %q = %q; want %q = %q", k, gotHeader[k], k, v)
158+
}
159+
}
160+
}
156161

157-
gotHeader := make(http.Header)
162+
func (tf *testConnFramer) readHeaders() (streamID uint32, endStream bool, gotHeader http.Header) {
163+
tf.t.Helper()
164+
hf := readFrame[*HeadersFrame](tf.t, tf)
165+
streamID = hf.StreamID
166+
endStream = hf.StreamEnded()
167+
gotHeader = make(http.Header)
158168
tf.dec.SetEmitFunc(func(hf hpack.HeaderField) {
159169
gotHeader[hf.Name] = append(gotHeader[hf.Name], hf.Value)
160170
})
@@ -176,12 +186,7 @@ func (tf *testConnFramer) wantHeaders(want wantHeader) {
176186
if err := tf.dec.Close(); err != nil {
177187
tf.t.Fatalf("hpack decoding error: %v", err)
178188
}
179-
180-
for k, v := range want.header {
181-
if !reflect.DeepEqual(v, gotHeader[k]) {
182-
tf.t.Fatalf("got header %q = %q; want %q = %q", k, gotHeader[k], k, v)
183-
}
184-
}
189+
return streamID, endStream, gotHeader
185190
}
186191

187192
// decodeHeader supports some older server tests.

‎http2/export_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
MaxFrameSize = maxFrameSize
2828
MaxQueuedControlFrames = maxQueuedControlFrames
2929
MinMaxFrameSize = minMaxFrameSize
30+
CoalescedDialRetryTimeout = 0 // wrapping only
3031
)
3132

3233
type (

‎http2/export_wrap_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ package http2
88

99
import "net/http"
1010

11+
const CoalescedDialRetryTimeout = coalescedDialRetryTimeout
12+
1113
func (t *Transport) TestTransport() *http.Transport {
1214
return t.init()
1315
}

0 commit comments

Comments
 (0)