Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NotReadyError ¶
type NotReadyError struct {
Err error
// Quitting is true when a shutdown is in progress
Quitting bool
}
NotReadyError is returned from a Serve() call if the server is already running, or during shutdown.
func (*NotReadyError) Error ¶
func (e *NotReadyError) Error() string
type Server ¶
type Server struct {
*http.Server
// Timeout is the duration to allow outstanding requests to survive
// before forcefully terminating them.
// A timeout of 0 will make the server wait forever for connections to close
// by them selves. (as per github.com/tylerb/graceful doc.)
Timeout time.Duration
// ConnState specifies an optional callback function that is
// called when a client connection changes state. This is a proxy
// to the underlying http.Server's ConnState, and the original
// must not be set directly.
ConnState func(net.Conn, http.ConnState)
// SyncShutdown makes the server not exit immediately on Shutdown signal.
// But wait for keep-alive connections to finish.
// If not setting SyncShutdown true, Call Wait() to wait for shutdown to finish
SyncShutdown bool
// contains filtered or unexported fields
}
Server implements an extended http.Server with a Shutdown() method which enables it to be shut down gracefully, by first stopping accepting and disabling keepalives and then wait for all connections to have terminated. A Timeout can be set to finally kill the last outstanding connections forcefully.
func (*Server) ConnectionsKilled ¶
ConnectionsKilled returns the number of HTTP connections killed by the server during last shutdown.
func (*Server) Serve ¶
Serve is equivalent to net/http.Server.Serve() with graceful shutdown enabled.
Example ¶
s := &Server{
Server: &http.Server{},
}
l, err := net.Listen("tcp", "")
if err != nil {
log.Fatal(err)
}
go func() {
time.Sleep(time.Second)
s.Shutdown()
s.Wait()
}()
err = s.Serve(l)
if err != nil {
log.Fatal(err)
}
fmt.Println("OK")
Output: OK
func (*Server) Shutdown ¶
func (srv *Server) Shutdown()
Shutdown signales the server asynchronously to start shutdown process It only has effect the first time it's called.
func (*Server) ShutdownOK ¶
ShutdownOK stops the server from accepting new requets and begins shutting down. It returns true if this call ended up being the one triggering a shutdown