From f24b016346ccfc1b2692ebfb802672a1dc51deaf Mon Sep 17 00:00:00 2001 From: Ta-Ching Chen Date: Sat, 26 Jan 2019 04:48:10 +0800 Subject: [PATCH] Fix goroutines change DialContext of http.DefaultTransport in router (#1063) --- router/functionHandler.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/router/functionHandler.go b/router/functionHandler.go index c53f4759..6b63469b 100644 --- a/router/functionHandler.go +++ b/router/functionHandler.go @@ -183,10 +183,7 @@ func (roundTripper RetryingRoundTripper) RoundTrip(req *http.Request) (resp *htt } // set the timeout for transport context - transport := http.DefaultTransport.(*http.Transport) - - // Disables caching, Please refer to issue and specifically comment: https://github.com/fission/fission/issues/723#issuecomment-398781995 - transport.DisableKeepAlives = true + transport := roundTripper.getDefaultTransport() executingTimeout := roundTripper.funcHandler.tsRoundTripperParams.timeout @@ -346,6 +343,26 @@ func (roundTripper RetryingRoundTripper) RoundTrip(req *http.Request) (resp *htt return resp, err } +// getDefaultTransport returns a pointer to new copy of http.Transport object to prevent +// the value of http.DefaultTransport from being changed by goroutines. +func (roundTripper RetryingRoundTripper) getDefaultTransport() *http.Transport { + // The transport setup here follows the configurations of http.DefaultTransport + // but without Dialer since we will change it later. + transport := http.Transport{ + Proxy: http.ProxyFromEnvironment, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + } + + // Disables caching, Please refer to issue and specifically + // comment: https://github.com/fission/fission/issues/723#issuecomment-398781995 + transport.DisableKeepAlives = true + + return &transport +} + func (fh *functionHandler) tapService(serviceUrl *url.URL) { if fh.executor == nil { return