Skip to content

Instantly share code, notes, and snippets.

@MilosSimic
Forked from kpurdon/main.go
Created April 17, 2021 22:34
Show Gist options
  • Save MilosSimic/497fd8015f51e1abaa430d3e79f24dcb to your computer and use it in GitHub Desktop.
Save MilosSimic/497fd8015f51e1abaa430d3e79f24dcb to your computer and use it in GitHub Desktop.
golang graceful server shutdown example
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
)
const (
service = "fooapi"
)
var (
port = flag.Int("port", 3333, "http port to listen on")
shutdownTimeout = flag.Duration("shutdown-timeout", 10*time.Second,
"shutdown timeout (5s,5m,5h) before connections are cancelled")
)
func main() {
flag.Parse()
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
srv := &http.Server{
Addr: fmt.Sprintf(":%d", *port),
Handler: mux,
}
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
go func() {
log.Printf("%s listening on 0.0.0.0:%d with %v timeout", service, *port, *shutdownTimeout)
if err := srv.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Fatal(err)
}
}
}()
<-stop
log.Printf("%s shutting down ...\n", service)
ctx, cancel := context.WithTimeout(context.Background(), *shutdownTimeout)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal(err)
}
log.Printf("%s down\n", service)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment