-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
54 lines (45 loc) · 1.62 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
package httputils
import (
"context"
"net/http"
"time"
"github.com/hashicorp/go-retryablehttp"
"go.uber.org/zap"
)
func RobustHTTPClient(logger *zap.Logger) *http.Client {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 3
retryClient.RetryWaitMin = 1 * time.Second
retryClient.RetryWaitMax = 10 * time.Second
retryClient.Logger = retryablehttp.LeveledLogger(LeveledZap{inner: logger})
retryClient.CheckRetry = RetryPolicy
client := retryClient.StandardClient()
client.Timeout = 30 * time.Second
return client
}
type LeveledZap struct {
inner *zap.Logger
}
// re-writes HTTP client ERROR to WARN level (because of retries)
func (l LeveledZap) Error(msg string, keysAndValues ...interface{}) {
l.inner.Sugar().Warnw(msg, keysAndValues...)
}
func (l LeveledZap) Warn(msg string, keysAndValues ...interface{}) {
l.inner.Sugar().Warnw(msg, keysAndValues...)
}
func (l LeveledZap) Info(msg string, keysAndValues ...interface{}) {
l.inner.Sugar().Infow(msg, keysAndValues...)
}
func (l LeveledZap) Debug(msg string, keysAndValues ...interface{}) {
l.inner.Sugar().Debugw(msg, keysAndValues...)
}
// RetryPolicy is a custom wrapper around retryablehttp.DefaultRetryPolicy.
// It treats `429 Too Many Requests` as non-retryable, so the application can decide
// how to deal with rate-limiting.
func RetryPolicy(ctx context.Context, resp *http.Response, err error) (bool, error) {
if err == nil && resp.StatusCode == http.StatusTooManyRequests {
return false, nil
}
// TODO: implement returning errors on non-200 responses w/o introducing circular dependencies.
return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
}