-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.go
150 lines (135 loc) · 3.5 KB
/
transport.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
package uhttp
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"sync"
"time"
"github.com/conductorone/baton-sdk/pkg/sdk"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.uber.org/zap"
"golang.org/x/net/http2"
)
// NewTransport creates a new Transport, applies the options, and then cycles the transport.
func NewTransport(ctx context.Context, options ...Option) (*Transport, error) {
t := newTransport()
for _, opt := range options {
opt.Apply(t)
}
_, err := t.cycle(ctx)
if err != nil {
return nil, err
}
return t, nil
}
type Transport struct {
userAgent string
tlsClientConfig *tls.Config
roundTripper http.RoundTripper
logger *zap.Logger
log bool
nextCycle time.Time
mtx sync.RWMutex
}
func newTransport() *Transport {
return &Transport{
tlsClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}
}
func (t *Transport) cycle(ctx context.Context) (http.RoundTripper, error) {
n := time.Now()
t.mtx.RLock()
if t.nextCycle.After(n) && t.roundTripper != nil {
defer t.mtx.RUnlock()
return t.roundTripper, nil
}
t.mtx.RUnlock()
t.mtx.Lock()
defer t.mtx.Unlock()
n = time.Now()
// other goroutine changed it under us
if t.nextCycle.After(n) && t.roundTripper != nil {
return t.roundTripper, nil
}
var err error
t.roundTripper, err = t.make(ctx)
if err != nil {
return nil, err
}
t.nextCycle = n.Add(time.Minute * 5)
return t.roundTripper, nil
}
type userAgentTripper struct {
next http.RoundTripper
userAgent string
}
func (uat *userAgentTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", uat.userAgent)
}
return uat.next.RoundTrip(req)
}
func (t *Transport) make(ctx context.Context) (http.RoundTripper, error) {
// based on http.DefaultTransport
baseTransport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: t.tlsClientConfig,
}
err := http2.ConfigureTransport(baseTransport)
if err != nil {
return nil, err
}
var rv http.RoundTripper = baseTransport
t.userAgent = t.userAgent + " baton-sdk/" + sdk.Version
rv = &userAgentTripper{next: rv, userAgent: t.userAgent}
return rv, nil
}
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
ctx := req.Context()
rt, err := t.cycle(ctx)
if err != nil {
return nil, fmt.Errorf("uhttp: cycle failed: %w", err)
}
if t.log {
t.l(ctx).Debug("Request started",
zap.String("http.method", req.Method),
zap.String("http.url_details.host", req.URL.Host),
zap.String("http.url_details.path", req.URL.Path),
)
}
resp, err := rt.RoundTrip(req)
if t.log {
fields := []zap.Field{zap.String("http.method", req.Method),
zap.String("http.url_details.host", req.URL.Host),
zap.String("http.url_details.path", req.URL.Path),
}
if err != nil {
fields = append(fields, zap.Error(err))
}
if resp != nil {
fields = append(fields, zap.Int("http.status_code", resp.StatusCode))
}
t.l(ctx).Debug("Request complete", fields...)
}
return resp, err
}
func (t *Transport) l(ctx context.Context) *zap.Logger {
if t.logger != nil {
return t.logger
}
return ctxzap.Extract(ctx)
}