pkg: update HttpClient to use newer Go features

This commit is contained in:
Alex Crawford 2015-08-07 19:23:56 -07:00
parent 481d98c0b5
commit b3b09aeb19

View File

@ -15,12 +15,10 @@
package pkg package pkg
import ( import (
"crypto/tls"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"net/http" "net/http"
neturl "net/url" neturl "net/url"
"strings" "strings"
@ -55,16 +53,15 @@ type ErrNetwork struct {
} }
type HttpClient struct { type HttpClient struct {
// Initial backoff duration. Defaults to 50 milliseconds
InitialBackoff time.Duration
// Maximum exp backoff duration. Defaults to 5 seconds // Maximum exp backoff duration. Defaults to 5 seconds
MaxBackoff time.Duration MaxBackoff time.Duration
// Maximum number of connection retries. Defaults to 15 // Maximum number of connection retries. Defaults to 15
MaxRetries int 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 // Whether or not to skip TLS verification. Defaults to false
SkipTLS bool SkipTLS bool
@ -78,29 +75,12 @@ type Getter interface {
func NewHttpClient() *HttpClient { func NewHttpClient() *HttpClient {
hc := &HttpClient{ hc := &HttpClient{
MaxBackoff: time.Second * 5, InitialBackoff: 50 * time.Millisecond,
MaxRetries: 15, MaxBackoff: time.Second * 5,
Timeout: time.Duration(2) * time.Second, MaxRetries: 15,
SkipTLS: false, SkipTLS: false,
} client: &http.Client{
Timeout: time.Duration(2) * time.Second,
// 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
},
}, },
} }
@ -134,7 +114,7 @@ func (h *HttpClient) GetRetry(rawurl string) ([]byte, error) {
dataURL := url.String() dataURL := url.String()
duration := 50 * time.Millisecond duration := h.InitialBackoff
for retry := 1; retry <= h.MaxRetries; retry++ { for retry := 1; retry <= h.MaxRetries; retry++ {
log.Printf("Fetching data from %s. Attempt #%d", dataURL, retry) log.Printf("Fetching data from %s. Attempt #%d", dataURL, retry)