package req
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http/httptrace"
"time"
)
const (
traceFmt = `TotalTime : %v
DNSLookupTime : %v
TCPConnectTime : %v
TLSHandshakeTime : %v
FirstResponseTime : %v
ResponseTime : %v
IsConnReused: : false
RemoteAddr : %v
LocalAddr : %v`
traceReusedFmt = `TotalTime : %v
FirstResponseTime : %v
ResponseTime : %v
IsConnReused: : true
RemoteAddr : %v
LocalAddr : %v`
)
func (t TraceInfo ) Blame () string {
if t .RemoteAddr == nil {
return "trace is not enabled"
}
var mk string
var mv time .Duration
m := map [string ]time .Duration {
"on dns lookup" : t .DNSLookupTime ,
"on tcp connect" : t .TCPConnectTime ,
"on tls handshake" : t .TLSHandshakeTime ,
"from connection ready to server respond first byte" : t .FirstResponseTime ,
"from server respond first byte to request completion" : t .ResponseTime ,
}
for k , v := range m {
if v > mv {
mk = k
mv = v
}
}
if mk == "" {
return "nothing to blame"
}
return fmt .Sprintf ("the request total time is %v, and costs %v %s" , t .TotalTime , mv , mk )
}
func (t TraceInfo ) String () string {
if t .RemoteAddr == nil {
return "trace is not enabled"
}
if t .IsConnReused {
return fmt .Sprintf (traceReusedFmt , t .TotalTime , t .FirstResponseTime , t .ResponseTime , t .RemoteAddr , t .LocalAddr )
}
return fmt .Sprintf (traceFmt , t .TotalTime , t .DNSLookupTime , t .TCPConnectTime , t .TLSHandshakeTime , t .FirstResponseTime , t .ResponseTime , t .RemoteAddr , t .LocalAddr )
}
type TraceInfo struct {
DNSLookupTime time .Duration
ConnectTime time .Duration
TCPConnectTime time .Duration
TLSHandshakeTime time .Duration
FirstResponseTime time .Duration
ResponseTime time .Duration
TotalTime time .Duration
IsConnReused bool
IsConnWasIdle bool
ConnIdleTime time .Duration
RemoteAddr net .Addr
LocalAddr net .Addr
}
type clientTrace struct {
getConn time .Time
dnsStart time .Time
dnsDone time .Time
connectDone time .Time
tlsHandshakeStart time .Time
tlsHandshakeDone time .Time
gotConn time .Time
gotFirstResponseByte time .Time
endTime time .Time
gotConnInfo httptrace .GotConnInfo
}
func (t *clientTrace ) createContext (ctx context .Context ) context .Context {
return httptrace .WithClientTrace (
ctx ,
&httptrace .ClientTrace {
DNSStart : func (_ httptrace .DNSStartInfo ) {
t .dnsStart = time .Now ()
},
DNSDone : func (_ httptrace .DNSDoneInfo ) {
t .dnsDone = time .Now ()
},
ConnectStart : func (_ , _ string ) {
if t .dnsDone .IsZero () {
t .dnsDone = time .Now ()
}
if t .dnsStart .IsZero () {
t .dnsStart = t .dnsDone
}
},
ConnectDone : func (net , addr string , err error ) {
t .connectDone = time .Now ()
},
GetConn : func (_ string ) {
t .getConn = time .Now ()
},
GotConn : func (ci httptrace .GotConnInfo ) {
t .gotConn = time .Now ()
t .gotConnInfo = ci
},
GotFirstResponseByte : func () {
t .gotFirstResponseByte = time .Now ()
},
TLSHandshakeStart : func () {
t .tlsHandshakeStart = time .Now ()
},
TLSHandshakeDone : func (_ tls .ConnectionState , _ error ) {
t .tlsHandshakeDone = time .Now ()
},
},
)
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .