Skip to content

Commit

Permalink
use sync.WaitGroup
Browse files Browse the repository at this point in the history
Less handrolling, more idiomatic and easier to extend if need be.
  • Loading branch information
klemensn committed Nov 3, 2024
1 parent 83c3563 commit 6ae62f0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 5 additions & 3 deletions cmd/yggdrasil/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func main() {
}
if n.admin != nil {
n.admin.SetupAdminHandlers()
n.admin.Created.Add(1)
}
}

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"net/url"
"os"
"sort"

"strings"
"sync"
"time"

"github.com/yggdrasil-network/yggdrasil-go/src/core"
Expand All @@ -26,7 +26,7 @@ type AdminSocket struct {
config struct {
listenaddr ListenAddress
}
Created bool
Created sync.WaitGroup
}

type AdminSocketRequest struct {
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 6ae62f0

Please sign in to comment.