Skip to content

bsm/shutdown

Repository files navigation

Shutdown

GoDoc License

Wait for servers to terminate gracefully.

Example:

import (
	"context"
	"log"
	"net/http"
	"time"

	"github.com/bsm/shutdown"
)

func main() {
	srv := &http.Server{
		Addr:		":8080",
		Handler:	http.FileServer(http.Dir("/usr/share/doc")),
	}

	// Wait for either SIGINT/SIGTERM or ListenAndServe to exit.
	// Handle errors.
	err := shutdown.Wait(srv.ListenAndServe)
	if err != nil && err != http.ErrServerClosed {
		log.Fatalln("Server error", err)
	}

	// Perform a graceful server shutdown.
	log.Println("Shutting down ...")
	timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := srv.Shutdown(timeoutCtx); err != nil {
		log.Println("Shutdown error", err)
	}
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published