Making http client global to async runner

This commit is contained in:
Denis Makogon
2017-07-31 21:22:12 +03:00
parent 7c22468b81
commit 333fc66906

View File

@@ -23,26 +23,27 @@ import (
"github.com/opentracing/opentracing-go"
)
var client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 120 * time.Second,
}).Dial,
MaxIdleConnsPerHost: 512,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{
ClientSessionCache: tls.NewLRUClientSessionCache(4096),
},
},
}
func getTask(ctx context.Context, url string) (*models.Task, error) {
// TODO shove this ctx into the request?
span, _ := opentracing.StartSpanFromContext(ctx, "get_task")
defer span.Finish()
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 120 * time.Second,
}).Dial,
MaxIdleConnsPerHost: 512,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{
ClientSessionCache: tls.NewLRUClientSessionCache(4096),
},
},
}
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return nil, err
@@ -105,20 +106,6 @@ func deleteTask(ctx context.Context, url string, task *models.Task) error {
if err != nil {
return err
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 120 * time.Second,
}).Dial,
MaxIdleConnsPerHost: 512,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{
ClientSessionCache: tls.NewLRUClientSessionCache(4096),
},
},
}
if resp, err := client.Do(req); err != nil {
return err