-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
268 lines (227 loc) · 7.59 KB
/
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package client
import (
"bytes"
"context"
"crypto/md5"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
discovery "github.com/Clever/discovery-go"
"github.com/Clever/wag/samples/gen-go-nils/models"
"github.com/afex/hystrix-go/hystrix"
logger "gopkg.in/Clever/kayvee-go.v6/logger"
)
var _ = json.Marshal
var _ = strings.Replace
var _ = strconv.FormatInt
var _ = bytes.Compare
// Version of the client.
const Version = "0.1.0"
// VersionHeader is sent with every request.
const VersionHeader = "X-Client-Version"
// WagClient is used to make requests to the nil-test service.
type WagClient struct {
basePath string
requestDoer doer
client *http.Client
timeout time.Duration
// Keep the retry doer around so that we can set the number of retries
retryDoer *retryDoer
// Keep the circuit doer around so that we can turn it on / off
circuitDoer *circuitBreakerDoer
defaultTimeout time.Duration
logger logger.KayveeLogger
}
var _ Client = (*WagClient)(nil)
// New creates a new client. The base path and http transport are configurable.
func New(basePath string) *WagClient {
basePath = strings.TrimSuffix(basePath, "/")
base := baseDoer{}
tracing := tracingDoer{d: base}
// For the short-term don't use the default retry policy since its 5 retries can 5X
// the traffic. Once we've enabled circuit breakers by default we can turn it on.
retry := retryDoer{d: tracing, retryPolicy: SingleRetryPolicy{}}
logger := logger.New("nil-test-wagclient")
circuit := &circuitBreakerDoer{
d: &retry,
debug: true,
// one circuit for each service + url pair
circuitName: fmt.Sprintf("nil-test-%s", shortHash(basePath)),
logger: logger,
}
circuit.init()
client := &WagClient{
basePath: basePath,
requestDoer: circuit,
client: &http.Client{Transport: http.DefaultTransport},
retryDoer: &retry,
circuitDoer: circuit,
defaultTimeout: 5 * time.Second,
logger: logger,
}
client.SetCircuitBreakerSettings(DefaultCircuitBreakerSettings)
return client
}
// NewFromDiscovery creates a client from the discovery environment variables. This method requires
// the three env vars: SERVICE_NIL_TEST_HTTP_(HOST/PORT/PROTO) to be set. Otherwise it returns an error.
func NewFromDiscovery() (*WagClient, error) {
url, err := discovery.URL("nil-test", "default")
if err != nil {
url, err = discovery.URL("nil-test", "http") // Added fallback to maintain reverse compatibility
if err != nil {
return nil, err
}
}
return New(url), nil
}
// SetRetryPolicy sets a the given retry policy for all requests.
func (c *WagClient) SetRetryPolicy(retryPolicy RetryPolicy) {
c.retryDoer.retryPolicy = retryPolicy
}
// SetCircuitBreakerDebug puts the circuit
func (c *WagClient) SetCircuitBreakerDebug(b bool) {
c.circuitDoer.debug = b
}
// SetLogger allows for setting a custom logger
func (c *WagClient) SetLogger(logger logger.KayveeLogger) {
c.logger = logger
c.circuitDoer.logger = logger
}
// CircuitBreakerSettings are the parameters that govern the client's circuit breaker.
type CircuitBreakerSettings struct {
// MaxConcurrentRequests is the maximum number of concurrent requests
// the client can make at the same time. Default: 100.
MaxConcurrentRequests int
// RequestVolumeThreshold is the minimum number of requests needed
// before a circuit can be tripped due to health. Default: 20.
RequestVolumeThreshold int
// SleepWindow how long, in milliseconds, to wait after a circuit opens
// before testing for recovery. Default: 5000.
SleepWindow int
// ErrorPercentThreshold is the threshold to place on the rolling error
// rate. Once the error rate exceeds this percentage, the circuit opens.
// Default: 90.
ErrorPercentThreshold int
}
// DefaultCircuitBreakerSettings describes the default circuit parameters.
var DefaultCircuitBreakerSettings = CircuitBreakerSettings{
MaxConcurrentRequests: 100,
RequestVolumeThreshold: 20,
SleepWindow: 5000,
ErrorPercentThreshold: 90,
}
// SetCircuitBreakerSettings sets parameters on the circuit breaker. It must be
// called on application startup.
func (c *WagClient) SetCircuitBreakerSettings(settings CircuitBreakerSettings) {
hystrix.ConfigureCommand(c.circuitDoer.circuitName, hystrix.CommandConfig{
// redundant, with the timeout we set on the context, so set
// this to something high and irrelevant
Timeout: 100 * 1000,
MaxConcurrentRequests: settings.MaxConcurrentRequests,
RequestVolumeThreshold: settings.RequestVolumeThreshold,
SleepWindow: settings.SleepWindow,
ErrorPercentThreshold: settings.ErrorPercentThreshold,
})
}
// SetTimeout sets a timeout on all operations for the client. To make a single request
// with a timeout use context.WithTimeout as described here: https://godoc.org/golang.org/x/net/context#WithTimeout.
func (c *WagClient) SetTimeout(timeout time.Duration) {
c.defaultTimeout = timeout
}
// SetTransport sets the http transport used by the client.
func (c *WagClient) SetTransport(t http.RoundTripper) {
c.client.Transport = t
}
// NilCheck makes a POST request to /check/{id}
// Nil check tests
// 200: nil
// 400: *models.BadRequest
// 500: *models.InternalError
// default: client side HTTP errors, for example: context.DeadlineExceeded.
func (c *WagClient) NilCheck(ctx context.Context, i *models.NilCheckInput) error {
headers := make(map[string]string)
var body []byte
path, err := i.Path()
if err != nil {
return err
}
path = c.basePath + path
headers["header"] = i.Header
if i.Body != nil {
var err error
body, err = json.Marshal(i.Body)
if err != nil {
return err
}
}
req, err := http.NewRequestWithContext(ctx, "POST", path, bytes.NewBuffer(body))
if err != nil {
return err
}
return c.doNilCheckRequest(ctx, req, headers)
}
func (c *WagClient) doNilCheckRequest(ctx context.Context, req *http.Request, headers map[string]string) error {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Canonical-Resource", "nilCheck")
req.Header.Set(VersionHeader, Version)
for field, value := range headers {
req.Header.Set(field, value)
}
// Add the opname for doers like tracing
ctx = context.WithValue(ctx, opNameCtx{}, "nilCheck")
req = req.WithContext(ctx)
// Don't add the timeout in a "doer" because we don't want to call "defer.cancel()"
// until we've finished all the processing of the request object. Otherwise we'll cancel
// our own request before we've finished it.
if c.defaultTimeout != 0 {
ctx, cancel := context.WithTimeout(req.Context(), c.defaultTimeout)
defer cancel()
req = req.WithContext(ctx)
}
resp, err := c.requestDoer.Do(c.client, req)
retCode := 0
if resp != nil {
retCode = resp.StatusCode
}
// log all client failures and non-successful HT
logData := logger.M{
"backend": "nil-test",
"method": req.Method,
"uri": req.URL,
"status_code": retCode,
}
if err == nil && retCode > 399 {
logData["message"] = resp.Status
c.logger.ErrorD("client-request-finished", logData)
}
if err != nil {
logData["message"] = err.Error()
c.logger.ErrorD("client-request-finished", logData)
return err
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
return nil
case 400:
var output models.BadRequest
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return err
}
return &output
case 500:
var output models.InternalError
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return err
}
return &output
default:
return &models.InternalError{Message: "Unknown response"}
}
}
func shortHash(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))[0:6]
}