[tor-commits] [meek/master] Reuse the same RoundTripper for all requests.

dcf at torproject.org dcf at torproject.org
Thu Sep 25 09:31:10 UTC 2014


commit a4427ae4f5907a8afc538f4403bf475148b273dc
Author: David Fifield <david at bamsoftware.com>
Date:   Thu Sep 25 02:01:12 2014 -0700

    Reuse the same RoundTripper for all requests.
    
    Previously we were creating a new http.Transport for each request, and
    selectively overriding the Proxy member. Since that code was first
    added, http.DefaultTransport has gotten other non-zero settings than
    Proxy: Dial and TLSHandshakeTimeout.
    
    https://code.google.com/p/go/source/diff?spec=svn733fefb1deae5490f2d6fcf498667a9077f92a42&name=733fefb1deae&r=733fefb1deae5490f2d6fcf498667a9077f92a42&format=side&path=/src/pkg/net/http/transport.go
    https://code.google.com/p/go/source/diff?spec=svnc8edfe4ddd5a28d929503ac34d3918759518970c&name=c8edfe4ddd5a&r=c8edfe4ddd5a28d929503ac34d3918759518970c&format=side&path=/src/pkg/net/http/transport.go
    
    We want to use those settings, but we also want to disable the default
    ProxyFromEnvironment. Creating a new Transport every time was messing
    with persistent connections; I watched as meek-client made a new TCP
    connection for every request. With this change, it reuses the same
    connection. Something must have changed in the http library (perhaps one
    of the revisions cited above), because I remember it using a persistent
    connection in the past.
    
    This only makes a difference when not using --helper. The browser helper
    handles all its own persistence stuff.
---
 meek-client/meek-client.go |   21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/meek-client/meek-client.go b/meek-client/meek-client.go
index d39e06a..33861e5 100644
--- a/meek-client/meek-client.go
+++ b/meek-client/meek-client.go
@@ -82,6 +82,10 @@ const (
 
 var ptInfo pt.ClientInfo
 
+// This is the RoundTripper used to make all our requests (when --helper is not
+// used).
+var httpTransport http.Transport
+
 // Store for command line options.
 var options struct {
 	URL        string
@@ -110,13 +114,6 @@ type RequestInfo struct {
 // Do an HTTP roundtrip using the payload data in buf and the request metadata
 // in info.
 func roundTripWithHTTP(buf []byte, info *RequestInfo) (*http.Response, error) {
-	tr := new(http.Transport)
-	if options.ProxyURL != nil {
-		if options.ProxyURL.Scheme != "http" {
-			panic(fmt.Sprintf("don't know how to use proxy %s", options.ProxyURL.String()))
-		}
-		tr.Proxy = http.ProxyURL(options.ProxyURL)
-	}
 	req, err := http.NewRequest("POST", info.URL.String(), bytes.NewReader(buf))
 	if err != nil {
 		return nil, err
@@ -125,7 +122,7 @@ func roundTripWithHTTP(buf []byte, info *RequestInfo) (*http.Response, error) {
 		req.Host = info.Host
 	}
 	req.Header.Set("X-Session-Id", info.SessionID)
-	return tr.RoundTrip(req)
+	return httpTransport.RoundTrip(req)
 }
 
 // Do a roundtrip, trying at most limit times if there is an HTTP status other
@@ -394,6 +391,13 @@ func main() {
 		}
 	}
 
+	// We make a copy of DefaultTransport because we want the default Dial
+	// and TLSHandshakeTimeout settings. But we want to disable the default
+	// ProxyFromEnvironment setting. Proxy is overridden below if proxyURL
+	// is set.
+	httpTransport = *http.DefaultTransport.(*http.Transport)
+	httpTransport.Proxy = nil
+
 	ptInfo, err = pt.ClientSetup([]string{ptMethodName})
 	if err != nil {
 		log.Fatalf("error in ClientSetup: %s", err)
@@ -416,6 +420,7 @@ func main() {
 			log.Fatal(fmt.Sprintf("proxy error: %s", err))
 		}
 		log.Printf("using proxy %s", options.ProxyURL.String())
+		httpTransport.Proxy = http.ProxyURL(options.ProxyURL)
 		if ptProxyURL != nil {
 			PtProxyDone()
 		}





More information about the tor-commits mailing list