Created
October 4, 2025 17:38
-
-
Save george124816/b01213190c20c6a995d8b9f811cb0f39 to your computer and use it in GitHub Desktop.
Graceful Shutdown in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "log/slog" | |
| "net/http" | |
| "os" | |
| "os/signal" | |
| "syscall" | |
| "time" | |
| ) | |
| func main() { | |
| slog.Info(fmt.Sprintf("starting app on pid=%d\n", os.Getpid())) | |
| // create a channel to receive a os.Signal (type) with size of 1 | |
| sigs := make(chan os.Signal, 1) | |
| // send to channel this signals | |
| signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP) | |
| // starting the server in a go func | |
| srv := startServer() | |
| // app will wait here, waiting to receive a channel | |
| sig := <-sigs | |
| slog.Info(fmt.Sprintf("received signal=%s\n", sig)) | |
| // create a context with timeout to wait server shutdown | |
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | |
| defer cancel() | |
| err := srv.Shutdown(ctx) | |
| if err != nil { | |
| slog.Error(err.Error()) | |
| } | |
| } | |
| func startServer() *http.Server { | |
| mux := http.NewServeMux() | |
| mux.HandleFunc("/ping", PongHandler) | |
| srv := &http.Server{Addr: ":4000", Handler: mux} | |
| go func() { | |
| err := srv.ListenAndServe() | |
| slog.Info(fmt.Sprintf("server returned this error=%s\n", err)) | |
| }() | |
| return srv | |
| } | |
| func PongHandler(response http.ResponseWriter, request *http.Request) { | |
| // a sleep to given you time to send signal | |
| time.Sleep(5 * time.Second) | |
| fmt.Fprintln(response, "pong") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment