cmd/cli: use config timeout when checking upstream

Otherwise, for slow network connection (like plane wifi), the check may
fail even though the internet is available.
This commit is contained in:
Cuong Manh Le
2025-01-08 16:57:32 +07:00
committed by Cuong Manh Le
parent db6e977e3a
commit 3ea69b180c

View File

@@ -93,9 +93,12 @@ func (p *prog) checkUpstream(upstream string, uc *ctrld.UpstreamConfig) {
}
msg := new(dns.Msg)
msg.SetQuestion(".", dns.TypeNS)
timeout := 1000 * time.Millisecond
if uc.Timeout > 0 {
timeout = time.Duration(uc.Timeout) * time.Millisecond
}
check := func() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
uc.ReBootstrap()
_, err := resolver.Resolve(ctx, msg)
@@ -112,6 +115,8 @@ func (p *prog) checkUpstream(upstream string, uc *ctrld.UpstreamConfig) {
mainLog.Load().Warn().Msg("stop leaking query")
}
return
} else {
mainLog.Load().Debug().Msgf("upstream %q is offline: %v", uc.Endpoint, err)
}
time.Sleep(checkUpstreamBackoffSleep)
}