Documentation ¶
Overview ¶
Package graceful is a Go 1.8+ package enabling graceful shutdown of http.Handler servers.
Usage:
package main import ( "log" "github.com/ETZhangSX/graceful" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() if err := graceful.ListenAndServe(":8080", r.Handler()); err != nil { log.Fatal(err) } }
Index ¶
Constants ¶
const DefaultShutdownTimeout = 5 * time.Second
Variables ¶
This section is empty.
Functions ¶
func ListenAndServe ¶
ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.
The handler is typically nil, in which case the DefaultServeMux is used. ShutdownTimeout defaults 5 seconds.
func ListenAndServeTLS ¶
ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate. ShutdownTimeout defaults 5 seconds.
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option interface
func WithShutdownFunc ¶
func WithShutdownFunc(fs ...func()) Option
WithShutdownFunc registers function(s) running while shutting down by calling s.RegisterOnShutdown.
func WithShutdownTimeout ¶
WithShutdownTimeout set timeout for shutting down server
type Server ¶
type Server struct { *http.Server // ShutdownTimeout is the maximum duration for shutting down the server. // A zero or negative value means there will be no timeout. ShutdownTimeout time.Duration // contains filtered or unexported fields }
A Server defines parameters for gracefully running an HTTP server. The zero value for Server is a valid configuration.
func (*Server) ListenAndServe ¶
ListenAndServe listens on the TCP network address s.Addr and then calls s.Server.ListenAndServe to handle requests on incoming connections.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS listens on the TCP network address srv.Addr and then calls ServeTLS to handle requests on incoming TLS connections. Accepted connections are configured to enable TCP keep-alives.