-
Notifications
You must be signed in to change notification settings - Fork 73
/
go18.go
134 lines (106 loc) · 3.18 KB
/
go18.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
// +build go1.8
package httpstat
import (
"context"
"crypto/tls"
"net/http/httptrace"
"time"
)
// End sets the time when reading response is done.
// This must be called after reading response body.
func (r *Result) End(t time.Time) {
r.transferDone = t
// This means result is empty (it does nothing).
// Skip setting value(contentTransfer and total will be zero).
if r.dnsStart.IsZero() {
return
}
r.contentTransfer = r.transferDone.Sub(r.transferStart)
r.total = r.transferDone.Sub(r.dnsStart)
}
// ContentTransfer returns the duration of content transfer time.
// It is from first response byte to the given time. The time must
// be time after read body (go-httpstat can not detect that time).
func (r *Result) ContentTransfer(t time.Time) time.Duration {
return t.Sub(r.serverDone)
}
// Total returns the duration of total http request.
// It is from dns lookup start time to the given time. The
// time must be time after read body (go-httpstat can not detect that time).
func (r *Result) Total(t time.Time) time.Duration {
return t.Sub(r.dnsStart)
}
func withClientTrace(ctx context.Context, r *Result) context.Context {
return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
DNSStart: func(i httptrace.DNSStartInfo) {
r.dnsStart = time.Now()
},
DNSDone: func(i httptrace.DNSDoneInfo) {
r.dnsDone = time.Now()
r.DNSLookup = r.dnsDone.Sub(r.dnsStart)
r.NameLookup = r.dnsDone.Sub(r.dnsStart)
},
ConnectStart: func(_, _ string) {
r.tcpStart = time.Now()
// When connecting to IP (When no DNS lookup)
if r.dnsStart.IsZero() {
r.dnsStart = r.tcpStart
r.dnsDone = r.tcpStart
}
},
ConnectDone: func(network, addr string, err error) {
r.tcpDone = time.Now()
r.TCPConnection = r.tcpDone.Sub(r.tcpStart)
r.Connect = r.tcpDone.Sub(r.dnsStart)
},
TLSHandshakeStart: func() {
r.isTLS = true
r.tlsStart = time.Now()
},
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) {
r.tlsDone = time.Now()
r.TLSHandshake = r.tlsDone.Sub(r.tlsStart)
r.Pretransfer = r.tlsDone.Sub(r.dnsStart)
},
GotConn: func(i httptrace.GotConnInfo) {
// Handle when keep alive is used and connection is reused.
// DNSStart(Done) and ConnectStart(Done) is skipped
if i.Reused {
r.isReused = true
}
},
WroteRequest: func(info httptrace.WroteRequestInfo) {
r.serverStart = time.Now()
// When client doesn't use DialContext or using old (before go1.7) `net`
// pakcage, DNS/TCP/TLS hook is not called.
if r.dnsStart.IsZero() && r.tcpStart.IsZero() {
now := r.serverStart
r.dnsStart = now
r.dnsDone = now
r.tcpStart = now
r.tcpDone = now
}
// When connection is re-used, DNS/TCP/TLS hook is not called.
if r.isReused {
now := r.serverStart
r.dnsStart = now
r.dnsDone = now
r.tcpStart = now
r.tcpDone = now
r.tlsStart = now
r.tlsDone = now
}
if r.isTLS {
return
}
r.TLSHandshake = r.tcpDone.Sub(r.tcpDone)
r.Pretransfer = r.Connect
},
GotFirstResponseByte: func() {
r.serverDone = time.Now()
r.ServerProcessing = r.serverDone.Sub(r.serverStart)
r.StartTransfer = r.serverDone.Sub(r.dnsStart)
r.transferStart = r.serverDone
},
})
}