Skip to content

Commit

Permalink
Add timeout for dns exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Mar 19, 2019
1 parent 6add7af commit 45ca063
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions dns/dns.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dns

import (
"context"
"net"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -73,17 +74,21 @@ func matchAndServe(w dns.ResponseWriter, r *dns.Msg, domain, listenIP, dnsServer
return
}

msg, err := dns.Exchange(r, dnsServer)
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()

msg, err := dns.ExchangeContext(ctx, r, dnsServer)
if err != nil {
if dhcpCh != nil {
select {
case dhcpCh <- struct{}{}:
default:
}
}
glog.V(1).Infof("get dns of %s fail: %s", domain, err)
return
} else if msg == nil { // expose any response except nil
glog.V(1).Infof("get dns of %s fail: %s", domain, err)
glog.V(1).Infof("get dns of %s return empty", domain)
return
}

Expand Down

0 comments on commit 45ca063

Please sign in to comment.