refactor: move network monitoring to separate goroutine

- Move network monitoring initialization out of serveDNS() function
- Start network monitoring in a separate goroutine during program startup
- Remove context parameter from monitorNetworkChanges() as it's not used
- Simplify serveDNS() function signature by removing unused context parameter
- Ensure network monitoring starts only once during initial run, not on reload

This change improves separation of concerns by isolating network monitoring
from DNS serving logic, and prevents potential issues with multiple
monitoring goroutines if starting multiple listeners.
This commit is contained in:
Cuong Manh Le
2025-08-01 18:55:07 +07:00
committed by Cuong Manh Le
parent 1ff5d1f05a
commit 0cd873a88f
2 changed files with 6 additions and 6 deletions

View File

@@ -81,13 +81,7 @@ type upstreamForResult struct {
srcAddr string
}
// serveDNS sets up and starts a DNS server on the specified listener, handling DNS queries and network monitoring.
func (p *prog) serveDNS(mainCtx context.Context, listenerNum string) error {
if err := p.monitorNetworkChanges(mainCtx); err != nil {
p.Error().Err(err).Msg("Failed to start network monitoring")
// Don't return here as we still want DNS service to run
}
listenerConfig := p.cfg.Listener[listenerNum]
if allocErr := p.allocateIP(listenerConfig.IP); allocErr != nil {
p.Error().Err(allocErr).Str("ip", listenerConfig.IP).Msg("serveUDP: failed to allocate listen ip")

View File

@@ -514,6 +514,12 @@ func (p *prog) run(reload bool, reloadCh chan struct{}) {
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]