-
Notifications
You must be signed in to change notification settings - Fork 0
/
hystrix.go
169 lines (138 loc) · 4.18 KB
/
hystrix.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package boomerang
import (
"fmt"
"github.com/afex/hystrix-go/hystrix"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
const (
defaultMinTimeout = 10 * time.Millisecond
defaultMaxTimeout = 20 * time.Millisecond
defaultFactor = 2
DefaultMaxHystrixRetries = 1
)
type fallbackFunc func(error) error
type HystrixCommandConfig struct {
Timeout int `json:"timeout"`
MaxConcurrentRequests int `json:"max_concurrent_requests"`
RequestVolumeThreshold int `json:"request_volume_threshold"`
SleepWindow int `json:"sleep_window"`
ErrorPercentThreshold int `json:"error_percent_threshold"`
CommandName string
Transport *http.Transport
}
func NewHystrixClient(timeout time.Duration, hc HystrixCommandConfig) *HystrixClient {
httpClient := &http.Client{
Timeout: timeout,
Transport: hc.Transport,
}
hysCmdConfig := hystrix.CommandConfig{
Timeout: hc.Timeout,
MaxConcurrentRequests: hc.MaxConcurrentRequests,
RequestVolumeThreshold: hc.RequestVolumeThreshold,
SleepWindow: hc.SleepWindow,
ErrorPercentThreshold: hc.ErrorPercentThreshold,
}
hystrix.ConfigureCommand(hc.CommandName, hysCmdConfig)
return &HystrixClient{
client: httpClient,
MaxRetries: DefaultMaxHystrixRetries,
commandName: hc.CommandName,
Backoff: NewConstantBackoff(
defaultMinTimeout,
),
}
}
type HystrixClient struct {
commandName string
client *http.Client
Logger *log.Logger // Customer logger instance.
Backoff Backoff
// CheckRetry specifies the policy for handling retries, and is called
// after each request. The default policy is DefaultRetryPolicy.
CheckRetry CheckRetry
MaxRetries int
fallbackFunc func(err error) error
}
func (c *HystrixClient) SetFallbackFunc(fbf func(err error) error) {
c.fallbackFunc = fbf
}
func (c *HystrixClient) Head(url string) (*http.Response, error) {
req, err := NewRequest("HEAD", url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
func (c *HystrixClient) Get(url string) (*http.Response, error) {
req, err := NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
func (c *HystrixClient) Post(url string, contentType string, body io.ReadSeeker) (*http.Response, error) {
req, err := NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return c.Do(req)
}
func (c *HystrixClient) PostForm(url string, data url.Values) (*http.Response, error) {
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}
func (c *HystrixClient) Do(req *http.Request) (*http.Response, error) {
var resp *http.Response
var err error
for i := 0; i < c.MaxRetries; i++ {
err = hystrix.Do(c.commandName, func() error {
resp, err = c.client.Do(req)
if err != nil {
c.Logger.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL, err)
}
// Check if we should continue with retries.
checkOK, checkErr := c.CheckRetry(resp, err)
if !checkOK {
if checkErr != nil {
err = checkErr
}
return err
}
// if response.StatusCode >= http.StatusInternalServerError {
// return fmt.Errorf("Server is down: returned status code: %d", response.StatusCode)
// }
return nil
}, c.fallbackFunc)
if err != nil {
waitTime := c.Backoff.NextInterval(i)
desc := fmt.Sprintf("%s %s", req.Method, req.URL)
// desc = fmt.Sprintf("%s (status: %d)", desc, code)
c.Logger.Printf("[DEBUG] %s: retrying in %s (%d left)", desc, waitTime, i)
time.Sleep(waitTime)
continue
}
// We're going to retry, consume any response to reuse the connection.
if err == nil {
c.drainBody(resp.Body)
return resp, err
}
break
}
// Return an error if we fall out of the retry loop
return nil, fmt.Errorf("%s %s giving up after %d attempts",
req.Method, req.URL, c.MaxRetries+1)
}
// Try to read the response body so we can reuse this connection.
func (c *HystrixClient) drainBody(body io.ReadCloser) {
defer body.Close()
_, err := io.Copy(ioutil.Discard, io.LimitReader(body, respReadLimit))
if err != nil {
c.Logger.Printf("[ERR] error reading response body: %v", err)
}
}