forked from gojek/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhystrix_client.go
151 lines (116 loc) · 3.83 KB
/
hystrix_client.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package heimdall
import (
"fmt"
"io"
"net/http"
"time"
"github.com/afex/hystrix-go/hystrix"
"github.com/pkg/errors"
)
const defaultHystrixRetryCount int = 0
type hystrixHTTPClient struct {
client *http.Client
hystrixCommandName string
retryCount int
retrier Retriable
}
// NewHystrixHTTPClient returns a new instance of HystrixHTTPClient
func NewHystrixHTTPClient(timeoutInMillis int, hystrixConfig HystrixConfig) Client {
httpTimeout := time.Duration(timeoutInMillis) * time.Millisecond
httpClient := &http.Client{
Timeout: httpTimeout,
}
hystrix.ConfigureCommand(hystrixConfig.commandName, hystrixConfig.commandConfig)
return &hystrixHTTPClient{
client: httpClient,
retryCount: defaultHystrixRetryCount,
retrier: NewNoRetrier(),
hystrixCommandName: hystrixConfig.commandName,
}
}
// SetRetryCount sets the retry count for the hystrixHTTPClient
func (hhc *hystrixHTTPClient) SetRetryCount(count int) {
hhc.retryCount = count
}
// SetRetrier sets the strategy for retrying
func (hhc *hystrixHTTPClient) SetRetrier(retrier Retriable) {
hhc.retrier = retrier
}
// Get makes a HTTP GET request to provided URL
func (hhc *hystrixHTTPClient) Get(url string, headers http.Header) (*http.Response, error) {
var response *http.Response
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return response, errors.Wrap(err, "GET - request creation failed")
}
request.Header = headers
return hhc.Do(request)
}
// Post makes a HTTP POST request to provided URL and requestBody
func (hhc *hystrixHTTPClient) Post(url string, body io.Reader, headers http.Header) (*http.Response, error) {
var response *http.Response
request, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return response, errors.Wrap(err, "POST - request creation failed")
}
request.Header = headers
return hhc.Do(request)
}
// Put makes a HTTP PUT request to provided URL and requestBody
func (hhc *hystrixHTTPClient) Put(url string, body io.Reader, headers http.Header) (*http.Response, error) {
var response *http.Response
request, err := http.NewRequest(http.MethodPut, url, body)
if err != nil {
return response, errors.Wrap(err, "PUT - request creation failed")
}
request.Header = headers
return hhc.Do(request)
}
// Patch makes a HTTP PATCH request to provided URL and requestBody
func (hhc *hystrixHTTPClient) Patch(url string, body io.Reader, headers http.Header) (*http.Response, error) {
var response *http.Response
request, err := http.NewRequest(http.MethodPatch, url, body)
if err != nil {
return response, errors.Wrap(err, "PATCH - request creation failed")
}
request.Header = headers
return hhc.Do(request)
}
// Delete makes a HTTP DELETE request with provided URL
func (hhc *hystrixHTTPClient) Delete(url string, headers http.Header) (*http.Response, error) {
var response *http.Response
request, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return response, errors.Wrap(err, "DELETE - request creation failed")
}
request.Header = headers
return hhc.Do(request)
}
// Do makes an HTTP request with the native `http.Do` interface
func (hhc *hystrixHTTPClient) Do(request *http.Request) (*http.Response, error) {
request.Close = true
var response *http.Response
var err error
for i := 0; i <= hhc.retryCount; i++ {
err = hystrix.Do(hhc.hystrixCommandName, func() error {
response, err = hhc.client.Do(request)
if err != nil {
return err
}
if response.StatusCode >= http.StatusInternalServerError {
return fmt.Errorf("Server is down: returned status code: %d", response.StatusCode)
}
return nil
}, func(err error) error {
//TODO: make this fallback configurable in client
return err
})
if err != nil {
backoffTime := hhc.retrier.NextInterval(i)
time.Sleep(backoffTime)
continue
}
break
}
return response, err
}