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)
| 57 | // and SSL certificate validation; if you'd like to use a different hostname, |
| 58 | // set params.HostHeader. |
| 59 | func 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. |