fcambus / rrda

REST API allowing to perform DNS queries over HTTP
https://www.statdns.com
BSD 2-Clause "Simplified" License
223 stars 56 forks source link

Add a -timeout parameter to dictate how long (in ms) we are willing to wait for an answer to our DNS queries #14

Closed scouture-bw closed 1 year ago

scouture-bw commented 2 years ago

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
fcambus commented 1 year ago

Agreed, this makes sense and is definitely useful, thanks for working on this.

Merged, thanks!