From 12bf850c8478aea31f7703ede169e496afdcbfb3 Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Wed, 12 Apr 2023 12:26:38 -0400 Subject: cmd/dockerd: gracefully shut down the API server As of Go 1.8, "net/http".Server provides facilities to close all listeners, making the same facilities in server.Server redundant. http.Server also improves upon server.Server by additionally providing a facility to also wait for outstanding requests to complete after closing all listeners. Leverage those facilities to give in-flight requests up to five seconds to finish up after all containers have been shut down. Signed-off-by: Cory Snider --- api/server/server.go | 83 ++-------------------------------------------------- 1 file changed, 3 insertions(+), 80 deletions(-) (limited to 'api/server') diff --git a/api/server/server.go b/api/server/server.go index 397323793e..fdf0a8ef6d 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -2,10 +2,7 @@ package server // import "github.com/docker/docker/api/server" import ( "context" - "net" "net/http" - "strings" - "time" "github.com/docker/docker/api/server/httpstatus" "github.com/docker/docker/api/server/httputils" @@ -23,8 +20,6 @@ const versionMatcher = "/v{version:[0-9.]+}" // Server contains instance details for the server type Server struct { - servers []*HTTPServer - routers []router.Router middlewares []middleware.Middleware } @@ -34,71 +29,6 @@ func (s *Server) UseMiddleware(m middleware.Middleware) { s.middlewares = append(s.middlewares, m) } -// Accept sets a listener the server accepts connections into. -func (s *Server) Accept(addr string, listeners ...net.Listener) { - for _, listener := range listeners { - httpServer := &HTTPServer{ - srv: &http.Server{ - Addr: addr, - ReadHeaderTimeout: 5 * time.Minute, // "G112: Potential Slowloris Attack (gosec)"; not a real concern for our use, so setting a long timeout. - }, - l: listener, - } - s.servers = append(s.servers, httpServer) - } -} - -// Close closes servers and thus stop receiving requests -func (s *Server) Close() { - for _, srv := range s.servers { - if err := srv.Close(); err != nil { - logrus.Error(err) - } - } -} - -// Serve starts listening for inbound requests. -func (s *Server) Serve() error { - var chErrors = make(chan error, len(s.servers)) - for _, srv := range s.servers { - srv.srv.Handler = s.createMux() - go func(srv *HTTPServer) { - var err error - logrus.Infof("API listen on %s", srv.l.Addr()) - if err = srv.Serve(); err != nil && strings.Contains(err.Error(), "use of closed network connection") { - err = nil - } - chErrors <- err - }(srv) - } - - for range s.servers { - err := <-chErrors - if err != nil { - return err - } - } - return nil -} - -// HTTPServer contains an instance of http server and the listener. -// srv *http.Server, contains configuration to create an http server and a mux router with all api end points. -// l net.Listener, is a TCP or Socket listener that dispatches incoming request to the router. -type HTTPServer struct { - srv *http.Server - l net.Listener -} - -// Serve starts listening for inbound requests. -func (s *HTTPServer) Serve() error { - return s.srv.Serve(s.l) -} - -// Close closes the HTTPServer from listening for the inbound requests. -func (s *HTTPServer) Close() error { - return s.l.Close() -} - func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // Define the context that we'll pass around to share info @@ -130,12 +60,6 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc { } } -// InitRouter initializes the list of routers for the server. -// This method also enables the Go profiler. -func (s *Server) InitRouter(routers ...router.Router) { - s.routers = append(s.routers, routers...) -} - type pageNotFoundError struct{} func (pageNotFoundError) Error() string { @@ -144,12 +68,12 @@ func (pageNotFoundError) Error() string { func (pageNotFoundError) NotFound() {} -// createMux initializes the main router the server uses. -func (s *Server) createMux() *mux.Router { +// CreateMux returns a new mux with all the routers registered. +func (s *Server) CreateMux(routers ...router.Router) *mux.Router { m := mux.NewRouter() logrus.Debug("Registering routers") - for _, apiRouter := range s.routers { + for _, apiRouter := range routers { for _, r := range apiRouter.Routes() { f := s.makeHTTPHandler(r.Handler()) @@ -160,7 +84,6 @@ func (s *Server) createMux() *mux.Router { } debugRouter := debug.NewRouter() - s.routers = append(s.routers, debugRouter) for _, r := range debugRouter.Routes() { f := s.makeHTTPHandler(r.Handler()) m.Path("/debug" + r.Path()).Handler(f) -- cgit v1.2.1