go-opslisted
Install: claude install-skill AllThingsSmitty/agent-skills
## Graceful Shutdown
Never use `log.Fatal(http.ListenAndServe(...))`. It makes your service unkillable gracefully — Kubernetes sends SIGTERM and your pods drop in-flight requests.
The correct pattern uses `signal.NotifyContext` (Go 1.16+) or `signal.Notify` with a channel:
```go
package main
import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
srv := &http.Server{
Addr: ":8080",
Handler: routes(),
}
// Catch SIGINT and SIGTERM
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
// Start serving in a goroutine
go func() {
slog.Info("server starting", "addr", srv.Addr)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("server error", "err", err)
os.Exit(1)
}
}()
// Block until signal received
<-ctx.Done()
slog.Info("shutdown signal received")
// Give in-flight requests up to 30 seconds to complete
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
slog.Error("shutdown error", "err", err)
os.Exit(1)
}
slog.Info("server stopped cleanly")
}
```
Key rules:
- Use `Shutdown(ctx)` not `Close()` — `Shutdown` waits for in-flight requests; `Close` drops them.
- Al