From cb62365f5737d8c6a803b0737b3f34a64e526b6b Mon Sep 17 00:00:00 2001 From: Alexey Borzenkov Date: Mon, 28 May 2012 10:46:51 -0700 Subject: [PATCH] net/http: reuse http proxy connections for different http requests Comment on cache keys above connectMethod says "http to proxy, http anywhere after that", however in reality target address was always included, which prevented http requests to different target addresses to reuse the same http proxy connection. R=golang-dev, r, rsc, bradfitz CC=golang-dev https://golang.org/cl/5901064 --- src/pkg/net/http/proxy_test.go | 30 ++++++++++++++++++++++++++++++ src/pkg/net/http/transport.go | 6 +++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/pkg/net/http/proxy_test.go b/src/pkg/net/http/proxy_test.go index 9b320b3aa5..5ecffaface 100644 --- a/src/pkg/net/http/proxy_test.go +++ b/src/pkg/net/http/proxy_test.go @@ -5,6 +5,7 @@ package http import ( + "net/url" "os" "testing" ) @@ -46,3 +47,32 @@ func TestUseProxy(t *testing.T) { } } } + +var cacheKeysTests = []struct { + proxy string + scheme string + addr string + key string +}{ + {"", "http", "foo.com", "|http|foo.com"}, + {"", "https", "foo.com", "|https|foo.com"}, + {"http://foo.com", "http", "foo.com", "http://foo.com|http|"}, + {"http://foo.com", "https", "foo.com", "http://foo.com|https|foo.com"}, +} + +func TestCacheKeys(t *testing.T) { + for _, tt := range cacheKeysTests { + var proxy *url.URL + if tt.proxy != "" { + u, err := url.Parse(tt.proxy) + if err != nil { + t.Fatal(err) + } + proxy = u + } + cm := connectMethod{proxy, tt.scheme, tt.addr} + if cm.String() != tt.key { + t.Fatalf("{%q, %q, %q} cache key %q; want %q", tt.proxy, tt.scheme, tt.addr, cm.String(), tt.key) + } + } +} diff --git a/src/pkg/net/http/transport.go b/src/pkg/net/http/transport.go index 483af556e4..dd514386ac 100644 --- a/src/pkg/net/http/transport.go +++ b/src/pkg/net/http/transport.go @@ -450,10 +450,14 @@ type connectMethod struct { func (ck *connectMethod) String() string { proxyStr := "" + targetAddr := ck.targetAddr if ck.proxyURL != nil { proxyStr = ck.proxyURL.String() + if ck.targetScheme == "http" { + targetAddr = "" + } } - return strings.Join([]string{proxyStr, ck.targetScheme, ck.targetAddr}, "|") + return strings.Join([]string{proxyStr, ck.targetScheme, targetAddr}, "|") } // addr returns the first hop "host:port" to which we need to TCP connect. -- 2.48.1