-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprobe.go
More file actions
171 lines (162 loc) · 4.88 KB
/
Copy pathprobe.go
File metadata and controls
171 lines (162 loc) · 4.88 KB
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
package netquality
import (
"context"
"crypto/tls"
"io"
"net/http"
"net/http/httptrace"
"sync"
)
// probeTimes collects httptrace timestamps. HTTP/2 invokes trace hooks from
// several goroutines, so every access is under the mutex.
type probeTimes struct {
mu sync.Mutex
s LatencySample
dnsStart, connStart, tlsStart, wrote instant
reused bool
}
// foreignProbeBytes / selfProbeBytes are the draft's per-probe cost estimates
// (Section 5.3), used to keep probe traffic under PTC of capacity and to count
// probe bytes against MaxBytes.
const (
foreignProbeBytes = 5000
selfProbeBytes = 1000
)
// foreignProbe performs a GET of the small URL on a brand-new connection and
// records per-stage timings. rt must not reuse connections.
// observe, if non-nil, receives the TLS state of every successful handshake.
func foreignProbe(ctx context.Context, rt http.RoundTripper, url string, extra http.Header, now func() instant, observe func(tls.ConnectionState)) (LatencySample, error) {
pt := &probeTimes{}
start := now()
lock := func(f func()) { pt.mu.Lock(); defer pt.mu.Unlock(); f() }
trace := &httptrace.ClientTrace{
DNSStart: func(httptrace.DNSStartInfo) { lock(func() { pt.dnsStart = now() }) },
DNSDone: func(httptrace.DNSDoneInfo) {
lock(func() {
if !pt.dnsStart.isZero() {
pt.s.DNS = now().sub(pt.dnsStart)
}
})
},
ConnectStart: func(_, _ string) {
lock(func() {
if pt.connStart.isZero() {
pt.connStart = now()
}
})
},
ConnectDone: func(_, _ string, err error) {
lock(func() {
if err == nil && !pt.connStart.isZero() && pt.s.Connect == 0 {
pt.s.Connect = now().sub(pt.connStart)
}
})
},
TLSHandshakeStart: func() { lock(func() { pt.tlsStart = now() }) },
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
lock(func() {
if err == nil && !pt.tlsStart.isZero() {
pt.s.TLS = now().sub(pt.tlsStart)
pt.s.TLSRTTs = tlsRoundTrips(cs.Version)
}
})
if err == nil && observe != nil {
observe(cs)
}
},
GotConn: func(info httptrace.GotConnInfo) { lock(func() { pt.reused = info.Reused }) },
WroteRequest: func(httptrace.WroteRequestInfo) { lock(func() { pt.wrote = now() }) },
GotFirstResponseByte: func() {
lock(func() {
if !pt.wrote.isZero() {
pt.s.TTFB = now().sub(pt.wrote)
}
})
},
}
if err := doProbe(httptrace.WithClientTrace(ctx, trace), rt, url, extra); err != nil {
return LatencySample{}, err
}
end := now()
pt.mu.Lock()
defer pt.mu.Unlock()
s := pt.s
s.Total = end.sub(start)
wr := pt.wrote
if wr.isZero() {
wr = start
}
s.HTTP = end.sub(wr)
// A reused connection (only possible with a custom RoundTripper) still
// yields a request-time sample, just without connection stages.
s.Staged = !pt.reused
if pt.reused {
s.DNS, s.Connect, s.TLS, s.TLSRTTs = 0, 0, 0, 0
}
return s, nil
}
// doProbe issues the GET and drains the 1-byte body.
func doProbe(ctx context.Context, rt http.RoundTripper, url string, extra http.Header) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
setProbeHeaders(req, extra)
resp, err := rt.RoundTrip(req)
if err != nil {
return err
}
defer resp.Body.Close()
if err := checkStatus(resp); err != nil {
return err
}
_, err = io.Copy(io.Discard, resp.Body)
return err
}
// selfProbe performs a GET of the small URL on an existing (load) transport.
// Only the request-to-full-response time is meaningful.
func selfProbe(ctx context.Context, rt http.RoundTripper, url string, extra http.Header, now func() instant) (LatencySample, error) {
pt := &probeTimes{}
start := now()
trace := &httptrace.ClientTrace{
WroteRequest: func(httptrace.WroteRequestInfo) {
pt.mu.Lock()
pt.wrote = now()
pt.mu.Unlock()
},
}
if err := doProbe(httptrace.WithClientTrace(ctx, trace), rt, url, extra); err != nil {
return LatencySample{}, err
}
end := now()
pt.mu.Lock()
wr := pt.wrote
pt.mu.Unlock()
if wr.isZero() {
wr = start
}
return LatencySample{Total: end.sub(start), HTTP: end.sub(wr)}, nil
}
// setProbeHeaders applies the headers every test request carries, then the
// caller-supplied extras (which win on conflict).
func setProbeHeaders(req *http.Request, extra http.Header) {
req.Header.Set("Accept-Encoding", "identity")
req.Header.Set("Cache-Control", "no-store")
req.Header.Set("User-Agent", userAgent)
for k, vs := range extra {
req.Header[http.CanonicalHeaderKey(k)] = append([]string(nil), vs...)
}
}
const userAgent = "netquality-go/1 (+https://github.com/korya/netquality)"
// tlsRoundTrips is the number of round trips the handshake of a TLS version
// costs, used to normalise tls_f per draft Section 5.3.
func tlsRoundTrips(version uint16) int {
switch version {
case tls.VersionTLS13:
return 1
case 0:
return 0
default:
return 2
}
}