-
Notifications
You must be signed in to change notification settings - Fork 24
/
http.go
72 lines (59 loc) · 1.96 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package clients
import (
"crypto/tls"
"fmt"
"net/http"
"time"
"github.com/hashicorp/go-hclog"
)
// HTTP defines an interface for a HTTP client
type HTTP interface {
// HealthCheckHTTP makes a HTTP GET request to the given URI and
// if a successful status []codes is returned the method returns a nil error.
// If it is not possible to contact the URI or if any status other than the passed codes is returned
// by the upstream, then the URI is retried until the timeout elapses.
HealthCheckHTTP(uri string, codes []int, timeout time.Duration) error
// Do executes a HTTP request and returns the response
Do(r *http.Request) (*http.Response, error)
}
type HTTPImpl struct {
backoff time.Duration
httpc *http.Client
l hclog.Logger
}
func NewHTTP(backoff time.Duration, l hclog.Logger) HTTP {
httpc := &http.Client{}
httpc.Transport = http.DefaultTransport.(*http.Transport).Clone()
httpc.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
return &HTTPImpl{backoff, httpc, l}
}
// HealthCheckHTTP checks a http or HTTPS endpoint for a status 200
func (h *HTTPImpl) HealthCheckHTTP(address string, codes []int, timeout time.Duration) error {
h.l.Debug("Performing health check for address", "address", address)
st := time.Now()
for {
if time.Now().Sub(st) > timeout {
h.l.Error("Timeout wating for HTTP healthcheck", "address", address)
return fmt.Errorf("Timeout waiting for HTTP healthcheck %s", address)
}
resp, err := h.httpc.Get(address)
if err == nil && assertResponseCode(codes, resp.StatusCode) {
h.l.Debug("Health check complete", "address", address)
return nil
}
// backoff
time.Sleep(h.backoff)
}
}
func assertResponseCode(codes []int, responseCode int) bool {
for _, c := range codes {
if responseCode == c {
return true
}
}
return false
}
// Do executes a HTTP request and returns the response
func (h *HTTPImpl) Do(r *http.Request) (*http.Response, error) {
return h.httpc.Do(r)
}