// Creates a new HTTP connection pool using the given address and pool parameters. // // 'addr' is a net.Dial()-style 'host:port' destination for making the TCP connection for // HTTP/HTTPS traffic. It will be used as the hostname by default for virtual hosting // and SSL certificate validation; if you'd like to use a different hostname, // set params.HostHeader. func NewSimplePool(addr string, params ConnectionParams) *SimplePool { pool := &SimplePool{ addr: addr, params: params, client: new(http.Client), } // setup HTTP transport transport := new(http.Transport) transport.ResponseHeaderTimeout = params.ResponseTimeout transport.MaxIdleConnsPerHost = params.MaxIdle transport.Proxy = http.ProxyFromEnvironment if params.Dial == nil { // dialTimeout could only be used in none proxy requests since it talks directly // to pool.addr if getenvEitherCase("HTTP_PROXY") == "" { transport.Dial = pool.dialTimeout } } else { transport.Dial = params.Dial } pool.transport = transport pool.client.Transport = transport if params.UseSSL && params.SkipVerifySSL { transport.TLSClientConfig = &tls.Config{ InsecureSkipVerify: true, } } return pool }
// New creates a new Firebase reference func New(url string) *Firebase { var tr *http.Transport tr = &http.Transport{ DisableKeepAlives: true, // https://code.google.com/p/go/issues/detail?id=3514 Dial: func(network, address string) (net.Conn, error) { start := time.Now() c, err := net.DialTimeout(network, address, TimeoutDuration) tr.ResponseHeaderTimeout = TimeoutDuration - time.Since(start) return c, err }, } var client *http.Client client = &http.Client{ Transport: tr, CheckRedirect: redirectPreserveHeaders, } return &Firebase{ url: sanitizeURL(url), params: _url.Values{}, client: client, stopWatching: make(chan struct{}), } }
// Creates a new HTTP connection pool using the given address and pool parameters. // // 'addr' is a net.Dial()-style 'host:port' destination for making the TCP connection for // HTTP/HTTPS traffic. It will be used as the hostname by default for virtual hosting // and SSL certificate validation; if you'd like to use a different hostname, // set params.HostHeader. func NewSimplePool(addr string, params ConnectionParams) *SimplePool { pool := &SimplePool{ addr: addr, params: params, client: new(http.Client), } // It's desirable to enforce the timeout at the client-level since it // includes the connection time, redirects and the time to finish reading // the full response. Unlike ResponseHeaderTimeout supported by // `http.Transport` which merely accounts for the timeout to receive the // first response header byte. It ignores the time to send the request or // the time to read the full response. pool.client.Timeout = params.ResponseTimeout // setup HTTP transport transport := new(http.Transport) transport.ResponseHeaderTimeout = params.ResponseTimeout transport.MaxIdleConnsPerHost = params.MaxIdle if params.Proxy != nil { transport.Proxy = params.Proxy } else { transport.Proxy = http.ProxyFromEnvironment } if params.Dial == nil { // dialTimeout could only be used in none proxy requests since it talks directly // to pool.addr if getenvEitherCase("HTTP_PROXY") == "" && params.Proxy == nil { transport.Dial = pool.dialTimeout } } else { transport.Dial = params.Dial } pool.transport = transport pool.client.Transport = transport if params.UseSSL && params.SkipVerifySSL { transport.TLSClientConfig = &tls.Config{ InsecureSkipVerify: true, } } return pool }
// New creates a new Firebase reference. Using a custom client invalidates the // TimeoutDuration guarantee. func New(url string, client *http.Client) *Firebase { if client == nil { var tr *http.Transport tr = &http.Transport{ Dial: func(network, address string) (net.Conn, error) { start := time.Now() c, err := net.DialTimeout(network, address, TimeoutDuration) tr.ResponseHeaderTimeout = TimeoutDuration - time.Since(start) return c, err }, } client = &http.Client{Transport: tr} } return &Firebase{ url: sanitizeURL(url), params: _url.Values{}, client: client, stopWatching: make(chan struct{}), } }
// New creates a new Firebase reference func New(url string) *Firebase { var tr *http.Transport tr = &http.Transport{ DisableKeepAlives: true, // https://code.google.com/p/go/issues/detail?id=3514 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, Dial: func(network, address string) (net.Conn, error) { start := time.Now() c, err := net.DialTimeout(network, address, TimeoutDuration) tr.ResponseHeaderTimeout = TimeoutDuration - time.Since(start) return c, err }, } return &Firebase{ url: sanitizeURL(url), params: _url.Values{}, client: &http.Client{Transport: tr}, stopWatching: make(chan struct{}), } }
func TestNewWithProvidedHttpClient(t *testing.T) { t.Parallel() var tr *http.Transport tr = &http.Transport{ DisableKeepAlives: true, // https://code.google.com/p/go/issues/detail?id=3514 Dial: func(network, address string) (net.Conn, error) { start := time.Now() c, err := net.DialTimeout(network, address, TimeoutDuration) tr.ResponseHeaderTimeout = TimeoutDuration - time.Since(start) return c, err }, } var client = http.Client{Transport: tr} testCases := []struct { givenURL string }{ { URL, }, { URL + "/", }, { "somefirebaseapp.firebaseIO.com", }, { "somefirebaseapp.firebaseIO.com/", }, } for _, tt := range testCases { fb := New(tt.givenURL, &client) assert.Equal(t, URL, fb.url, "givenURL: %s", tt.givenURL) } }
// Transport returns an http.RoundTripper with the correct timeouts func (ci *ConfigInfo) Transport() http.RoundTripper { noTransport.Do(func() { // Start with a sensible set of defaults then override. // This also means we get new stuff when it gets added to go t := new(http.Transport) setDefaults(t, http.DefaultTransport.(*http.Transport)) t.Proxy = http.ProxyFromEnvironment t.MaxIdleConnsPerHost = 4 * (ci.Checkers + ci.Transfers + 1) t.TLSHandshakeTimeout = ci.ConnectTimeout t.ResponseHeaderTimeout = ci.Timeout t.TLSClientConfig = &tls.Config{InsecureSkipVerify: ci.InsecureSkipVerify} t.DisableCompression = *noGzip // Set in http_old.go initTransport // t.Dial // Set in http_new.go initTransport // t.DialContext // t.IdelConnTimeout // t.ExpectContinueTimeout ci.initTransport(t) // Wrap that http.Transport in our own transport transport = NewTransport(t, ci.DumpHeaders, ci.DumpBodies) }) return transport }