From d71d1341b61b3cd183f9d894100985637f10a79a Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Mon, 11 Aug 2025 17:07:59 +0700 Subject: [PATCH] refactor(prog): move network monitoring outside listener loop Move the network monitoring goroutine initialization outside the listener loop to prevent it from being started multiple times. Previously, the network monitoring was started once per listener during first run, which was unnecessary and could lead to multiple monitoring instances. The change ensures network monitoring is started only once per program execution cycle, improving efficiency and preventing potential resource waste from duplicate monitoring goroutines. - Extract network monitoring goroutine from listener loop - Start network monitoring once per run cycle instead of per listener - Maintain same functionality while improving resource usage --- cmd/cli/prog.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/cli/prog.go b/cmd/cli/prog.go index fd36620..76f7c36 100644 --- a/cmd/cli/prog.go +++ b/cmd/cli/prog.go @@ -530,15 +530,18 @@ func (p *prog) run(reload bool, reloadCh chan struct{}) { go p.watchLinkState(ctx) } + if !reload { + go func() { + // Start network monitoring + if err := p.monitorNetworkChanges(); err != nil { + mainLog.Load().Error().Err(err).Msg("Failed to start network monitoring") + } + }() + } + for listenerNum := range p.cfg.Listener { p.cfg.Listener[listenerNum].Init() if !reload { - go func() { - // Start network monitoring - if err := p.monitorNetworkChanges(); err != nil { - mainLog.Load().Error().Err(err).Msg("Failed to start network monitoring") - } - }() go func(listenerNum string) { listenerConfig := p.cfg.Listener[listenerNum] upstreamConfig := p.cfg.Upstream[listenerNum]