Canonical guidance

Use when

Avoid

Preferred pattern

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

<-ctx.Done()

shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = srv.Shutdown(shutdownCtx)

Anti-pattern

Explanation: This is tempting because the process is technically responding to signals, but it defeats the whole drain strategy.

Why

Related pages

Sources