Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: add timeout to Resolver #69498

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/net/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"internal/singleflight"
"net/netip"
"sync"
"time"

"golang.org/x/net/dns/dnsmessage"
)
Expand Down Expand Up @@ -164,8 +165,10 @@ type Resolver struct {
// The return values are ([]IPAddr, error).
lookupGroup singleflight.Group

// Timeout is the time to spend Resolving
Timeout time.Duration

// TODO(bradfitz): optional interface impl override hook
// TODO(bradfitz): Timeout time.Duration?
}

func (r *Resolver) preferGo() bool { return r != nil && r.PreferGo }
Expand Down Expand Up @@ -678,12 +681,21 @@ func (r *Resolver) dial(ctx context.Context, network, server string) (Conn, erro
// call back here to translate it. The DNS config parser has
// already checked that all the cfg.servers are IP
// addresses, which Dial will use without a DNS lookup.
if r.Timeout > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, r.Timeout)
defer cancel()
}

var c Conn
var err error
if r != nil && r.Dial != nil {
c, err = r.Dial(ctx, network, server)
} else {
var d Dialer
if r.Timeout > 0 {
d.Timeout = r.Timeout
}
c, err = d.DialContext(ctx, network, server)
}
if err != nil {
Expand Down