healthcheck: answer 503 when no root server gave a usable reply in the last round of DNS checks (closes #302)
check / check (push) Waiting to run

The health check answered ok while every DNS lookup failed, as on a
network that intercepts DNS queries. A lookup whose root servers give no
usable reply now fails with ErrNoRootServerAnswered. At the end of each
round of DNS checks that shutdown did not cut short, the watcher records
whether every lookup failed with it or with ErrIntercepted; the health
check then gives status error, and /.well-known/healthcheck and /health
answer 503. Until the first round finishes it stays ok.

The health check now depends on the watcher, so fx starts the watcher
before the HTTP server and stops the server first. NewWithRootServers
lets tests start a resolver's walks at real servers that are not root
servers.

Model: opus-5-5
This commit was merged in pull request #304.
This commit is contained in:
2026-10-18 17:24:49 +02:00
parent b23cf43abf
commit 774f7caff6
14 changed files with 534 additions and 45 deletions
+55 -9
View File
@@ -11,6 +11,7 @@ import (
"sort"
"strconv"
"strings"
"sync/atomic"
"time"
"go.uber.org/fx"
@@ -62,6 +63,11 @@ type Watcher struct {
cancel context.CancelFunc
done chan struct{} // closed when Run returns
firstRun bool
// dnsUnreachable is what DNSUnreachable reports. Each round of DNS
// checks sets it, and the HTTP server reads it from its own
// goroutines.
dnsUnreachable atomic.Bool
}
// New creates a new Watcher instance wired into the fx lifecycle.
@@ -197,6 +203,18 @@ func (w *Watcher) RunOnce(ctx context.Context) {
w.firstRun = false
}
// DNSUnreachable reports whether the most recent round of DNS checks
// could not reach the DNS at all: every lookup in it failed because no
// root server gave a usable reply, as when none of them answered or the
// network intercepts DNS queries. A round reached the DNS even when a
// name's own nameservers did not answer or a domain does not exist,
// since the root servers gave a usable reply to those lookups. It is
// false until the first round finishes, and a round that shutdown cut
// short changes nothing. It may be called while a round runs.
func (w *Watcher) DNSUnreachable() bool {
return w.dnsUnreachable.Load()
}
func (w *Watcher) detectFirstRun() {
snap := w.state.GetSnapshot()
hasState := len(snap.Domains) > 0 ||
@@ -265,15 +283,40 @@ func (w *Watcher) cleanupRemovedTargets() {
// runDNSChecks performs DNS resolution for all configured domains
// and hostnames, updating state with freshly resolved records.
// This must complete before port or TLS checks run so those
// checks operate on current IP addresses.
// checks operate on current IP addresses. Unless ctx was cut short, it
// then records whether this round could reach the DNS (see
// DNSUnreachable).
func (w *Watcher) runDNSChecks(ctx context.Context) {
errs := make([]error, 0, len(w.config.Domains)+len(w.config.Hostnames))
for _, domain := range w.config.Domains {
w.checkDomain(ctx, domain)
errs = append(errs, w.checkDomain(ctx, domain))
}
for _, hostname := range w.config.Hostnames {
w.checkHostname(ctx, hostname)
_, err := w.checkHostname(ctx, hostname)
errs = append(errs, err)
}
if ctx.Err() == nil {
w.dnsUnreachable.Store(noRootServerGaveUsableReply(errs))
}
}
// noRootServerGaveUsableReply reports whether errs, the errors of the
// lookups in a round of DNS checks, nil for a lookup that succeeded,
// show that every lookup failed because no root server gave a usable
// reply: each is ErrNoRootServerAnswered or ErrIntercepted. A round with
// no lookups shows nothing.
func noRootServerGaveUsableReply(errs []error) bool {
for _, err := range errs {
if !errors.Is(err, resolver.ErrNoRootServerAnswered) &&
!errors.Is(err, resolver.ErrIntercepted) {
return false
}
}
return len(errs) > 0
}
// logFailedLookup logs a failed DNS lookup at error level, unless ctx
@@ -292,14 +335,16 @@ func (w *Watcher) logFailedLookup(
}
// checkDomain looks up domain's delegation and checks domain with it
// (see checkDomainResult).
// (see checkDomainResult). It returns the lookup's error.
func (w *Watcher) checkDomain(
ctx context.Context,
domain string,
) {
) error {
nameservers, err := w.resolver.LookupNS(ctx, domain)
w.checkDomainResult(ctx, domain, nameservers, err)
return err
}
// checkDomainResult finishes a check of domain from nameservers and err,
@@ -358,7 +403,7 @@ func (w *Watcher) checkDomainResult(
// check, as detectNSDisappearances needs.
if nxdomain {
w.state.DeleteHostnameState(domain)
} else if !w.checkHostname(ctx, domain) {
} else if saved, _ := w.checkHostname(ctx, domain); !saved {
return
}
@@ -484,11 +529,12 @@ func (w *Watcher) detectNSAddressChanges(
}
// checkHostname looks up hostname's records and checks them (see
// checkHostnameResult). It reports whether it saved them.
// checkHostnameResult). It reports whether it saved them, and returns
// the lookup's error.
func (w *Watcher) checkHostname(
ctx context.Context,
hostname string,
) bool {
) (bool, error) {
results, err := w.resolver.LookupAllRecords(ctx, hostname)
if err != nil {
w.logFailedLookup(
@@ -499,7 +545,7 @@ func (w *Watcher) checkHostname(
)
}
return w.checkHostnameResult(ctx, hostname, results, err)
return w.checkHostnameResult(ctx, hostname, results, err), err
}
// checkHostnameResult finishes a check of hostname from results and err,