-
Notifications
You must be signed in to change notification settings - Fork 38
/
option.go
161 lines (140 loc) · 3.56 KB
/
option.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
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"runtime"
"time"
"github.com/vektah/gqlparser/v2/gqlerror"
"go.uber.org/zap"
)
type Option func(http.RoundTripper) http.RoundTripper
func WithTokenGetter(getter func() (string, error)) Option {
return setHeaderFn("Authorization", func() (string, error) {
token, err := getter()
if err != nil {
return "", err
}
return "Bearer " + token, nil
})
}
func WithUserAgent(version string) Option {
return setHeaderFn("User-Agent", func() (string, error) {
return fmt.Sprintf("stateful-cli/%s (%s; %s)", version, runtime.GOOS, runtime.GOARCH), nil
})
}
func WithContentType(value string) Option {
return setHeaderFn("Content-Type", func() (string, error) {
return value, nil
})
}
type responseWriter struct {
*http.Response
buf *bytes.Buffer
}
var _ http.ResponseWriter = (*responseWriter)(nil)
func (w responseWriter) Header() http.Header { return w.Response.Header }
func (w *responseWriter) Write(data []byte) (int, error) {
if w.Response.StatusCode == 0 {
w.WriteHeader(http.StatusOK)
}
return w.buf.Write(data)
}
func (w *responseWriter) WriteHeader(statusCode int) {
w.Response.Status = http.StatusText(statusCode)
w.Response.StatusCode = statusCode
}
func WithChaosMonkey(srvErrRate, gqlErrRate float64) Option {
return func(rt http.RoundTripper) http.RoundTripper {
return funcTripper(func(r *http.Request) (*http.Response, error) {
rnd := rand.Float64() //#nosec
if rnd > srvErrRate+gqlErrRate {
return rt.RoundTrip(r)
}
buf := new(bytes.Buffer)
rw := &responseWriter{
Response: &http.Response{
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Body: io.NopCloser(buf),
},
buf: buf,
}
// GraphQL error
if rnd < gqlErrRate {
data := struct {
Data interface{} `json:"data"`
Errors gqlerror.List `json:"errors"`
}{
Errors: gqlerror.List{
&gqlerror.Error{
Message: "chaos gql error",
},
},
}
if err := json.NewEncoder(rw).Encode(&data); err != nil {
return nil, err
}
return rw.Response, nil
}
// Server error
http.Error(rw, "welcome from chaos", http.StatusNotFound)
return rw.Response, nil
})
}
}
func WithLogger(log *zap.Logger) Option {
return func(rt http.RoundTripper) http.RoundTripper {
return funcTripper(func(r *http.Request) (*http.Response, error) {
start := time.Now()
log.Debug(
"send an API request",
zap.String("path", r.URL.Path),
zap.String("method", r.Method),
zap.Duration("latency", time.Since(start)),
)
resp, err := rt.RoundTrip(r)
if resp != nil {
log.Debug(
"received an API response",
zap.Int("status", resp.StatusCode),
)
}
return resp, err
})
}
}
func NewHTTPClient(client *http.Client, opts ...Option) *http.Client {
if client == nil {
client = &http.Client{
Transport: http.DefaultTransport,
}
}
for _, o := range opts {
client.Transport = o(client.Transport)
}
return client
}
type funcTripper func(*http.Request) (*http.Response, error)
func (f funcTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func setHeaderFn(name string, valueGetter func() (string, error)) Option {
return func(rt http.RoundTripper) http.RoundTripper {
return funcTripper(func(r *http.Request) (*http.Response, error) {
value, err := valueGetter()
if err != nil {
return nil, err
}
if r.Header.Get(name) == "" {
r.Header.Set(name, value)
}
return rt.RoundTrip(r)
})
}
}