From f499770d450cf5e9a8ef71b508164111e15deba0 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Mon, 6 May 2024 14:16:18 +0700 Subject: [PATCH] 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. --- cmd/cli/dns_proxy.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/cli/dns_proxy.go b/cmd/cli/dns_proxy.go index a5242c5..fd8a5cc 100644 --- a/cmd/cli/dns_proxy.go +++ b/cmd/cli/dns_proxy.go @@ -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 }