-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
152 lines (136 loc) · 4.95 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
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-deprecated/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
// WagClient is used to make requests to the swagger-test service.
type WagClient struct {
basePath string
requestDoer doer
transport http.RoundTripper
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("swagger-test-wagclient")
circuit := &circuitBreakerDoer{
d: &retry,
debug: true,
// one circuit for each service + url pair
circuitName: fmt.Sprintf("swagger-test-%s", shortHash(basePath)),
logger: logger,
}
circuit.init()
client := &WagClient{
requestDoer: circuit,
retryDoer: &retry,
circuitDoer: circuit,
defaultTimeout: 5 * time.Second,
transport: &http.Transport{},
basePath: basePath,
logger: logger,
}
client.SetCircuitBreakerSettings(DefaultCircuitBreakerSettings)
return client
}
// NewFromDiscovery creates a client from the discovery environment variables. This method requires
// the three env vars: SERVICE_SWAGGER_TEST_HTTP_(HOST/PORT/PROTO) to be set. Otherwise it returns an error.
func NewFromDiscovery() (*WagClient, error) {
url, err := discovery.URL("swagger-test", "default")
if err != nil {
url, err = discovery.URL("swagger-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.transport = t
}
func shortHash(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))[0:6]
}