Skip to content

Commit 5219400

Browse files
committed
Refactor domain resolution in main.go to use context with timeout for improved DNS lookup handling. This change enhances the reliability of the resolver by ensuring that the lookup respects the specified timeout.
1 parent 5ad1ae7 commit 5219400

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,16 @@ func resolveDomain(domain string, timeout time.Duration, dnsServer string, verbo
371371
resolver := &net.Resolver{
372372
PreferGo: true,
373373
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
374-
d := net.Dialer{
375-
Timeout: timeout,
376-
}
374+
d := net.Dialer{Timeout: timeout}
377375
return d.DialContext(ctx, "udp", dnsServer)
378376
},
379377
}
380378

379+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
380+
defer cancel()
381+
381382
start := time.Now()
382-
ips, err := resolver.LookupHost(context.Background(), domain)
383+
ips, err := resolver.LookupHost(ctx, domain)
383384
elapsed := time.Since(start)
384385

385386
if verbose && err == nil {

0 commit comments

Comments
 (0)