Skip to content

proposal: net: only request DNS A record when IPv6 not support, when using the net.Dial method #58995

@WeiZhixiong

Description

@WeiZhixiong

Problem:

when use net or net/http start reqeust to domain name, like this:

http.Get("https://golang.org/")
net.Dial("tcp", "golang.org:80")
net.Dial("udp", "golang.org:443")

The method DialContext of net.Dialer will resolve both A and AAAA records for a domain name, regardless of whether the machine's network supports IPv6 or not. If not support IPv6, DNS AAAA requests is unnecessary, it will cause additional cost.

Possible solution:
change file: go/src/net/dial.go
old:

func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error) {
        ...
	addrs, err := d.resolver().resolveAddrList(resolveCtx, "dial", network, address, d.LocalAddr)
        ...
}

new:

func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error) {
         ...
	networkForResolve := network
	switch network {
	case "tcp", "udp":
		if !supportsIPv6() {
			networkForResolve = network + "4"
		}
	}
	addrs, err := d.resolver().resolveAddrList(resolveCtx, "dial", networkForResolve, address, d.LocalAddr)
        ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions