http3: allow re-dialing of connection after a dial error (#4573)

* http3: do not cache dial error

* add an integration test

* http3: add a unit test for dial failures

---------

Co-authored-by: 世界 <i@sekai.icu>
This commit is contained in:
Marten Seemann
2024-06-23 12:38:49 +08:00
committed by GitHub
parent 10f8bd4e06
commit 2a082f973a
3 changed files with 56 additions and 0 deletions

View File

@@ -166,6 +166,7 @@ func (r *RoundTripper) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.
}
if cl.dialErr != nil {
r.removeClient(hostname)
return nil, cl.dialErr
}
defer cl.useCount.Add(-1)
@@ -258,6 +259,7 @@ func (r *RoundTripper) getClient(ctx context.Context, hostname string, onlyCache
select {
case <-cl.dialing:
if cl.dialErr != nil {
delete(r.clients, hostname)
return nil, false, cl.dialErr
}
select {

View File

@@ -265,6 +265,37 @@ var _ = Describe("RoundTripper", func() {
Expect(count).To(Equal(1))
})
It("redials a connection if dialing failed", func() {
cl1 := NewMockSingleRoundTripper(mockCtrl)
clientChan <- cl1
req1, err := http.NewRequest("GET", "https://quic-go.net/foo.html", nil)
Expect(err).ToNot(HaveOccurred())
req2, err := http.NewRequest("GET", "https://quic-go.net/bar.html", nil)
Expect(err).ToNot(HaveOccurred())
testErr := errors.New("handshake error")
conn := mockquic.NewMockEarlyConnection(mockCtrl)
var count int
rt.Dial = func(context.Context, string, *tls.Config, *quic.Config) (quic.EarlyConnection, error) {
count++
if count == 1 {
return nil, testErr
}
return conn, nil
}
handshakeChan := make(chan struct{})
close(handshakeChan)
conn.EXPECT().HandshakeComplete().Return(handshakeChan).MaxTimes(2)
cl1.EXPECT().RoundTrip(req2).Return(&http.Response{Request: req2}, nil)
_, err = rt.RoundTrip(req1)
Expect(err).To(MatchError(testErr))
rsp, err := rt.RoundTrip(req2)
Expect(err).ToNot(HaveOccurred())
Expect(rsp.Request).To(Equal(req2))
Expect(count).To(Equal(2))
})
It("immediately removes a clients when a request errored", func() {
cl1 := NewMockSingleRoundTripper(mockCtrl)
clientChan <- cl1