Skip to content

Commit d943b9a

Browse files
authored
Revert "fix: negative trace substraction when using SetTimeout (#1038)"
This reverts commit ffa5382.
1 parent 443cafa commit d943b9a

3 files changed

Lines changed: 3 additions & 87 deletions

File tree

request.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,31 +1241,16 @@ func (r *Request) TraceInfo() TraceInfo {
12411241
return TraceInfo{}
12421242
}
12431243

1244-
ct.lock.RLock()
1245-
defer ct.lock.RUnlock()
1246-
12471244
ti := TraceInfo{
1248-
DNSLookup: 0,
1249-
TCPConnTime: 0,
1250-
ServerTime: 0,
1245+
DNSLookup: ct.dnsDone.Sub(ct.dnsStart),
1246+
TLSHandshake: ct.tlsHandshakeDone.Sub(ct.tlsHandshakeStart),
1247+
ServerTime: ct.gotFirstResponseByte.Sub(ct.gotConn),
12511248
IsConnReused: ct.gotConnInfo.Reused,
12521249
IsConnWasIdle: ct.gotConnInfo.WasIdle,
12531250
ConnIdleTime: ct.gotConnInfo.IdleTime,
12541251
RequestAttempt: r.Attempt,
12551252
}
12561253

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

request_test.go

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

1930-
func TestTraceInfoOnTimeoutWithSetTimeout(t *testing.T) {
1931-
t.Run("timeout with very short timeout", func(t *testing.T) {
1932-
client := New().
1933-
SetTimeout(1 * time.Millisecond).
1934-
SetBaseURL("http://resty-nowhere.local").
1935-
EnableTrace()
1936-
1937-
resp, err := client.R().Get("/")
1938-
assertNotNil(t, err)
1939-
assertNotNil(t, resp)
1940-
1941-
tr := resp.Request.TraceInfo()
1942-
1943-
assertEqual(t, true, tr.DNSLookup == 0)
1944-
assertEqual(t, true, tr.ConnTime == 0)
1945-
assertEqual(t, true, tr.TLSHandshake == 0)
1946-
assertEqual(t, true, tr.TCPConnTime == 0)
1947-
assertEqual(t, true, tr.ServerTime == 0)
1948-
assertEqual(t, true, tr.ResponseTime == 0)
1949-
assertEqual(t, true, tr.TotalTime > 0)
1950-
assertEqual(t, true, tr.TotalTime == resp.Duration())
1951-
})
1952-
1953-
t.Run("successful request with SetTimeout", func(t *testing.T) {
1954-
ts := createGetServer(t)
1955-
defer ts.Close()
1956-
1957-
client := New().
1958-
SetTimeout(5 * time.Second).
1959-
SetBaseURL(ts.URL).
1960-
EnableTrace()
1961-
1962-
resp, err := client.R().Get("/")
1963-
assertNil(t, err)
1964-
assertNotNil(t, resp)
1965-
1966-
tr := resp.Request.TraceInfo()
1967-
1968-
assertEqual(t, true, tr.DNSLookup >= 0)
1969-
assertEqual(t, true, tr.ConnTime >= 0)
1970-
assertEqual(t, true, tr.TLSHandshake >= 0)
1971-
assertEqual(t, true, tr.TCPConnTime >= 0)
1972-
assertEqual(t, true, tr.ServerTime >= 0)
1973-
assertEqual(t, true, tr.ResponseTime >= 0)
1974-
assertEqual(t, true, tr.TotalTime > 0)
1975-
assertEqual(t, true, tr.TotalTime == resp.Duration())
1976-
})
1977-
}
1978-
19791930
func TestDebugLoggerRequestBodyTooLarge(t *testing.T) {
19801931
formTs := createFormPostServer(t)
19811932
defer formTs.Close()

trace.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"crypto/tls"
1111
"fmt"
1212
"net/http/httptrace"
13-
"sync"
1413
"time"
1514
)
1615

@@ -96,7 +95,6 @@ func (ti TraceInfo) Clone() *TraceInfo {
9695
// with the same naming for easy understanding. Plus additional insights
9796
// [Request].
9897
type clientTrace struct {
99-
lock sync.RWMutex
10098
getConn time.Time
10199
dnsStart time.Time
102100
dnsDone time.Time
@@ -114,55 +112,37 @@ func (t *clientTrace) createContext(ctx context.Context) context.Context {
114112
ctx,
115113
&httptrace.ClientTrace{
116114
DNSStart: func(_ httptrace.DNSStartInfo) {
117-
t.lock.Lock()
118115
t.dnsStart = time.Now()
119-
t.lock.Unlock()
120116
},
121117
DNSDone: func(_ httptrace.DNSDoneInfo) {
122-
t.lock.Lock()
123118
t.dnsDone = time.Now()
124-
t.lock.Unlock()
125119
},
126120
ConnectStart: func(_, _ string) {
127-
t.lock.Lock()
128121
if t.dnsDone.IsZero() {
129122
t.dnsDone = time.Now()
130123
}
131124
if t.dnsStart.IsZero() {
132125
t.dnsStart = t.dnsDone
133126
}
134-
t.lock.Unlock()
135127
},
136128
ConnectDone: func(net, addr string, err error) {
137-
t.lock.Lock()
138129
t.connectDone = time.Now()
139-
t.lock.Unlock()
140130
},
141131
GetConn: func(_ string) {
142-
t.lock.Lock()
143132
t.getConn = time.Now()
144-
t.lock.Unlock()
145133
},
146134
GotConn: func(ci httptrace.GotConnInfo) {
147-
t.lock.Lock()
148135
t.gotConn = time.Now()
149136
t.gotConnInfo = ci
150-
t.lock.Unlock()
151137
},
152138
GotFirstResponseByte: func() {
153-
t.lock.Lock()
154139
t.gotFirstResponseByte = time.Now()
155-
t.lock.Unlock()
156140
},
157141
TLSHandshakeStart: func() {
158-
t.lock.Lock()
159142
t.tlsHandshakeStart = time.Now()
160-
t.lock.Unlock()
161143
},
162144
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) {
163-
t.lock.Lock()
164145
t.tlsHandshakeDone = time.Now()
165-
t.lock.Unlock()
166146
},
167147
},
168148
)

0 commit comments

Comments
 (0)