Skip to content

Commit a1283ec

Browse files
committed
Added timeout option to customize the DNS timeout
1 parent c7f5d33 commit a1283ec

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"github.com/mediocregopher/lever"
77
"github.com/miekg/dns"
88
"strings"
9+
"time"
910
)
1011

1112
var dnsServerGroups [][]string
13+
var client dns.Client
1214

1315
func tryProxy(m *dns.Msg, addr string) *dns.Msg {
14-
aM, err := dns.Exchange(m, addr)
16+
aM, _, err := client.Exchange(m, addr)
1517
if err != nil {
1618
log.Printf("forwarding to %s got err: %s", addr, err)
1719
return nil
@@ -72,11 +74,17 @@ func main() {
7274
Description: "If sent the query will be sent to all addresses in parallel",
7375
Flag: true,
7476
})
77+
l.Add(lever.Param{
78+
Name: "--timeout",
79+
Description: "Timeout in milliseconds for each request",
80+
Default: "300",
81+
})
7582
l.Parse()
7683

7784
addr, _ := l.ParamStr("--listen-addr")
7885
dnsServers, _ := l.ParamStrs("--fwd-to")
7986
combineGroups := l.ParamFlag("--parallel")
87+
timeout, _ := l.ParamInt("--timeout")
8088

8189
if combineGroups {
8290
//combine all the servers sent into one group
@@ -93,6 +101,14 @@ func main() {
93101
}
94102
}
95103

104+
client = dns.Client{
105+
//since this is UDP, the Dial/Write timeouts don't mean much
106+
//we really only care about setting the read
107+
DialTimeout: time.Millisecond * 100,
108+
WriteTimeout: time.Millisecond * 100,
109+
ReadTimeout: time.Millisecond * time.Duration(timeout),
110+
}
111+
96112
handler := dns.HandlerFunc(handleRequest)
97113
go func() {
98114
log.Printf("Listening on %s (udp)", addr)

0 commit comments

Comments
 (0)