summaryrefslogtreecommitdiff
path: root/api/server
diff options
context:
space:
mode:
authorSebastiaan van Stijn <thaJeztah@users.noreply.github.com>2023-04-28 23:54:34 +0200
committerGitHub <noreply@github.com>2023-04-28 23:54:34 +0200
commite22758bfb2d615f67512336f121c677d099b3269 (patch)
treec251c2d1719b4f2d9d0f96415ca973239f8643aa /api/server
parentdffad6b0b77351e8c6c8feba6cea5cd84c4d78a4 (diff)
parent12bf850c8478aea31f7703ede169e496afdcbfb3 (diff)
downloaddocker-e22758bfb2d615f67512336f121c677d099b3269.tar.gz
Merge pull request #45314 from corhere/graceful-shutdown
cmd/dockerd: gracefully shut down the API server
Diffstat (limited to 'api/server')
-rw-r--r--api/server/server.go83
1 files changed, 3 insertions, 80 deletions
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)