Skip to content

Commit

Permalink
improve LookupNetIP performance
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Nov 2, 2024
1 parent 2415368 commit f4abf5f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 5 additions & 4 deletions client_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@ func (c *Client) AppendLookupNetIP(dst []netip.Addr, ctx context.Context, networ
return nil, err
}

cname := make([]byte, 0, 64)
var cname []byte
for r := range resp.Records {
switch r.Type {
case TypeCNAME:
cname = resp.DecodeName(cname[:0], r.Data)
cname = r.Data
case TypeA:
dst = append(dst, netip.AddrFrom4(*(*[4]byte)(r.Data)))
case TypeAAAA:
dst = append(dst, netip.AddrFrom16(*(*[16]byte)(r.Data)))
}
}

if len(cname) != 0 && len(dst) == 0 {
dst, err = c.AppendLookupNetIP(dst, ctx, network, b2s(cname))
if cname != nil && len(dst) == 0 {
b := resp.DecodeName(make([]byte, 0, 64), cname)
dst, err = c.AppendLookupNetIP(dst, ctx, network, b2s(b))
}

return dst, err
Expand Down
15 changes: 15 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ func TestClientLookup(t *testing.T) {
}
}

func TestClientLookupNetIP(t *testing.T) {
host := "ip.phus.lu"

client := &Client{
Addr: "1.1.1.1:53",
Timeout: 1 * time.Second,
}

ips, err := client.LookupNetIP(context.Background(), "ip", host)
if err != nil {
t.Errorf("dns_server=%+v LookupNetIP(%#v) error: %+v\n", client.Addr, host, err)
}
t.Logf("dns_server=%+v LookupNetIP(%#v) return %+v", client.Addr, host, ips)
}

func BenchmarkResolverPureGo(b *testing.B) {
resolver := net.Resolver{PreferGo: true}

Expand Down

0 comments on commit f4abf5f

Please sign in to comment.