cmd/cli: new flow for leaking queries to OS resolver

The current flow involves marking OS resolver as down, which is not
right at all, since ctrld depends on it for leaking queries.

This commits implements new flow, which ctrld will restore DNS settings
once leaking marked, allowing queries go to OS resolver until the
internet connection is established.
This commit is contained in:
Cuong Manh Le
2025-01-15 19:51:55 +07:00
committed by Cuong Manh Le
parent f986a575e8
commit 89600f6091
4 changed files with 20 additions and 29 deletions
+4 -22
View File
@@ -44,6 +44,10 @@ func newUpstreamMonitor(cfg *ctrld.Config) *upstreamMonitor {
// increaseFailureCount increase failed queries count for an upstream by 1.
func (um *upstreamMonitor) increaseFailureCount(upstream string) {
// Do not count "upstream.os", since it must not be down for leaking queries.
if upstream == upstreamOS {
return
}
um.mu.Lock()
defer um.mu.Unlock()
@@ -60,14 +64,6 @@ func (um *upstreamMonitor) isDown(upstream string) bool {
return um.down[upstream]
}
// isChecking reports whether the given upstream is being checked.
func (um *upstreamMonitor) isChecking(upstream string) bool {
um.mu.Lock()
defer um.mu.Unlock()
return um.checking[upstream]
}
// reset marks an upstream as up and set failed queries counter to zero.
func (um *upstreamMonitor) reset(upstream string) {
um.mu.Lock()
@@ -94,11 +90,6 @@ func (p *prog) checkUpstream(upstream string, uc *ctrld.UpstreamConfig) {
p.um.mu.Unlock()
}()
isOsResolver := uc.Type == ctrld.ResolverTypeOS
if isOsResolver {
p.resetDNS()
defer p.setDNS()
}
resolver, err := ctrld.NewResolver(uc)
if err != nil {
mainLog.Load().Warn().Err(err).Msg("could not check upstream")
@@ -114,9 +105,6 @@ func (p *prog) checkUpstream(upstream string, uc *ctrld.UpstreamConfig) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
uc.ReBootstrap()
if isOsResolver {
ctrld.InitializeOsResolver()
}
_, err := resolver.Resolve(ctx, msg)
return err
}
@@ -129,12 +117,6 @@ func (p *prog) checkUpstream(upstream string, uc *ctrld.UpstreamConfig) {
if err := check(); err == nil {
mainLog.Load().Warn().Msgf("upstream %q is online", endpoint)
p.um.reset(upstream)
if p.leakingQuery.CompareAndSwap(true, false) {
p.leakingQueryMu.Lock()
p.leakingQueryWasRun = false
p.leakingQueryMu.Unlock()
mainLog.Load().Warn().Msg("stop leaking query")
}
return
} else {
mainLog.Load().Debug().Msgf("checked upstream %q failed: %v", endpoint, err)