Skip to content

Commit cb3ba8c

Browse files
Fix negative trace substraction when using SetTimeout
1 parent 66256ef commit cb3ba8c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

request.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,15 +1242,27 @@ func (r *Request) TraceInfo() TraceInfo {
12421242
}
12431243

12441244
ti := TraceInfo{
1245-
DNSLookup: ct.dnsDone.Sub(ct.dnsStart),
1246-
TLSHandshake: ct.tlsHandshakeDone.Sub(ct.tlsHandshakeStart),
1247-
ServerTime: ct.gotFirstResponseByte.Sub(ct.gotConn),
1245+
DNSLookup: 0,
1246+
TCPConnTime: 0,
1247+
ServerTime: 0,
12481248
IsConnReused: ct.gotConnInfo.Reused,
12491249
IsConnWasIdle: ct.gotConnInfo.WasIdle,
12501250
ConnIdleTime: ct.gotConnInfo.IdleTime,
12511251
RequestAttempt: r.Attempt,
12521252
}
12531253

1254+
if !ct.dnsStart.IsZero() && !ct.dnsDone.IsZero() {
1255+
ti.DNSLookup = ct.dnsDone.Sub(ct.dnsStart)
1256+
}
1257+
1258+
if !ct.tlsHandshakeDone.IsZero() && !ct.tlsHandshakeStart.IsZero() {
1259+
ti.TLSHandshake = ct.tlsHandshakeDone.Sub(ct.tlsHandshakeStart)
1260+
}
1261+
1262+
if !ct.gotFirstResponseByte.IsZero() && !ct.gotConn.IsZero() {
1263+
ti.ServerTime = ct.gotFirstResponseByte.Sub(ct.gotConn)
1264+
}
1265+
12541266
// Calculate the total time accordingly when connection is reused,
12551267
// and DNS start and get conn time may be zero if the request is invalid.
12561268
// See issue #1016.

request_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,28 @@ func TestTraceInfoOnTimeout(t *testing.T) {
19271927
assertEqual(t, true, tr.TotalTime == resp.Duration())
19281928
}
19291929

1930+
func TestTraceInfoOnTimeoutWithSetTimeout(t *testing.T) {
1931+
client := New().
1932+
SetTimeout(1 * time.Millisecond).
1933+
SetBaseURL("http://resty-nowhere.local").
1934+
EnableTrace()
1935+
1936+
resp, err := client.R().Get("/")
1937+
assertNotNil(t, err)
1938+
assertNotNil(t, resp)
1939+
1940+
tr := resp.Request.TraceInfo()
1941+
1942+
assertEqual(t, true, tr.DNSLookup == 0)
1943+
assertEqual(t, true, tr.ConnTime == 0)
1944+
assertEqual(t, true, tr.TLSHandshake == 0)
1945+
assertEqual(t, true, tr.TCPConnTime == 0)
1946+
assertEqual(t, true, tr.ServerTime == 0)
1947+
assertEqual(t, true, tr.ResponseTime == 0)
1948+
assertEqual(t, true, tr.TotalTime > 0)
1949+
assertEqual(t, true, tr.TotalTime == resp.Duration())
1950+
}
1951+
19301952
func TestDebugLoggerRequestBodyTooLarge(t *testing.T) {
19311953
formTs := createFormPostServer(t)
19321954
defer formTs.Close()

0 commit comments

Comments
 (0)