diff options
author | Michael Fraenkel <michael.fraenkel@gmail.com> | 2019-10-18 22:19:59 -0400 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2019-10-29 16:45:39 +0000 |
commit | 1a058cd04b7b902e59ff5584c7fda8ddfe388981 (patch) | |
tree | 7b3e78f56bb650c12ffc79b05ef8e679d8939003 /src/net/http/transport_test.go | |
parent | e7ce8627b0adb0dfa8657c5186a1a53e1baad404 (diff) | |
download | go-1a058cd04b7b902e59ff5584c7fda8ddfe388981.tar.gz go-1a058cd04b7b902e59ff5584c7fda8ddfe388981.zip |
net/http: only decrement connection count if we removed a connection
The connection count must only be decremented if the persistent
connection was also removed.
Fixes #34941
Change-Id: I5070717d5d9effec78016005fa4910593500c8cf
Reviewed-on: https://go-review.googlesource.com/c/go/+/202087
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/net/http/transport_test.go')
-rw-r--r-- | src/net/http/transport_test.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go index 3673ed29f0..0d63e46d4f 100644 --- a/src/net/http/transport_test.go +++ b/src/net/http/transport_test.go @@ -5893,3 +5893,59 @@ func TestDontCacheBrokenHTTP2Conn(t *testing.T) { t.Errorf("GotConn calls = %v; want %v", got, want) } } + +// Issue 34941 +// When the client has too many concurrent requests on a single connection, +// http.http2noCachedConnError is reported on multiple requests. There should +// only be one decrement regardless of the number of failures. +func TestTransportDecrementConnWhenIdleConnRemoved(t *testing.T) { + defer afterTest(t) + + h := HandlerFunc(func(w ResponseWriter, r *Request) { + _, err := w.Write([]byte("foo")) + if err != nil { + t.Fatalf("Write: %v", err) + } + }) + + ts := httptest.NewUnstartedServer(h) + ts.EnableHTTP2 = true + ts.StartTLS() + defer ts.Close() + + c := ts.Client() + tr := c.Transport.(*Transport) + tr.MaxConnsPerHost = 1 + if err := ExportHttp2ConfigureTransport(tr); err != nil { + t.Fatalf("ExportHttp2ConfigureTransport: %v", err) + } + + errCh := make(chan error, 300) + doReq := func() { + resp, err := c.Get(ts.URL) + if err != nil { + errCh <- fmt.Errorf("request failed: %v", err) + return + } + defer resp.Body.Close() + _, err = ioutil.ReadAll(resp.Body) + if err != nil { + errCh <- fmt.Errorf("read body failed: %v", err) + } + } + + var wg sync.WaitGroup + for i := 0; i < 300; i++ { + wg.Add(1) + go func() { + defer wg.Done() + doReq() + }() + } + wg.Wait() + close(errCh) + + for err := range errCh { + t.Errorf("error occurred: %v", err) + } +} |