forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
78 lines (66 loc) · 2.56 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
package util
import (
"context"
"log/slog"
"net/http"
"time"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-retryablehttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
type LeveledSlog struct {
inner *slog.Logger
}
// re-writes HTTP client ERROR to WARN level (because of retries)
func (l LeveledSlog) Error(msg string, keysAndValues ...interface{}) {
l.inner.Warn(msg, keysAndValues...)
}
func (l LeveledSlog) Warn(msg string, keysAndValues ...interface{}) {
l.inner.Warn(msg, keysAndValues...)
}
func (l LeveledSlog) Info(msg string, keysAndValues ...interface{}) {
l.inner.Info(msg, keysAndValues...)
}
func (l LeveledSlog) Debug(msg string, keysAndValues ...interface{}) {
l.inner.Debug(msg, keysAndValues...)
}
// Generates an HTTP client with decent general-purpose defaults around
// timeouts and retries. The returned client has the stdlib http.Client
// interface, but has Hashicorp retryablehttp logic internally.
//
// This client will retry on connection errors, 5xx status (except 501).
// It will log intermediate failures with WARN level. This does not start from
// http.DefaultClient.
//
// This should be usable for XRPC clients, and other general inter-service
// client needs. CLI tools might want shorter timeouts and fewer retries by
// default.
func RobustHTTPClient() *http.Client {
logger := LeveledSlog{inner: slog.Default().With("subsystem", "RobustHTTPClient")}
retryClient := retryablehttp.NewClient()
retryClient.HTTPClient.Transport = otelhttp.NewTransport(cleanhttp.DefaultPooledTransport())
retryClient.RetryMax = 3
retryClient.RetryWaitMin = 1 * time.Second
retryClient.RetryWaitMax = 10 * time.Second
retryClient.Logger = retryablehttp.LeveledLogger(logger)
retryClient.CheckRetry = XRPCRetryPolicy
client := retryClient.StandardClient()
client.Timeout = 30 * time.Second
return client
}
// For use in local integration tests. Short timeouts, no retries, etc
func TestingHTTPClient() *http.Client {
client := http.DefaultClient
client.Timeout = 1 * time.Second
return client
}
// XRPCRetryPolicy 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 XRPCRetryPolicy(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)
}