mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-28 10:51:44 +01:00 
			
		
		
		
	Improve signal handling
This commit starts to wire up better signal handling, it starts with handling shutdown a bit better, using the graceful shutdown for all the listeners we use. It also adds the initial switch case for handling config and acl reload, which is to be implemented.
This commit is contained in:
		
							parent
							
								
									b472e5a689
								
							
						
					
					
						commit
						764ed9120c
					
				
							
								
								
									
										55
									
								
								app.go
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								app.go
									
									
									
									
									
								
							| @ -567,19 +567,6 @@ func (h *Headscale) Serve() error { | ||||
| 		return fmt.Errorf("failed change permission of gRPC socket: %w", err) | ||||
| 	} | ||||
| 
 | ||||
| 	// Handle common process-killing signals so we can gracefully shut down:
 | ||||
| 	sigc := make(chan os.Signal, 1) | ||||
| 	signal.Notify(sigc, os.Interrupt, 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) | ||||
| 
 | ||||
| 	grpcGatewayMux := runtime.NewServeMux() | ||||
| 
 | ||||
| 	// Make the grpc-gateway connect to grpc over socket
 | ||||
| @ -724,6 +711,48 @@ func (h *Headscale) Serve() error { | ||||
| 	log.Info(). | ||||
| 		Msgf("listening and serving metrics on: %s", h.cfg.MetricsAddr) | ||||
| 
 | ||||
| 	// Handle common process-killing signals so we can gracefully shut down:
 | ||||
| 	sigc := make(chan os.Signal, 1) | ||||
| 	signal.Notify(sigc, | ||||
| 		syscall.SIGHUP, | ||||
| 		syscall.SIGINT, | ||||
| 		syscall.SIGTERM, | ||||
| 		syscall.SIGQUIT, | ||||
| 		syscall.SIGHUP) | ||||
| 	go func(c chan os.Signal) { | ||||
| 		// Wait for a SIGINT or SIGKILL:
 | ||||
| 		sig := <-c | ||||
| 		switch sig { | ||||
| 		case syscall.SIGHUP: | ||||
| 			log.Info(). | ||||
| 				Str("signal", sig.String()). | ||||
| 				Msg("Received SIGHUP, reloading ACL and Config") | ||||
| 
 | ||||
| 		// TODO(kradalby): Reload config on SIGHUP
 | ||||
| 
 | ||||
| 		default: | ||||
| 			log.Info(). | ||||
| 				Str("signal", sig.String()). | ||||
| 				Msg("Received signal to stop, shutting down gracefully") | ||||
| 
 | ||||
| 			// Gracefully shut down servers
 | ||||
| 			promHTTPServer.Shutdown(ctx) | ||||
| 			httpServer.Shutdown(ctx) | ||||
| 			grpcSocket.GracefulStop() | ||||
| 
 | ||||
| 			// Close network listeners
 | ||||
| 			promHTTPListener.Close() | ||||
| 			httpListener.Close() | ||||
| 			grpcGatewayConn.Close() | ||||
| 
 | ||||
| 			// Stop listening (and unlink the socket if unix type):
 | ||||
| 			socketListener.Close() | ||||
| 
 | ||||
| 			// And we're done:
 | ||||
| 			os.Exit(0) | ||||
| 		} | ||||
| 	}(sigc) | ||||
| 
 | ||||
| 	return errorGroup.Wait() | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user