Skip to content

Commit

Permalink
trace log and notifier shutdown (#1922)
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
  • Loading branch information
kradalby committed May 2, 2024
1 parent 1c6bfc5 commit a9c568c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions hscontrol/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,43 +800,68 @@ func (h *Headscale) Serve() error {
}

default:
trace := log.Trace().Msgf
log.Info().
Str("signal", sig.String()).
Msg("Received signal to stop, shutting down gracefully")

trace("closing map sessions")
wg := sync.WaitGroup{}
for _, mapSess := range h.mapSessions {
wg.Add(1)
go func() {
mapSess.close()
wg.Done()
}()
}
wg.Wait()

trace("waiting for netmap stream to close")
h.pollNetMapStreamWG.Wait()

// Gracefully shut down servers
ctx, cancel := context.WithTimeout(
context.Background(),
types.HTTPShutdownTimeout,
)
trace("shutting down debug http server")
if err := debugHTTPServer.Shutdown(ctx); err != nil {
log.Error().Err(err).Msg("Failed to shutdown prometheus http")
}
trace("shutting down main http server")
if err := httpServer.Shutdown(ctx); err != nil {
log.Error().Err(err).Msg("Failed to shutdown http")
}

trace("shutting down grpc server (socket)")
grpcSocket.GracefulStop()

if grpcServer != nil {
trace("shutting down grpc server (external)")
grpcServer.GracefulStop()
grpcListener.Close()
}

if tailsqlContext != nil {
trace("shutting down tailsql")
tailsqlContext.Done()
}

trace("closing node notifier")
h.nodeNotifier.Close()

// Close network listeners
trace("closing network listeners")
debugHTTPListener.Close()
httpListener.Close()
grpcGatewayConn.Close()

// Stop listening (and unlink the socket if unix type):
trace("closing socket listener")
socketListener.Close()

// Close db connections
trace("closing database connection")
err = h.db.Close()
if err != nil {
log.Error().Err(err).Msg("Failed to close db")
Expand Down
5 changes: 5 additions & 0 deletions hscontrol/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func NewNotifier(cfg *types.Config) *Notifier {
return n
}

// Close stops the batcher inside the notifier.
func (n *Notifier) Close() {
n.b.close()
}

func (n *Notifier) AddNode(nodeID types.NodeID, c chan<- types.StateUpdate) {
log.Trace().Caller().Uint64("node.id", nodeID.Uint64()).Msg("acquiring lock to add node")
defer log.Trace().
Expand Down

0 comments on commit a9c568c

Please sign in to comment.