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-09 18:39:30 +07:00
committed by Cuong Manh Le
parent 4769da4ef4
commit f499770d45
+4 -5
View File
@@ -752,20 +752,19 @@ func runDNSServer(addr, network string, handler dns.Handler) (*dns.Server, <-cha
Handler: handler, Handler: handler,
} }
waitLock := sync.Mutex{} startedCh := make(chan struct{})
waitLock.Lock() s.NotifyStartedFunc = func() { sync.OnceFunc(func() { close(startedCh) })() }
s.NotifyStartedFunc = waitLock.Unlock
errCh := make(chan error) errCh := make(chan error)
go func() { go func() {
defer close(errCh) defer close(errCh)
if err := s.ListenAndServe(); err != nil { 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) mainLog.Load().Error().Err(err).Msgf("could not listen and serve on: %s", s.Addr)
errCh <- err errCh <- err
} }
}() }()
waitLock.Lock() <-startedCh
return s, errCh return s, errCh
} }