File tree 2 files changed +22
-6
lines changed
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 {
907
907
RequestAttempt : r .Attempt ,
908
908
}
909
909
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
916
918
}
919
+ ti .TotalTime = ct .endTime .Sub (requestStartTime )
917
920
918
921
// Only calculate on successful connections
919
922
if ! ct .connectDone .IsZero () {
Original file line number Diff line number Diff line change @@ -2103,6 +2103,19 @@ func TestTraceInfoWithoutEnableTrace(t *testing.T) {
2103
2103
}
2104
2104
}
2105
2105
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
+
2106
2119
func TestTraceInfoOnTimeout (t * testing.T ) {
2107
2120
client := dc ()
2108
2121
client .SetHostURL ("http://resty-nowhere.local" ).EnableTrace ()
You can’t perform that action at this time.
0 commit comments