diff options
author | Brad Fitzpatrick <bradfitz@golang.org> | 2016-04-14 17:47:25 -0700 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-04-15 22:48:12 +0000 |
commit | b6b4004d5a5bf7099ac9ab76777797236da7fe63 (patch) | |
tree | c3545f04adaf5c8a6421f72be62e96b4a84ae3bd /src/net/lookup.go | |
parent | 1d0977a1d552ad19a88c4ba9aeecd403699d82b6 (diff) | |
download | go-b6b4004d5a5bf7099ac9ab76777797236da7fe63.tar.gz go-b6b4004d5a5bf7099ac9ab76777797236da7fe63.zip |
net: context plumbing, add Dialer.DialContext
For #12580 (http.Transport tracing/analytics)
Updates #13021
Change-Id: I126e494a7bd872e42c388ecb58499ecbf0f014cc
Reviewed-on: https://go-review.googlesource.com/22101
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
Diffstat (limited to 'src/net/lookup.go')
-rw-r--r-- | src/net/lookup.go | 41 |
1 files changed, 15 insertions, 26 deletions
diff --git a/src/net/lookup.go b/src/net/lookup.go index ab6886ddff..0d3ef79bab 100644 --- a/src/net/lookup.go +++ b/src/net/lookup.go @@ -5,8 +5,8 @@ package net import ( + "context" "internal/singleflight" - "time" ) // protocols contains minimal mappings between internet protocol @@ -33,7 +33,7 @@ func LookupHost(host string) (addrs []string, err error) { if ip := ParseIP(host); ip != nil { return []string{host}, nil } - return lookupHost(host) + return lookupHost(context.Background(), host) } // LookupIP looks up host using the local resolver. @@ -47,7 +47,7 @@ func LookupIP(host string) (ips []IP, err error) { if ip := ParseIP(host); ip != nil { return []IP{ip}, nil } - addrs, err := lookupIPMerge(host) + addrs, err := lookupIPMerge(context.Background(), host) if err != nil { return } @@ -63,9 +63,9 @@ var lookupGroup singleflight.Group // lookupIPMerge wraps lookupIP, but makes sure that for any given // host, only one lookup is in-flight at a time. The returned memory // is always owned by the caller. -func lookupIPMerge(host string) (addrs []IPAddr, err error) { +func lookupIPMerge(ctx context.Context, host string) (addrs []IPAddr, err error) { addrsi, err, shared := lookupGroup.Do(host, func() (interface{}, error) { - return testHookLookupIP(lookupIP, host) + return testHookLookupIP(ctx, lookupIP, host) }) return lookupIPReturn(addrsi, err, shared) } @@ -85,37 +85,26 @@ func lookupIPReturn(addrsi interface{}, err error, shared bool) ([]IPAddr, error return addrs, nil } -// lookupIPDeadline looks up a hostname with a deadline. -func lookupIPDeadline(host string, deadline time.Time) (addrs []IPAddr, err error) { - if deadline.IsZero() { - return lookupIPMerge(host) - } - - // We could push the deadline down into the name resolution - // functions. However, the most commonly used implementation - // calls getaddrinfo, which has no timeout. - - timeout := deadline.Sub(time.Now()) - if timeout <= 0 { - return nil, errTimeout - } - t := time.NewTimer(timeout) - defer t.Stop() +// lookupIPContext looks up a hostname with a context. +func lookupIPContext(ctx context.Context, host string) (addrs []IPAddr, err error) { + // TODO(bradfitz): when adding trace hooks later here, make + // sure the tracing is done outside of the singleflight + // merging. Both callers should see the DNS lookup delay, even + // if it's only being done once. The r.Shared bit can be + // included in the trace for callers who need it. ch := lookupGroup.DoChan(host, func() (interface{}, error) { - return testHookLookupIP(lookupIP, host) + return testHookLookupIP(ctx, lookupIP, host) }) select { - case <-t.C: + case <-ctx.Done(): // The DNS lookup timed out for some reason. Force // future requests to start the DNS lookup again // rather than waiting for the current lookup to // complete. See issue 8602. lookupGroup.Forget(host) - - return nil, errTimeout - + return nil, mapErr(ctx.Err()) case r := <-ch: return lookupIPReturn(r.Val, r.Err, r.Shared) } |