func (srv *Server) Stop(timeout time.Duration) {
	srv.stopLock.Lock()
	defer srv.stopLock.Unlock()
	srv.Timeout = timeout
	sendSignalInt(srv.interruptChan())
}
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.
package graceful
import (
	"crypto/tls"
	"log"
	"net"
	"net/http"
	"os"
	"sync"
	"time"
)
type Server struct {
	*http.Server
	Timeout time.Duration
	ListenLimit int
	TCPKeepAlive time.Duration
	ConnState func(net.Conn, http.ConnState)
	BeforeShutdown func() bool
	ShutdownInitiated func()
	NoSignalHandling bool
	Logger *log.Logger
	LogFunc func(format string, args ...interface{})
	Interrupted bool
	interrupt chan os.Signal
	stopLock sync.Mutex
	stopChan chan struct{}
	chanLock sync.RWMutex
	connections map[net.Conn]struct{}
	idleConnections map[net.Conn]struct{}
}
func Run(addr string, timeout time.Duration, n http.Handler) {
	srv := &Server{
		Timeout:      timeout,
		TCPKeepAlive: 3 * time.Minute,
		Server:       &http.Server{Addr: addr, Handler: n},
	}
	if err := srv.ListenAndServe(); err != nil {
		if opErr, ok := err.(*net.OpError); !ok || (ok && opErr.Op != "accept") {
			srv.logf("%s", err)
			os.Exit(1)
		}
	}
}
func RunWithErr(addr string, timeout time.Duration, n http.Handler) error {
	srv := &Server{
		Timeout:      timeout,
		TCPKeepAlive: 3 * time.Minute,
		Server:       &http.Server{Addr: addr, Handler: n},
		Logger:       DefaultLogger(),
	}
	return srv.ListenAndServe()
}
func ListenAndServe(server *http.Server, timeout time.Duration) error {
	srv := &Server{Timeout: timeout, Server: server, Logger: DefaultLogger()}
	return srv.ListenAndServe()
}
func (srv *Server) ListenAndServe() error {
	addr := srv.Addr
	if addr == "" {
		addr = ":http"
	}
	conn, err := srv.newTCPListener(addr)
	if err != nil {
		return err
	}
	return srv.Serve(conn)
}
func ListenAndServeTLS(server *http.Server, certFile, keyFile string, timeout time.Duration) error {
	srv := &Server{Timeout: timeout, Server: server, Logger: DefaultLogger()}
	return srv.ListenAndServeTLS(certFile, keyFile)
}
func (srv *Server) ListenTLS(certFile, keyFile string) (net.Listener, error) {
	addr := srv.Addr
	if addr == "" {
		addr = ":https"
	}
	config := &tls.Config{}
	if srv.TLSConfig != nil {
		*config = *srv.TLSConfig
	}
	var err error
	if certFile != "" && keyFile != "" {
		config.Certificates = make([]tls.Certificate, 1)
		config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
		if err != nil {
			return nil, err
		}
	}
	enableHTTP2ForTLSConfig(config)
	conn, err := srv.newTCPListener(addr)
	if err != nil {
		return nil, err
	}
	srv.TLSConfig = config
	tlsListener := tls.NewListener(conn, config)
	return tlsListener, nil
}
func enableHTTP2ForTLSConfig(t *tls.Config) {
	if TLSConfigHasHTTP2Enabled(t) {
		return
	}
	t.NextProtos = append(t.NextProtos, "h2")
}
func TLSConfigHasHTTP2Enabled(t *tls.Config) bool {
	for _, value := range t.NextProtos {
		if value == "h2" {
			return true
		}
	}
	return false
}
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
	l, err := srv.ListenTLS(certFile, keyFile)
	if err != nil {
		return err
	}
	return srv.Serve(l)
}
func (srv *Server) ListenAndServeTLSConfig(config *tls.Config) error {
	addr := srv.Addr
	if addr == "" {
		addr = ":https"
	}
	conn, err := srv.newTCPListener(addr)
	if err != nil {
		return err
	}
	srv.TLSConfig = config
	tlsListener := tls.NewListener(conn, config)
	return srv.Serve(tlsListener)
}
func Serve(server *http.Server, l net.Listener, timeout time.Duration) error {
	srv := &Server{Timeout: timeout, Server: server, Logger: DefaultLogger()}
	return srv.Serve(l)
}
func (srv *Server) Serve(listener net.Listener) error {
	if srv.ListenLimit != 0 {
		listener = LimitListener(listener, srv.ListenLimit)
	}
	srv.StopChan()
	add := make(chan net.Conn)
	idle := make(chan net.Conn)
	active := make(chan net.Conn)
	remove := make(chan net.Conn)
	srv.Server.ConnState = func(conn net.Conn, state http.ConnState) {
		switch state {
		case http.StateNew:
			add <- conn
		case http.StateActive:
			active <- conn
		case http.StateIdle:
			idle <- conn
		case http.StateClosed, http.StateHijacked:
			remove <- conn
		}
		srv.stopLock.Lock()
		defer srv.stopLock.Unlock()
		if srv.ConnState != nil {
			srv.ConnState(conn, state)
		}
	}
	shutdown := make(chan chan struct{})
	kill := make(chan struct{})
	go srv.manageConnections(add, idle, active, remove, shutdown, kill)
	interrupt := srv.interruptChan()
	if !srv.NoSignalHandling {
		signalNotify(interrupt)
	}
	quitting := make(chan struct{})
	go srv.handleInterrupt(interrupt, quitting, listener)
	err := srv.Server.Serve(listener)
	if err != nil {
		select {
		case <-quitting:
			err = nil
		default:
		}
	}
	srv.shutdown(shutdown, kill)
	return err
}