From 6ae62f0cf9dcf83b9f56f0ec5ca3e026f4f85e87 Mon Sep 17 00:00:00 2001 From: Klemens Nanni Date: Sun, 3 Nov 2024 23:01:18 +0300 Subject: [PATCH] use sync.WaitGroup Less handrolling, more idiomatic and easier to extend if need be. --- cmd/yggdrasil/main.go | 8 +++++--- src/admin/admin.go | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 86aeb8f87..eddaab845 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -235,6 +235,7 @@ func main() { } if n.admin != nil { n.admin.SetupAdminHandlers() + n.admin.Created.Add(1) } } @@ -284,9 +285,10 @@ func main() { // Change user if requested if *chuserto != "" { // Wait for other goroutines to finish potentially privileged tasks before dropping privileges. - doWait := n.admin != nil - // control socket: UNIX requires filesystem permissions, TCP may use a low privileged port. - for ; doWait; doWait = !n.admin.Created {} + if n.admin != nil { + // control socket: UNIX requires filesystem permissions, TCP may use a low privileged port. + n.admin.Created.Wait() + } err = chuser(*chuserto) if err != nil { diff --git a/src/admin/admin.go b/src/admin/admin.go index a9c2a675f..533e194c9 100644 --- a/src/admin/admin.go +++ b/src/admin/admin.go @@ -8,8 +8,8 @@ import ( "net/url" "os" "sort" - "strings" + "sync" "time" "github.com/yggdrasil-network/yggdrasil-go/src/core" @@ -26,7 +26,7 @@ type AdminSocket struct { config struct { listenaddr ListenAddress } - Created bool + Created sync.WaitGroup } type AdminSocketRequest struct { @@ -275,7 +275,7 @@ func (a *AdminSocket) listen() { a.log.Errorf("Admin socket failed to listen: %v", err) os.Exit(1) } - a.Created = true + a.Created.Done() a.log.Infof("%s admin socket listening on %s", strings.ToUpper(a.listener.Addr().Network()), a.listener.Addr().String())