Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Last active May 30, 2025 08:00
Show Gist options
  • Save wjkoh/f5cfd7d7a7a1dad558d0b0cf7924deb6 to your computer and use it in GitHub Desktop.
Save wjkoh/f5cfd7d7a7a1dad558d0b0cf7924deb6 to your computer and use it in GitHub Desktop.
Go: Graceful HTTP Server Shutdown
package main
import (
"context"
"log"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
if err := run(ctx); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
host := ""
port := "8080"
root := http.NewServeMux()
s := &http.Server{
Addr: net.JoinHostPort(host, port),
Handler: root,
// BaseContext: func(_ net.Listener) context.Context { return ctx },
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
slog.Info("Shutting down...")
if err := s.Shutdown(ctx); err != nil {
slog.Error("Shutdown failed", "err", err)
}
}()
listenAndServe := func() <-chan error {
err := make(chan error, 1)
go func() { err <- s.ListenAndServe() }()
return err
}
select {
case err := <-listenAndServe():
return err
case <-ctx.Done():
return ctx.Err()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment