Documentation ¶
Overview ¶
Deprecated: This package is no longer maintained.
Package graceful simplifies graceful shutdown of HTTP servers (Go 1.8+).
Installation ¶
Just go get the package:
go get -u github.com/TV4/graceful
Usage ¶
A small usage example
package main import ( "context" "log" "net/http" "os" "time" "github.com/TV4/graceful" ) type server struct { logger *log.Logger } func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { time.Sleep(5 * time.Second) w.Write([]byte("Hello!")) } func (s *server) Shutdown(ctx context.Context) error { time.Sleep(2 * time.Second) s.logger.Println("Shutdown finished") return nil } func main() { graceful.LogListenAndServe(setup(":2017")) } func setup(addr string) (*http.Server, *log.Logger) { s := &server{logger: log.New(os.Stdout, "", 0)} return &http.Server{Addr: addr, Handler: s}, s.logger }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ListeningFormat = "Listening on http://%s\n" ShutdownFormat = "\nServer shutdown with timeout: %s\n" ErrorFormat = "Error: %v\n" FinishedFormat = "Shutdown finished %ds before deadline\n" FinishedHTTP = "Finished all in-flight HTTP requests\n" HandlerShutdownFormat = "Shutting down handler with timeout: %ds\n" )
Format strings used by the logger
var Timeout = 15 * time.Second
Timeout for context used in call to *http.Server.Shutdown
Functions ¶
func ListenAndServe ¶
func ListenAndServe(s Server)
ListenAndServe starts the server in a goroutine and then calls Shutdown
func ListenAndServeTLS ¶ added in v0.3.0
ListenAndServeTLS starts the server in a goroutine and then calls Shutdown
func LogListenAndServe ¶
LogListenAndServe logs using the logger and then calls ListenAndServe
func Shutdown ¶
func Shutdown(s Shutdowner)
Shutdown blocks until os.Interrupt or syscall.SIGTERM received, then running *http.Server.Shutdown with a context having a timeout
Types ¶
type Logger ¶
type Logger interface { Printf(format string, v ...interface{}) Fatal(...interface{}) }
Logger is implemented by *log.Logger
type Server ¶
type Server interface { ListenAndServe() error Shutdowner }
Server is implemented by *http.Server
type Shutdowner ¶
Shutdowner is implemented by *http.Server, and optionally by *http.Server.Handler