-
Notifications
You must be signed in to change notification settings - Fork 0
/
httptrace.go
50 lines (40 loc) · 1.11 KB
/
httptrace.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
package libtower
import (
"crypto/tls"
"net/http"
"net/http/httptrace"
"time"
)
// Trace http
func (ht *HTTPTrace) Trace() error {
// res := HTTPTrace{URL: url, Method: method}
req, err := http.NewRequest(ht.Method, ht.URL, nil)
if err != nil {
return err
}
var start, connect, dns, tlsHandshake time.Time
trace := &httptrace.ClientTrace{
DNSStart: func(dsi httptrace.DNSStartInfo) { dns = time.Now() },
DNSDone: func(ddi httptrace.DNSDoneInfo) {
ht.DNS = time.Since(dns)
},
TLSHandshakeStart: func() { tlsHandshake = time.Now() },
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
ht.TLSHandshake = time.Since(tlsHandshake)
},
ConnectStart: func(network, addr string) { connect = time.Now() },
ConnectDone: func(network, addr string, err error) {
ht.Connect = time.Since(connect)
},
GotFirstResponseByte: func() {
ht.Connect = time.Since(start)
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
start = time.Now()
if _, err := http.DefaultTransport.RoundTrip(req); err != nil {
return err
}
ht.Total = time.Since(start)
return nil
}