fix leak_on_upstream_failure config param

This commit is contained in:
Alex
2025-02-18 20:22:33 +07:00
committed by Cuong Manh Le
parent a670708f93
commit e6de78c1fa
2 changed files with 17 additions and 8 deletions
+4 -2
View File
@@ -26,6 +26,7 @@ import (
"github.com/Control-D-Inc/ctrld/internal/controld" "github.com/Control-D-Inc/ctrld/internal/controld"
"github.com/Control-D-Inc/ctrld/internal/dnscache" "github.com/Control-D-Inc/ctrld/internal/dnscache"
ctrldnet "github.com/Control-D-Inc/ctrld/internal/net" ctrldnet "github.com/Control-D-Inc/ctrld/internal/net"
"github.com/Control-D-Inc/ctrld/internal/router"
) )
const ( const (
@@ -620,7 +621,7 @@ func (p *prog) proxy(ctx context.Context, req *proxyRequest) *proxyResponse {
ctrld.Log(ctx, mainLog.Load().Error(), "all %v endpoints failed", upstreams) ctrld.Log(ctx, mainLog.Load().Error(), "all %v endpoints failed", upstreams)
// if we have no healthy upstreams, trigger recovery flow // if we have no healthy upstreams, trigger recovery flow
if p.recoverOnUpstreamFailure() { if p.leakOnUpstreamFailure() {
if p.um.countHealthy(upstreams) == 0 { if p.um.countHealthy(upstreams) == 0 {
p.recoveryCancelMu.Lock() p.recoveryCancelMu.Lock()
if p.recoveryCancel == nil { if p.recoveryCancel == nil {
@@ -1306,7 +1307,8 @@ func (p *prog) monitorNetworkChanges(ctx context.Context) error {
} }
mainLog.Load().Debug().Msgf("Set default local IPv4: %s, IPv6: %s", selfIP, ipv6) mainLog.Load().Debug().Msgf("Set default local IPv4: %s, IPv6: %s", selfIP, ipv6)
if p.recoverOnUpstreamFailure() { // we only trigger recovery flow for network changes on non router devices
if router.Name() == "" {
p.handleRecovery(RecoveryReasonNetworkChange) p.handleRecovery(RecoveryReasonNetworkChange)
} }
}) })
+13 -6
View File
@@ -985,12 +985,6 @@ func findWorkingInterface(currentIface string) string {
return currentIface return currentIface
} }
// recoverOnUpstreamFailure reports whether ctrld should recover from upstream failure.
func (p *prog) recoverOnUpstreamFailure() bool {
// Default is false on routers, since this recovery flow is only useful for devices that move between networks.
return router.Name() == ""
}
func randomLocalIP() string { func randomLocalIP() string {
n := rand.Intn(254-2) + 2 n := rand.Intn(254-2) + 2
return fmt.Sprintf("127.0.0.%d", n) return fmt.Sprintf("127.0.0.%d", n)
@@ -1286,3 +1280,16 @@ func selfUninstallCheck(uninstallErr error, p *prog, logger zerolog.Logger) {
selfUninstall(p, logger) selfUninstall(p, logger)
} }
} }
// leakOnUpstreamFailure reports whether ctrld should initiate a recovery flow
// when upstream failures occur.
func (p *prog) leakOnUpstreamFailure() bool {
if ptr := p.cfg.Service.LeakOnUpstreamFailure; ptr != nil {
return *ptr
}
// Default is false on routers, since this leaking is only useful for devices that move between networks.
if router.Name() != "" {
return false
}
return true
}