getClient returns a cached HTTP client for the given configuration, creating one if needed. This enables connection pooling across requests.
(proxy *url.URL, timeout int, redirect bool)
| 34 | // getClient returns a cached HTTP client for the given configuration, |
| 35 | // creating one if needed. This enables connection pooling across requests. |
| 36 | func getClient(proxy *url.URL, timeout int, redirect bool) *http.Client { |
| 37 | proxyStr := "" |
| 38 | if proxy != nil { |
| 39 | proxyStr = proxy.String() |
| 40 | } |
| 41 | key := clientCacheKey{proxyStr, timeout, redirect} |
| 42 | |
| 43 | if v, ok := clientCache.Load(key); ok { |
| 44 | return v.(*http.Client) |
| 45 | } |
| 46 | |
| 47 | timeoutDuration := time.Duration(timeout) * time.Millisecond |
| 48 | transport := &http.Transport{ |
| 49 | Proxy: http.ProxyURL(proxy), |
| 50 | TLSClientConfig: &tls.Config{ |
| 51 | InsecureSkipVerify: true, |
| 52 | }, |
| 53 | DialContext: (&net.Dialer{ |
| 54 | Timeout: timeoutDuration, |
| 55 | KeepAlive: 30 * time.Second, |
| 56 | }).DialContext, |
| 57 | MaxIdleConns: 100, |
| 58 | MaxIdleConnsPerHost: 10, |
| 59 | IdleConnTimeout: 90 * time.Second, |
| 60 | TLSHandshakeTimeout: timeoutDuration, |
| 61 | ResponseHeaderTimeout: timeoutDuration, |
| 62 | ExpectContinueTimeout: 1 * time.Second, |
| 63 | } |
| 64 | |
| 65 | client := &http.Client{ |
| 66 | Transport: transport, |
| 67 | Timeout: timeoutDuration, |
| 68 | } |
| 69 | |
| 70 | if !redirect { |
| 71 | client.CheckRedirect = func(req *http.Request, via []*http.Request) error { |
| 72 | return http.ErrUseLastResponse |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | clientCache.Store(key, client) |
| 77 | return client |
| 78 | } |
| 79 | |
| 80 | // parseFile reads a file given its filename and returns a list containing each of its lines. |
| 81 | func parseFile(filename string) ([]string, error) { |