cmd/cli: use channel instead of mutex in runDNSServer

So the code is easier to read/follow, and possible reduce the overhead
of using mutex in low resources system.
This commit is contained in:
Cuong Manh Le
2024-05-06 14:16:18 +07:00
committed by Cuong Manh Le
parent 4769da4ef4
commit f499770d45

View File

@@ -752,20 +752,19 @@ func runDNSServer(addr, network string, handler dns.Handler) (*dns.Server, <-cha
Handler: handler,
}
waitLock := sync.Mutex{}
waitLock.Lock()
s.NotifyStartedFunc = waitLock.Unlock
startedCh := make(chan struct{})
s.NotifyStartedFunc = func() { sync.OnceFunc(func() { close(startedCh) })() }
errCh := make(chan error)
go func() {
defer close(errCh)
if err := s.ListenAndServe(); err != nil {
waitLock.Unlock()
s.NotifyStartedFunc()
mainLog.Load().Error().Err(err).Msgf("could not listen and serve on: %s", s.Addr)
errCh <- err
}
}()
waitLock.Lock()
<-startedCh
return s, errCh
}