From 3ea69b180cbdf23a53b232f3c9722177bd502154 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 8 Jan 2025 16:57:32 +0700 Subject: [PATCH] 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. --- cmd/cli/upstream_monitor.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/cli/upstream_monitor.go b/cmd/cli/upstream_monitor.go index b17cb32..4d79c9f 100644 --- a/cmd/cli/upstream_monitor.go +++ b/cmd/cli/upstream_monitor.go @@ -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) }