Add handling for closing the socket

This commit is contained in:
Kristoffer Dalby 2021-11-02 21:46:15 +00:00
parent b91c115ade
commit 9954a3c599
No known key found for this signature in database
GPG Key ID: 09F62DC067465735

15
app.go
View File

@ -10,9 +10,11 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"os/signal"
"sort" "sort"
"strings" "strings"
"sync" "sync"
"syscall"
"time" "time"
"github.com/coreos/go-oidc/v3/oidc" "github.com/coreos/go-oidc/v3/oidc"
@ -352,6 +354,19 @@ func (h *Headscale) Serve() error {
panic(err) panic(err)
} }
// Handle common process-killing signals so we can gracefully shut down:
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)
go func(c chan os.Signal) {
// Wait for a SIGINT or SIGKILL:
sig := <-c
log.Printf("Caught signal %s: shutting down.", sig)
// Stop listening (and unlink the socket if unix type):
socketListener.Close()
// And we're done:
os.Exit(0)
}(sigc)
networkListener, err := net.Listen("tcp", h.cfg.Addr) networkListener, err := net.Listen("tcp", h.cfg.Addr)
if err != nil { if err != nil {
panic(err) panic(err)