1
0
mirror of https://github.com/golang/go synced 2024-11-05 15:06:09 -07:00

net/http: remove http2 connections when no longer cached

When the http2 transport returns a NoCachedConnError, the connection
must be removed from the idle list as well as the connections per host.

Fixes #34387

Change-Id: I7875c9c95e694a37a339bb04385243b49f9b20d3
Reviewed-on: https://go-review.googlesource.com/c/go/+/196665
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Michael Fraenkel 2019-09-21 09:39:50 -04:00 committed by Brad Fitzpatrick
parent c4fbaee859
commit eb4e5defb4
2 changed files with 39 additions and 0 deletions

View File

@ -539,6 +539,7 @@ func (t *Transport) roundTrip(req *Request) (*Response, error) {
}
if http2isNoCachedConnError(err) {
t.removeIdleConn(pconn)
t.decConnsPerHost(pconn.cacheKey)
} else if !pconn.shouldRetryRequest(req, err) {
// Issue 16465: return underlying net.Conn.Read error from peek,
// as we've historically done.

View File

@ -3594,6 +3594,44 @@ func TestTransportTraceGotConnH2IdleConns(t *testing.T) {
wantIdle("after round trip", 1)
}
func TestTransportRemovesH2ConnsAfterIdle(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
trFunc := func(tr *Transport) {
tr.MaxConnsPerHost = 1
tr.MaxIdleConnsPerHost = 1
tr.IdleConnTimeout = 10 * time.Millisecond
}
cst := newClientServerTest(t, h2Mode, HandlerFunc(func(w ResponseWriter, r *Request) {}), trFunc)
defer cst.close()
if _, err := cst.c.Get(cst.ts.URL); err != nil {
t.Fatalf("got error: %s", err)
}
time.Sleep(100 * time.Millisecond)
got := make(chan error)
go func() {
if _, err := cst.c.Get(cst.ts.URL); err != nil {
got <- err
}
close(got)
}()
timeout := time.NewTimer(5 * time.Second)
defer timeout.Stop()
select {
case err := <-got:
if err != nil {
t.Fatalf("got error: %s", err)
}
case <-timeout.C:
t.Fatal("request never completed")
}
}
// This tests that an client requesting a content range won't also
// implicitly ask for gzip support. If they want that, they need to do it
// on their own.