Commit 15f2d0e4 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net, net/http: don't trace UDP dials

The httptrace.ConnectStart and ConnectDone hooks are just about the
post-DNS connection to the host. We were accidentally also firing on
the UDP dials to DNS. Exclude those for now. We can add them back
later as separate hooks if desired. (but they'd only work for pure Go
DNS)

This wasn't noticed earlier because I was developing on a Mac at the
time, which always uses cgo for DNS. When running other tests on
Linux, I started seeing UDP dials.

Updates #12580

Change-Id: I2b2403f2483e227308fe008019f1100f6300250b
Reviewed-on: https://go-review.googlesource.com/23069Reviewed-by: 's avatarAndrew Gerrand <adg@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 4cffe44e
...@@ -32,12 +32,14 @@ type Trace struct { ...@@ -32,12 +32,14 @@ type Trace struct {
// actually be for circular dependency reasons. // actually be for circular dependency reasons.
DNSDone func(netIPs []interface{}, coalesced bool, err error) DNSDone func(netIPs []interface{}, coalesced bool, err error)
// ConnectStart is called before a Dial. In the case of // ConnectStart is called before a TCPAddr or UnixAddr
// DualStack (Happy Eyeballs) dialing, this may be called // Dial. In the case of DualStack (Happy Eyeballs) dialing,
// multiple times, from multiple goroutines. // this may be called multiple times, from multiple
// goroutines.
ConnectStart func(network, addr string) ConnectStart func(network, addr string)
// ConnectStart is called after a Dial with the results. It // ConnectStart is called after a TCPAddr or UnixAddr Dial
// may also be called multiple times, like ConnectStart. // with the results. It may also be called multiple times,
// like ConnectStart.
ConnectDone func(network, addr string, err error) ConnectDone func(network, addr string, err error)
} }
...@@ -472,11 +472,21 @@ func dialSerial(ctx context.Context, dp *dialParam, ras addrList) (Conn, error) ...@@ -472,11 +472,21 @@ func dialSerial(ctx context.Context, dp *dialParam, ras addrList) (Conn, error)
return nil, firstErr return nil, firstErr
} }
// traceDialType reports whether ra is an address type for which
// nettrace.Trace should trace.
func traceDialType(ra Addr) bool {
switch ra.(type) {
case *TCPAddr, *UnixAddr:
return true
}
return false
}
// dialSingle attempts to establish and returns a single connection to // dialSingle attempts to establish and returns a single connection to
// the destination address. // the destination address.
func dialSingle(ctx context.Context, dp *dialParam, ra Addr) (c Conn, err error) { func dialSingle(ctx context.Context, dp *dialParam, ra Addr) (c Conn, err error) {
trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace) trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace)
if trace != nil { if trace != nil && traceDialType(ra) {
raStr := ra.String() raStr := ra.String()
if trace.ConnectStart != nil { if trace.ConnectStart != nil {
trace.ConnectStart(dp.network, raStr) trace.ConnectStart(dp.network, raStr)
......
...@@ -3292,6 +3292,7 @@ func testTransportEventTrace(t *testing.T, noHooks bool) { ...@@ -3292,6 +3292,7 @@ func testTransportEventTrace(t *testing.T, noHooks bool) {
wantSub("Getting conn for dns-is-faked.golang:" + port) wantSub("Getting conn for dns-is-faked.golang:" + port)
wantSub("DNS start: {Host:dns-is-faked.golang}") wantSub("DNS start: {Host:dns-is-faked.golang}")
wantSub("DNS done: {Addrs:[{IP:" + ip + " Zone:}] Err:<nil> Coalesced:false}") wantSub("DNS done: {Addrs:[{IP:" + ip + " Zone:}] Err:<nil> Coalesced:false}")
wantSub("Connecting to tcp " + ts.Listener.Addr().String())
wantSub("connected to tcp " + ts.Listener.Addr().String() + " = <nil>") wantSub("connected to tcp " + ts.Listener.Addr().String() + " = <nil>")
wantSub("Reused:false WasIdle:false IdleTime:0s") wantSub("Reused:false WasIdle:false IdleTime:0s")
wantSub("first response byte") wantSub("first response byte")
...@@ -3299,6 +3300,9 @@ func testTransportEventTrace(t *testing.T, noHooks bool) { ...@@ -3299,6 +3300,9 @@ func testTransportEventTrace(t *testing.T, noHooks bool) {
wantSub("WroteRequest: {Err:<nil>}") wantSub("WroteRequest: {Err:<nil>}")
wantSub("Wait100Continue") wantSub("Wait100Continue")
wantSub("Got100Continue") wantSub("Got100Continue")
if strings.Contains(got, " to udp ") {
t.Errorf("should not see UDP (DNS) connections")
}
if t.Failed() { if t.Failed() {
t.Errorf("Output:\n%s", got) t.Errorf("Output:\n%s", got)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment