From 134561c85a1ab4f860c07dd81a9f7e3aaa02f3e3 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 f7586ab..2b41bc0 100644 --- a/cmd/cli/prog.go +++ b/cmd/cli/prog.go @@ -511,15 +511,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(ctx); err != nil { + p.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(ctx); 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]