SoFunction
Updated on 2025-05-12

Detailed explanation of the example of Go language implementing elegant shutdown and restart

In Go, achieving elegant shutdowns and restarts often involve processing system signals (such as SIGINT and SIGTERM) and ensuring that all necessary cleaning work is done before shutting down. Here is an example showing how to use and os/signal packages to achieve elegant shutdown and restart.

The realization of elegant shutdown

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	// Create an HTTP server	server := &{Addr: ":8080", Handler: }

	// Register a simple route	("/", func(w , r *) {
		(w, "Hello, World!")
	})

	// Start the server	go func() {
		if err := (); err != nil && err !=  {
			("ListenAndServe error: %v", err)
		}
	}()

	// Create a channel to receive system signals	quit := make(chan , 1)
	(quit, , )

	// Wait for signal	<-quit
	("Shutting down server...")

	// Create a context with a timeout	ctx, cancel := ((), 5*)
	defer cancel()

	// Close the server gracefully	if err := (ctx); err != nil {
		("Server forced to shutdown: %v", err)
	}

	("Server exiting")
}

The implementation of elegant restart

Elegant restarts usually involve starting a new server instance after receiving a signal and closing the old one. Here is a simple example:

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/exec"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	// Create an HTTP server	server := &{Addr: ":8080", Handler: }

	// Register a simple route	("/", func(w , r *) {
		(w, "Hello, World!")
	})

	// Start the server	go func() {
		if err := (); err != nil && err !=  {
			("ListenAndServe error: %v", err)
		}
	}()

	// Create a channel to receive system signals	quit := make(chan , 1)
	(quit, syscall.SIGUSR2)

	// Wait for signal	<-quit
	("Restarting server...")

	// Create a context with a timeout	ctx, cancel := ((), 5*)
	defer cancel()

	// Close the server gracefully	if err := (ctx); err != nil {
		("Server forced to shutdown: %v", err)
	}

	// Start a new server instance	cmd := ([0], [1:]...)
	 = 
	 = 
	if err := (); err != nil {
		("Failed to restart server: %v", err)
	}

	("Server restarted")
}

illustrate

Elegant shutdown: When receiving SIGINT or SIGTERM signals, the server is elegantly shut down to ensure that all requests being processed are completed.

Graceful restart: When SIGUSR2 signal is received, the server will start a new instance and close the old instance. This method is often used to update applications without interrupting services.

In this way, it is ensured that during shutdown and restart, the service does not suddenly interrupt, thereby providing a better user experience.

This is the article about the detailed explanation of the examples of Go to achieve elegant shutdown and restart. For more information about elegant shutdown and restart of Go, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!