From b3b09aeb192568141dc1dc959f016090d8da1425 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Fri, 7 Aug 2015 19:23:56 -0700 Subject: [PATCH] pkg: update HttpClient to use newer Go features --- pkg/http_client.go | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/pkg/http_client.go b/pkg/http_client.go index 5d275ec..a5b832d 100644 --- a/pkg/http_client.go +++ b/pkg/http_client.go @@ -15,12 +15,10 @@ package pkg import ( - "crypto/tls" "errors" "fmt" "io/ioutil" "log" - "net" "net/http" neturl "net/url" "strings" @@ -55,16 +53,15 @@ type ErrNetwork struct { } type HttpClient struct { + // Initial backoff duration. Defaults to 50 milliseconds + InitialBackoff time.Duration + // Maximum exp backoff duration. Defaults to 5 seconds MaxBackoff time.Duration // Maximum number of connection retries. Defaults to 15 MaxRetries int - // HTTP client timeout, this is suggested to be low since exponential - // backoff will kick off too. Defaults to 2 seconds - Timeout time.Duration - // Whether or not to skip TLS verification. Defaults to false SkipTLS bool @@ -78,29 +75,12 @@ type Getter interface { func NewHttpClient() *HttpClient { hc := &HttpClient{ - MaxBackoff: time.Second * 5, - MaxRetries: 15, - Timeout: time.Duration(2) * time.Second, - SkipTLS: false, - } - - // We need to create our own client in order to add timeout support. - // TODO(c4milo) Replace it once Go 1.3 is officially used by CoreOS - // More info: https://code.google.com/p/go/source/detail?r=ada6f2d5f99f - hc.client = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: hc.SkipTLS, - }, - Dial: func(network, addr string) (net.Conn, error) { - deadline := time.Now().Add(hc.Timeout) - c, err := net.DialTimeout(network, addr, hc.Timeout) - if err != nil { - return nil, err - } - c.SetDeadline(deadline) - return c, nil - }, + InitialBackoff: 50 * time.Millisecond, + MaxBackoff: time.Second * 5, + MaxRetries: 15, + SkipTLS: false, + client: &http.Client{ + Timeout: time.Duration(2) * time.Second, }, } @@ -134,7 +114,7 @@ func (h *HttpClient) GetRetry(rawurl string) ([]byte, error) { dataURL := url.String() - duration := 50 * time.Millisecond + duration := h.InitialBackoff for retry := 1; retry <= h.MaxRetries; retry++ { log.Printf("Fetching data from %s. Attempt #%d", dataURL, retry)