mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
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:
committed by
Cuong Manh Le
parent
1ff5d1f05a
commit
0cd873a88f
@@ -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")
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user