Skip to content

Commit

Permalink
Use a signal to close the longpolls on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfont committed Jun 23, 2022
1 parent d404ba1 commit 39b58f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type Headscale struct {
registrationCache *cache.Cache

ipAllocationMutex sync.Mutex

shutdownChan chan struct{}
}

// Look up the TLS constant relative to user-supplied TLS client
Expand Down Expand Up @@ -642,6 +644,7 @@ func (h *Headscale) Serve() error {
Msgf("listening and serving metrics on: %s", h.cfg.MetricsAddr)

// Handle common process-killing signals so we can gracefully shut down:
h.shutdownChan = make(chan struct{})
sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP,
Expand Down Expand Up @@ -679,6 +682,8 @@ func (h *Headscale) Serve() error {
Str("signal", sig.String()).
Msg("Received signal to stop, shutting down gracefully")

h.shutdownChan <- struct{}{}

// Gracefully shut down servers
promHTTPServer.Shutdown(ctx)
httpServer.Shutdown(ctx)
Expand Down
7 changes: 7 additions & 0 deletions poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,13 @@ func (h *Headscale) PollNetMapStream(

// The connection has been closed, so we can stop polling.
return

case <-h.shutdownChan:
log.Info().
Str("handler", "PollNetMapStream").
Str("machine", machine.Hostname).
Msg("The long-poll handler is shutting down")
return
}
}
}
Expand Down

0 comments on commit 39b58f7

Please sign in to comment.