This is a creation in Article, where the information may have evolved or changed.
Docker Swarm manage
The last part of the function is the creation Docker API server
section:
server := api.NewServer(hosts, tlsConfig)if c.Bool("replication") { ...... setupReplication(c, cl, server, discovery, addr, leaderTTL, tlsConfig)} else { server.SetHandler(api.NewPrimary(cl, tlsConfig, &statusHandler{cl, nil, nil}, c.GlobalBool("debug"), c.Bool("cors"))) cluster.NewWatchdog(cl)}log.Fatal(server.ListenAndServe())
Server
The structure is defined in api/server.go
:
type Server struct { hosts []string tlsConfig *tls.Config dispatcher *dispatcher}
Its core approach is to ListenAndServe()
host
goroutine
listen for and process a connection request for each one Docker client
.
Func (S *server) Listenandserve () error {cherrors: = Make (chan error, Len (s.hosts)) for _, Host: = Range S.hosts { Protoaddrparts: = Strings. SPLITN (Host, "://", 2) If Len (protoaddrparts) = = 1 {protoaddrparts = append ([]string{"TCP"}, PROTOADDRP Arts ...) } go func () {log. Withfields (log. fields{"Proto": Protoaddrparts[0], "addr": Protoaddrparts[1]}). Info ("Listening for HTTP") VAR (l net). Listener Err Error Server = &http. server{Addr:protoaddrparts[1], Handler:s.dispatcher,} ) Switch Protoaddrparts[0] {case "UNIX": l, Err = Newunixlistener (protoaddrparts[ 1], S.tlsconfig) case "TCP": l, Err = Newlistener ("TCP", Protoaddrparts[1], s.tlsconfig) Default:err = FMT. Errorf ("Unsupported protocol:%q", ProtoaddrpartS[0])} if err! = Nil {cherrors <-err} else {Cherror S <-server. Serve (L)}} ()} for I: = 0; I < Len (s.hosts); i++ {err: = <-cherrors if err! = Nil {return err}} return nil}