This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
57 lines (43 loc) · 1.58 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
package core
import (
"context"
"net"
"net/http"
"time"
)
type ProfilingTransport struct {
roundTripper http.RoundTripper
dialer *net.Dialer
connectionStart time.Time
connectionEnd time.Time
}
type ProfilingContextKey string
func newProfilingTransport() *ProfilingTransport {
transport := &ProfilingTransport{
dialer: &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
},
}
transport.roundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: transport.dial,
TLSHandshakeTimeout: 10 * time.Second,
}
return transport
}
func (transport *ProfilingTransport) RoundTrip(r *http.Request) (*http.Response, error) {
ctxRoundTripStart := context.WithValue(r.Context(), ProfilingContextKey("roundTripStart"), time.Now())
response, err := transport.roundTripper.RoundTrip(r.WithContext(ctxRoundTripStart))
ctxRoundTripEnd := context.WithValue(response.Request.Context(), ProfilingContextKey("roundTripEnd"), time.Now())
ctxConnectionStart := context.WithValue(ctxRoundTripEnd, ProfilingContextKey("connectionStart"), transport.connectionStart)
ctxConnectionEnd := context.WithValue(ctxConnectionStart, ProfilingContextKey("connectionEnd"), transport.connectionEnd)
response.Request = response.Request.WithContext(ctxConnectionEnd)
return response, err
}
func (transport *ProfilingTransport) dial(network, addr string) (net.Conn, error) {
transport.connectionStart = time.Now()
connections, err := transport.dialer.Dial(network, addr)
transport.connectionEnd = time.Now()
return connections, err
}