File tree Expand file tree Collapse file tree 2 files changed +22
-6
lines changed Expand file tree Collapse file tree 2 files changed +22
-6
lines changed Original file line number Diff line number Diff line change @@ -907,13 +907,16 @@ func (r *Request) TraceInfo() TraceInfo {
907907 RequestAttempt : r .Attempt ,
908908 }
909909
910- // Calculate the total time accordingly,
911- // when connection is reused
912- if ct .gotConnInfo .Reused {
913- ti .TotalTime = ct .endTime .Sub (ct .getConn )
914- } else {
915- ti .TotalTime = ct .endTime .Sub (ct .dnsStart )
910+ // Calculate the total time accordingly when connection is reused,
911+ // and DNS start and get conn time may be zero if the request is invalid.
912+ // See issue #1016.
913+ requestStartTime := r .Time
914+ if ct .gotConnInfo .Reused && ! ct .getConn .IsZero () {
915+ requestStartTime = ct .getConn
916+ } else if ! ct .dnsStart .IsZero () {
917+ requestStartTime = ct .dnsStart
916918 }
919+ ti .TotalTime = ct .endTime .Sub (requestStartTime )
917920
918921 // Only calculate on successful connections
919922 if ! ct .connectDone .IsZero () {
Original file line number Diff line number Diff line change @@ -2103,6 +2103,19 @@ func TestTraceInfoWithoutEnableTrace(t *testing.T) {
21032103 }
21042104}
21052105
2106+ func TestTraceInfoWithInvalidRequest (t * testing.T ) {
2107+ client := dc ()
2108+ resp , err := client .R ().EnableTrace ().Get ("unknown://url.com" )
2109+ assertNotNil (t , err )
2110+ tr := resp .Request .TraceInfo ()
2111+ assertEqual (t , true , tr .DNSLookup == 0 )
2112+ assertEqual (t , true , tr .ConnTime == 0 )
2113+ assertEqual (t , true , tr .TLSHandshake == 0 )
2114+ assertEqual (t , true , tr .ServerTime == 0 )
2115+ assertEqual (t , true , tr .ResponseTime == 0 )
2116+ assertEqual (t , true , tr .TotalTime > 0 && tr .TotalTime < time .Second )
2117+ }
2118+
21062119func TestTraceInfoOnTimeout (t * testing.T ) {
21072120 client := dc ()
21082121 client .SetHostURL ("http://resty-nowhere.local" ).EnableTrace ()
You can’t perform that action at this time.
0 commit comments