In more time-sensitive scenarios where one may want to fail and move on quickly if an answer to a DNS query cannot be returned in a timely fashion, it may be beneficial to have the ability to set a timeout value that is lower than the system's default.
This takes advantage of miekg/dns's ability to specify a Timeout parameter through a net.Dialer as explained in their documentation:
More advanced options are available using a net.Dialer and the corresponding API.
For example it is possible to set a timeout, or to specify a source IP address
and port to use for the connection:
c := new(dns.Client)
laddr := net.UDPAddr{
IP: net.ParseIP("[::1]"),
Port: 12345,
Zone: "",
}
c.Dialer := &net.Dialer{
Timeout: 200 * time.Millisecond,
LocalAddr: &laddr,
}
in, rtt, err := c.Exchange(m1, "8.8.8.8:53")
Before the change:
# ./rrda -host=127.0.0.1 -port=5353
# time curl http://127.0.0.1:5353/:53/<domain>/<query type>
{"code":501,"message":"DNS server could not be reached"}
real 0m2.011s
user 0m0.003s
sys 0m0.007s
After the change:
# ./rrda -host=127.0.0.1 -port=5353 -timeout=200
# time curl http://127.0.0.1:5353/:53/<domain>/<query type>
{"code":501,"message":"DNS server could not be reached"}
real 0m0.212s
user 0m0.002s
sys 0m0.008s
In more time-sensitive scenarios where one may want to fail and move on quickly if an answer to a DNS query cannot be returned in a timely fashion, it may be beneficial to have the ability to set a timeout value that is lower than the system's default.
This takes advantage of miekg/dns's ability to specify a
Timeout
parameter through anet.Dialer
as explained in their documentation:Before the change:
After the change: