Documentation ¶
Index ¶
- func ListenAndServe(server *http.Server, timeout time.Duration) error
- func ListenAndServeTLS(server *http.Server, certFile, keyFile string, timeout time.Duration) error
- func Run(addr string, timeout time.Duration, n http.Handler)
- func Serve(server *http.Server, l net.Listener, timeout time.Duration) error
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ListenAndServe ¶
ListenAndServe is equivalent to http.Server.ListenAndServe with graceful shutdown enabled.
timeout is the duration to wait until killing active requests and stopping the server. If timeout is 0, the server never times out. It waits for all active requests to finish.
func ListenAndServeTLS ¶
ListenAndServeTLS is equivalent to http.Server.ListenAndServeTLS with graceful shutdown enabled.
timeout is the duration to wait until killing active requests and stopping the server. If timeout is 0, the server never times out. It waits for all active requests to finish.
func Run ¶
Run serves the http.Handler with graceful shutdown enabled.
timeout is the duration to wait until killing active requests and stopping the server. If timeout is 0, the server never times out. It waits for all active requests to finish.
Types ¶
type Server ¶
type Server struct { *http.Server // Timeout is the duration to allow outstanding requests to survive // before forcefully terminating them. Timeout time.Duration // Limit the number of outstanding requests ListenLimit int // 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) // ShutdownInitiated is an optional callback function that is called // when shutdown is initiated. It can be used to notify the client // side of long lived connections (e.g. websockets) to reconnect. ShutdownInitiated func() // contains filtered or unexported fields }
Server wraps an http.Server with graceful connection handling. It may be used directly in the same way as http.Server, or may be constructed with the global functions in this package.
Example:
srv := &graceful.Server{ Timeout: 5 * time.Second, Server: &http.Server{Addr: ":1234", Handler: handler}, } srv.ListenAndServe()
func (*Server) ListenAndServe ¶
ListenAndServe is equivalent to http.Server.ListenAndServe with graceful shutdown enabled.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS is equivalent to http.Server.ListenAndServeTLS with graceful shutdown enabled.
func (*Server) Stop ¶
Stop instructs the type to halt operations and close the stop channel when it is finished.
timeout is grace period for which to wait before shutting down the server. The timeout value passed here will override the timeout given when constructing the server, as this is an explicit command to stop the server.