Skip to content

Commit f586aa4

Browse files
authored
net/dnscache: use parent context to perform lookup (#48)
Upstream PR: tailscale#11935
1 parent d329bbd commit f586aa4

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

net/dnscache/dnscache.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (r *Resolver) LookupIP(ctx context.Context, host string) (ip, v6 netip.Addr
222222
}
223223

224224
ch := r.sf.DoChan(host, func() (ret ipRes, _ error) {
225-
ip, ip6, allIPs, err := r.lookupIP(host)
225+
ip, ip6, allIPs, err := r.lookupIP(ctx, host)
226226
if err != nil {
227227
return ret, err
228228
}
@@ -283,30 +283,30 @@ func (r *Resolver) lookupTimeoutForHost(host string) time.Duration {
283283
return 10 * time.Second
284284
}
285285

286-
func (r *Resolver) lookupIP(host string) (ip, ip6 netip.Addr, allIPs []netip.Addr, err error) {
286+
func (r *Resolver) lookupIP(ctx context.Context, host string) (ip, ip6 netip.Addr, allIPs []netip.Addr, err error) {
287287
if ip, ip6, allIPs, ok := r.lookupIPCache(host); ok {
288288
r.dlogf("%q found in cache as %v", host, ip)
289289
return ip, ip6, allIPs, nil
290290
}
291291

292-
ctx, cancel := context.WithTimeout(context.Background(), r.lookupTimeoutForHost(host))
293-
defer cancel()
294-
ips, err := r.fwd().LookupNetIP(ctx, "ip", host)
292+
lookupCtx, lookupCancel := context.WithTimeout(ctx, r.lookupTimeoutForHost(host))
293+
defer lookupCancel()
294+
ips, err := r.fwd().LookupNetIP(lookupCtx, "ip", host)
295295
if err != nil || len(ips) == 0 {
296296
if resolver, ok := r.cloudHostResolver(); ok {
297297
r.dlogf("resolving %q via cloud resolver", host)
298-
ips, err = resolver.LookupNetIP(ctx, "ip", host)
298+
ips, err = resolver.LookupNetIP(lookupCtx, "ip", host)
299299
}
300300
}
301301
if (err != nil || len(ips) == 0) && r.LookupIPFallback != nil {
302-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
303-
defer cancel()
302+
lookupCtx, lookupCancel := context.WithTimeout(ctx, 30*time.Second)
303+
defer lookupCancel()
304304
if err != nil {
305305
r.dlogf("resolving %q using fallback resolver due to error", host)
306306
} else {
307307
r.dlogf("resolving %q using fallback resolver due to no returned IPs", host)
308308
}
309-
ips, err = r.LookupIPFallback(ctx, host)
309+
ips, err = r.LookupIPFallback(lookupCtx, host)
310310
}
311311
if err != nil {
312312
return netip.Addr{}, netip.Addr{}, nil, err

0 commit comments

Comments
 (0)