MCPcopy
hub / github.com/dropbox/godropbox / NewSimplePool

Function NewSimplePool

net2/http2/simple_pool.go:59–133  ·  view source on GitHub ↗

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

(addr string, params ConnectionParams)

Source from the content-addressed store, hash-verified

57// and SSL certificate validation; if you'd like to use a different hostname,
58// set params.HostHeader.
59func NewSimplePool(addr string, params ConnectionParams) *SimplePool {
60 statsFactory := stats.NoOpStatsFactory
61 if params.StatsFactory != nil {
62 statsFactory = params.StatsFactory
63 }
64
65 tags := map[string]string{}
66 pool := &SimplePool{
67 addr: addr,
68 params: params,
69 client: new(http.Client),
70 closeWait: 5 * time.Minute,
71 connAcquireMsSummary: statsFactory.NewSummary("pool_conn_acquire_ms", tags),
72 acquiredConnsGauge: statsFactory.NewGauge("pool_acquired_conns", tags),
73 }
74
75 if params.MaxConns > 0 && params.ConnectionAcquireTimeout > 0 {
76 pool.connsLimiter = sync2.NewBoundedSemaphore(uint(params.MaxConns))
77 }
78
79 // It's desirable to enforce the timeout at the client-level since it
80 // includes the connection time, redirects and the time to finish reading
81 // the full response. Unlike ResponseHeaderTimeout supported by
82 // `http.Transport` which merely accounts for the timeout to receive the
83 // first response header byte. It ignores the time to send the request or
84 // the time to read the full response.
85 pool.client.Timeout = params.ResponseTimeout
86
87 // setup HTTP transport
88 transport := new(http.Transport)
89 transport.ResponseHeaderTimeout = params.ResponseTimeout
90 transport.MaxIdleConnsPerHost = params.MaxIdle
91
92 if params.Proxy != nil {
93 transport.Proxy = params.Proxy
94 } else {
95 transport.Proxy = http.ProxyFromEnvironment
96 }
97
98 dial := net.Dial
99 if params.Dial == nil {
100 // dialTimeout could only be used in none proxy requests since it talks directly
101 // to pool.addr
102 if getenvEitherCase("HTTP_PROXY") == "" && params.Proxy == nil {
103 dial = pool.dialTimeout
104 }
105 } else {
106 dial = params.Dial
107 }
108
109 pool.conns = NewConnectionTracker(params.MaxConns, dial, statsFactory)
110 transport.Dial = pool.conns.Dial
111
112 if params.UseSSL {
113 transport.TLSClientConfig = params.TLSClientConfig
114
115 // Silently ignore error for now, but probably need to change api
116 // to return error.

Callers 13

TestHTTPMethod · 0.85
TestConnectTimeoutMethod · 0.85
TestResponseTimeoutMethod · 0.85
TestConnectionRefusedMethod · 0.85
TestMaxConnMethod · 0.85
TestCloseMethod · 0.85
TestRedirectMethod · 0.85
TestHttp2Method · 0.85
TestHttp2vsHttp1Method · 0.85

Calls 5

NewBoundedSemaphoreFunction · 0.92
getenvEitherCaseFunction · 0.85
NewConnectionTrackerFunction · 0.85
NewSummaryMethod · 0.65
NewGaugeMethod · 0.65

Tested by 12

TestHTTPMethod · 0.68
TestConnectTimeoutMethod · 0.68
TestResponseTimeoutMethod · 0.68
TestConnectionRefusedMethod · 0.68
TestMaxConnMethod · 0.68
TestCloseMethod · 0.68
TestRedirectMethod · 0.68
TestHttp2Method · 0.68
TestHttp2vsHttp1Method · 0.68