/
http.go
258 lines (229 loc) · 5.73 KB
/
http.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
package cloud
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type request struct {
hasBody bool
body []byte
headers http.Header
hasAuth bool
hasHeaders bool
}
type requestOpt func(*request) error
func withAuth() requestOpt {
return func(r *request) error {
r.hasAuth = true
return nil
}
}
func withHeader(key, value string) requestOpt {
return func(r *request) error {
r.hasHeaders = true
r.headers = http.Header{}
r.headers.Add(key, value)
return nil
}
}
func withJSONBody(body proto.Message) requestOpt {
return func(r *request) error {
encodedBody, err := protojson.Marshal(body)
if err != nil {
return err
}
r.hasBody = true
r.body = encodedBody
return nil
}
}
func withFileBody(pathOnDisk string) requestOpt {
return func(r *request) error {
_, err := os.Stat(pathOnDisk)
if err != nil {
return errors.Wrapf(err, "could not stat file at %s", pathOnDisk)
}
contents, err := os.ReadFile(pathOnDisk)
if err != nil {
return errors.Wrapf(err, "could not add file %s to request body", pathOnDisk)
}
r.hasBody = true
r.body = contents
return nil
}
}
func withBody(body []byte) requestOpt {
return func(r *request) error {
r.hasBody = true
r.body = append([]byte{}, body...)
return nil
}
}
func (c *Client) doCall(ctx context.Context, method, url string, opts ...requestOpt) (int, []byte, error) {
const maxAttempt = 10
const maxSleepBeforeRetry = time.Second * 3
var r request
for _, opt := range opts {
err := opt(&r)
if err != nil {
return 0, nil, err
}
}
alreadyReAuthed := false
if r.hasAuth && time.Now().UTC().After(c.authTokenExpiry) {
if _, err := c.Authenticate(ctx); err != nil {
if errors.Is(err, ErrUnauthorized) {
return 0, nil, ErrUnauthorized
}
if errors.Is(err, ErrAuthTokenExpired) {
return 0, nil, ErrAuthTokenExpired
}
return 0, nil, errors.Wrap(err, "failed refreshing expired auth token")
}
alreadyReAuthed = true
}
var status int
body := []byte{}
var callErr error
duration := time.Millisecond * 100
reqID := c.getRequestID()
for attempt := 0; attempt < maxAttempt; attempt++ {
status, body, callErr = c.doCallImp(ctx, r, method, url, reqID, opts...)
retry, err := shouldRetry(status, body, callErr, c.debugFunc, reqID)
if err != nil {
return status, body, err
}
if !retry {
return status, body, nil
}
if status == http.StatusUnauthorized {
if !r.hasAuth || alreadyReAuthed {
msg, err := getMessageFromJSON(bytes.NewReader(body))
if err != nil || msg != tokenExpiredServerError {
return status, body, ErrUnauthorized
}
return status, body, ErrAuthTokenExpired
}
_, err := c.Authenticate(ctx)
if err != nil {
return status, body, errors.Wrap(err, "auth credentials not valid")
}
alreadyReAuthed = true
}
if duration > maxSleepBeforeRetry {
duration = maxSleepBeforeRetry
}
time.Sleep(duration)
duration *= 2
}
return status, body, callErr
}
func shouldRetry(status int, body []byte, callErr error, debugFunc func(string, ...interface{}), reqID string) (bool, error) {
if status == http.StatusUnauthorized {
return true, nil
}
if 500 <= status && status <= 599 {
msg, err := getMessageFromJSON(bytes.NewReader(body))
if err != nil {
debugFunc("retrying http request due to unexpected status code %v {reqID: %s}", status, reqID)
} else {
debugFunc("retrying http request due to unexpected status code %v: %v {reqID: %s}", status, msg, reqID)
}
return true, nil
}
switch {
case callErr == nil:
return false, nil
case errors.Is(callErr, ErrNoAuthorizedPublicKeys):
return false, callErr
case errors.Is(callErr, ErrNoSSHAgent):
return false, callErr
case errors.Is(callErr, context.Canceled):
return false, callErr
case errors.Is(callErr, context.DeadlineExceeded):
return false, callErr
case strings.Contains(callErr.Error(), "failed to connect to ssh-agent"):
return false, callErr
default:
debugFunc("retrying http request due to unexpected error %v {reqID: %s}", callErr, reqID)
return true, nil
}
}
func (c *Client) doCallImp(ctx context.Context, r request, method, url, reqID string, opts ...requestOpt) (int, []byte, error) {
var bodyReader io.Reader
var bodyLen int64
if r.hasBody {
bodyReader = bytes.NewReader(r.body)
bodyLen = int64(len(r.body))
}
req, err := http.NewRequestWithContext(ctx, method, c.httpAddr+url, bodyReader)
if err != nil {
return 0, nil, err
}
if bodyReader != nil {
req.ContentLength = bodyLen
}
if r.hasHeaders {
req.Header = r.headers.Clone()
}
if r.hasAuth {
if c.authToken == "" {
return 0, nil, ErrUnauthorized
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.authToken))
}
req.Header.Add(requestID, reqID)
client := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: c.serverConnTimeout,
}).DialContext,
Proxy: http.ProxyFromEnvironment,
},
}
resp, err := client.Do(req)
if err != nil {
return 0, nil, err
}
respBody, err := readAllWithContext(ctx, resp.Body)
if err != nil {
return 0, nil, err
}
return resp.StatusCode, respBody, nil
}
func readAllWithContext(ctx context.Context, r io.Reader) ([]byte, error) {
dt := []byte{}
var readErr error
ch := make(chan struct{})
go func() {
dt, readErr = io.ReadAll(r)
close(ch)
}()
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-ch:
return dt, readErr
}
}
func getMessageFromJSON(r io.Reader) (string, error) {
decoder := json.NewDecoder(r)
msg := struct {
Message string `json:"message"`
}{}
err := decoder.Decode(&msg)
if err != nil {
return "", err
}
return msg.Message, nil
}