Use a signal to close the longpolls on shutdown

This commit is contained in:
Juan Font Alonso 2022-06-23 19:40:07 +02:00
parent d404ba102d
commit 39b58f7d4c
2 changed files with 12 additions and 0 deletions

5
app.go
View file

@ -93,6 +93,8 @@ type Headscale struct {
registrationCache *cache.Cache registrationCache *cache.Cache
ipAllocationMutex sync.Mutex ipAllocationMutex sync.Mutex
shutdownChan chan struct{}
} }
// Look up the TLS constant relative to user-supplied TLS client // Look up the TLS constant relative to user-supplied TLS client
@ -642,6 +644,7 @@ func (h *Headscale) Serve() error {
Msgf("listening and serving metrics on: %s", h.cfg.MetricsAddr) Msgf("listening and serving metrics on: %s", h.cfg.MetricsAddr)
// Handle common process-killing signals so we can gracefully shut down: // Handle common process-killing signals so we can gracefully shut down:
h.shutdownChan = make(chan struct{})
sigc := make(chan os.Signal, 1) sigc := make(chan os.Signal, 1)
signal.Notify(sigc, signal.Notify(sigc,
syscall.SIGHUP, syscall.SIGHUP,
@ -679,6 +682,8 @@ func (h *Headscale) Serve() error {
Str("signal", sig.String()). Str("signal", sig.String()).
Msg("Received signal to stop, shutting down gracefully") Msg("Received signal to stop, shutting down gracefully")
h.shutdownChan <- struct{}{}
// Gracefully shut down servers // Gracefully shut down servers
promHTTPServer.Shutdown(ctx) promHTTPServer.Shutdown(ctx)
httpServer.Shutdown(ctx) httpServer.Shutdown(ctx)

View file

@ -572,6 +572,13 @@ func (h *Headscale) PollNetMapStream(
// The connection has been closed, so we can stop polling. // The connection has been closed, so we can stop polling.
return return
case <-h.shutdownChan:
log.Info().
Str("handler", "PollNetMapStream").
Str("machine", machine.Hostname).
Msg("The long-poll handler is shutting down")
return
} }
} }
} }