mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-09 16:20:26 +00:00
Improve shutdown behaviour (fixes #891)
This commit is contained in:
parent
ee21c56e43
commit
69782ad87b
@ -14,6 +14,7 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"golang.org/x/text/encoding/unicode"
|
"golang.org/x/text/encoding/unicode"
|
||||||
@ -183,8 +184,7 @@ func getArgs() yggArgs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The main function is responsible for configuring and starting Yggdrasil.
|
// The main function is responsible for configuring and starting Yggdrasil.
|
||||||
func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
func run(args yggArgs, ctx context.Context) {
|
||||||
defer close(done)
|
|
||||||
// Create a new logger that logs output to stdout.
|
// Create a new logger that logs output to stdout.
|
||||||
var logger *log.Logger
|
var logger *log.Logger
|
||||||
switch args.logto {
|
switch args.logto {
|
||||||
@ -371,14 +371,11 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
|||||||
logger.Infof("Your public key is %s", hex.EncodeToString(public[:]))
|
logger.Infof("Your public key is %s", hex.EncodeToString(public[:]))
|
||||||
logger.Infof("Your IPv6 address is %s", address.String())
|
logger.Infof("Your IPv6 address is %s", address.String())
|
||||||
logger.Infof("Your IPv6 subnet is %s", subnet.String())
|
logger.Infof("Your IPv6 subnet is %s", subnet.String())
|
||||||
// Catch interrupts from the operating system to exit gracefully.
|
|
||||||
<-ctx.Done()
|
|
||||||
// Capture the service being stopped on Windows.
|
|
||||||
minwinsvc.SetOnExit(n.shutdown)
|
|
||||||
n.shutdown()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *node) shutdown() {
|
// Block until we are told to shut down.
|
||||||
|
<-ctx.Done()
|
||||||
|
|
||||||
|
// Shut down the node.
|
||||||
_ = n.admin.Stop()
|
_ = n.admin.Stop()
|
||||||
_ = n.multicast.Stop()
|
_ = n.multicast.Stop()
|
||||||
_ = n.tun.Stop()
|
_ = n.tun.Stop()
|
||||||
@ -387,24 +384,19 @@ func (n *node) shutdown() {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
args := getArgs()
|
args := getArgs()
|
||||||
hup := make(chan os.Signal, 1)
|
|
||||||
//signal.Notify(hup, os.Interrupt, syscall.SIGHUP)
|
// Catch interrupts from the operating system to exit gracefully.
|
||||||
term := make(chan os.Signal, 1)
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
|
|
||||||
for {
|
// Capture the service being stopped on Windows.
|
||||||
done := make(chan struct{})
|
minwinsvc.SetOnExit(cancel)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
go run(args, ctx, done)
|
// Start the node, block and then wait for it to shut down.
|
||||||
select {
|
var wg sync.WaitGroup
|
||||||
case <-hup:
|
wg.Add(1)
|
||||||
cancel()
|
go func() {
|
||||||
<-done
|
defer wg.Done()
|
||||||
case <-term:
|
run(args, ctx)
|
||||||
cancel()
|
}()
|
||||||
<-done
|
wg.Wait()
|
||||||
return
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user