2014-05-22 14:37:19 -04:00
|
|
|
package pkg
|
2014-05-21 13:13:20 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
neturl "net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
HTTP_2xx = 2
|
|
|
|
HTTP_4xx = 4
|
|
|
|
)
|
|
|
|
|
2014-06-23 11:32:31 -07:00
|
|
|
type Err error
|
|
|
|
|
2014-06-26 14:58:32 -07:00
|
|
|
type ErrTimeout struct {
|
2014-06-23 11:32:31 -07:00
|
|
|
Err
|
|
|
|
}
|
|
|
|
|
2014-06-26 14:58:32 -07:00
|
|
|
type ErrNotFound struct {
|
2014-06-23 11:32:31 -07:00
|
|
|
Err
|
|
|
|
}
|
|
|
|
|
2014-06-26 14:58:32 -07:00
|
|
|
type ErrInvalid struct {
|
|
|
|
Err
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrServer struct {
|
|
|
|
Err
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrNetwork struct {
|
2014-06-23 11:32:31 -07:00
|
|
|
Err
|
|
|
|
}
|
|
|
|
|
2014-05-21 13:13:20 -04:00
|
|
|
type HttpClient struct {
|
|
|
|
// Maximum exp backoff duration. Defaults to 5 seconds
|
|
|
|
MaxBackoff time.Duration
|
|
|
|
|
2014-05-22 14:37:19 -04:00
|
|
|
// Maximum number of connection retries. Defaults to 15
|
2014-05-21 13:13:20 -04:00
|
|
|
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
|
|
|
|
|
2014-05-22 14:37:19 -04:00
|
|
|
// Whether or not to skip TLS verification. Defaults to false
|
2014-05-21 13:13:20 -04:00
|
|
|
SkipTLS bool
|
2014-06-26 14:58:32 -07:00
|
|
|
|
|
|
|
client *http.Client
|
2014-05-21 13:13:20 -04:00
|
|
|
}
|
|
|
|
|
2014-07-30 13:56:36 -07:00
|
|
|
type Getter interface {
|
2014-07-30 14:23:10 -07:00
|
|
|
Get(string) ([]byte, error)
|
2014-07-30 13:56:36 -07:00
|
|
|
GetRetry(string) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2014-05-21 13:13:20 -04:00
|
|
|
func NewHttpClient() *HttpClient {
|
2014-06-26 14:58:32 -07:00
|
|
|
hc := &HttpClient{
|
2014-05-21 13:13:20 -04:00
|
|
|
MaxBackoff: time.Second * 5,
|
|
|
|
MaxRetries: 15,
|
|
|
|
Timeout: time.Duration(2) * time.Second,
|
|
|
|
SkipTLS: false,
|
|
|
|
}
|
2014-06-26 14:58:32 -07:00
|
|
|
|
|
|
|
// 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
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return hc
|
2014-05-21 13:13:20 -04:00
|
|
|
}
|
|
|
|
|
2014-06-26 15:17:53 -07:00
|
|
|
func ExpBackoff(interval, max time.Duration) time.Duration {
|
2014-05-29 11:03:15 -07:00
|
|
|
interval = interval * 2
|
|
|
|
if interval > max {
|
|
|
|
interval = max
|
|
|
|
}
|
|
|
|
return interval
|
|
|
|
}
|
|
|
|
|
2014-06-26 14:58:32 -07:00
|
|
|
// GetRetry fetches a given URL with support for exponential backoff and maximum retries
|
|
|
|
func (h *HttpClient) GetRetry(rawurl string) ([]byte, error) {
|
2014-05-21 13:13:20 -04:00
|
|
|
if rawurl == "" {
|
2014-06-23 11:32:31 -07:00
|
|
|
return nil, ErrInvalid{errors.New("URL is empty. Skipping.")}
|
2014-05-21 13:13:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
url, err := neturl.Parse(rawurl)
|
|
|
|
if err != nil {
|
2014-06-23 11:32:31 -07:00
|
|
|
return nil, ErrInvalid{err}
|
2014-05-21 13:13:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unfortunately, url.Parse is too generic to throw errors if a URL does not
|
|
|
|
// have a valid HTTP scheme. So, we have to do this extra validation
|
|
|
|
if !strings.HasPrefix(url.Scheme, "http") {
|
2014-06-23 11:32:31 -07:00
|
|
|
return nil, ErrInvalid{fmt.Errorf("URL %s does not have a valid HTTP scheme. Skipping.", rawurl)}
|
2014-05-21 13:13:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dataURL := url.String()
|
|
|
|
|
2014-05-29 11:03:15 -07:00
|
|
|
duration := 50 * time.Millisecond
|
2014-05-21 13:13:20 -04:00
|
|
|
for retry := 1; retry <= h.MaxRetries; retry++ {
|
|
|
|
log.Printf("Fetching data from %s. Attempt #%d", dataURL, retry)
|
|
|
|
|
2014-06-26 14:58:32 -07:00
|
|
|
data, err := h.Get(dataURL)
|
|
|
|
switch err.(type) {
|
|
|
|
case ErrNetwork:
|
|
|
|
log.Printf(err.Error())
|
|
|
|
case ErrServer:
|
|
|
|
log.Printf(err.Error())
|
|
|
|
case ErrNotFound:
|
|
|
|
return data, err
|
|
|
|
default:
|
|
|
|
return data, err
|
2014-05-21 13:13:20 -04:00
|
|
|
}
|
|
|
|
|
2014-06-26 15:17:53 -07:00
|
|
|
duration = ExpBackoff(duration, h.MaxBackoff)
|
2014-05-22 14:53:54 -04:00
|
|
|
log.Printf("Sleeping for %v...", duration)
|
2014-05-21 13:13:20 -04:00
|
|
|
time.Sleep(duration)
|
|
|
|
}
|
|
|
|
|
2014-06-23 11:32:31 -07:00
|
|
|
return nil, ErrTimeout{fmt.Errorf("Unable to fetch data. Maximum retries reached: %d", h.MaxRetries)}
|
2014-05-21 13:13:20 -04:00
|
|
|
}
|
2014-06-26 14:58:32 -07:00
|
|
|
|
|
|
|
func (h *HttpClient) Get(dataURL string) ([]byte, error) {
|
|
|
|
if resp, err := h.client.Get(dataURL); err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
switch resp.StatusCode / 100 {
|
|
|
|
case HTTP_2xx:
|
|
|
|
return ioutil.ReadAll(resp.Body)
|
|
|
|
case HTTP_4xx:
|
|
|
|
return nil, ErrNotFound{fmt.Errorf("Not found. HTTP status code: %d", resp.StatusCode)}
|
|
|
|
default:
|
|
|
|
return nil, ErrServer{fmt.Errorf("Server error. HTTP status code: %d", resp.StatusCode)}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, ErrNetwork{fmt.Errorf("Unable to fetch data: %s", err.Error())}
|
|
|
|
}
|
|
|
|
}
|