Skip to content

Commit

Permalink
feat: retry on DNS queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jun 4, 2020
1 parent 0349e40 commit b324723
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions challenge/dns01/nameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sync"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/go-acme/lego/v3/log"
"github.com/miekg/dns"
)

Expand Down Expand Up @@ -229,15 +231,39 @@ func dnsQuery(fqdn string, rtype uint16, nameservers []string, recursive bool) (
m := createDNSMsg(fqdn, rtype, recursive)

var in *dns.Msg
var err error
var errG error

for _, ns := range nameservers {
in, err = sendDNSQuery(m, ns)
if err == nil && len(in.Answer) > 0 {
bo := backoff.NewExponentialBackOff()
bo.Multiplier = 1.2
bo.InitialInterval = dnsTimeout
bo.MaxInterval = 2 * bo.InitialInterval
bo.MaxElapsedTime = 4 * bo.InitialInterval

operation := func() error {
var err error
in, err = sendDNSQuery(m, ns)

// errors from miekg/dns package and some errors from the net package must stop the retry.
var e *dns.Error
if err != nil &&
(strings.Contains(err.Error(), "connection refused") || errors.As(err, &e)) {
return backoff.Permanent(err)
}

return err
}

notify := func(err error, d time.Duration) {
log.Infof("dnsQuery retry %v: fqdn=%s, ns=%s: %v", d, fqdn, ns, err)
}

errG = backoff.RetryNotify(operation, bo, notify)
if errG == nil && len(in.Answer) > 0 {
break
}
}
return in, err
return in, errG
}

func createDNSMsg(fqdn string, rtype uint16, recursive bool) *dns.Msg {
Expand Down

0 comments on commit b324723

Please sign in to comment.