Remove all docker debugging knowledge from the server.

It should be explicitly told whether to enable the profiler or not.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2016-02-16 13:58:24 -05:00
parent 556b1369de
commit e8f569b324
3 changed files with 37 additions and 35 deletions

View File

@ -9,8 +9,10 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
func profilerSetup(mainRouter *mux.Router, path string) { const debugPathPrefix = "/debug/"
var r = mainRouter.PathPrefix(path).Subrouter()
func profilerSetup(mainRouter *mux.Router) {
var r = mainRouter.PathPrefix(debugPathPrefix).Subrouter()
r.HandleFunc("/vars", expVars) r.HandleFunc("/vars", expVars)
r.HandleFunc("/pprof/", pprof.Index) r.HandleFunc("/pprof/", pprof.Index)
r.HandleFunc("/pprof/cmdline", pprof.Cmdline) r.HandleFunc("/pprof/cmdline", pprof.Cmdline)

View File

@ -72,8 +72,6 @@ func (s *Server) Close() {
// serveAPI loops through all initialized servers and spawns goroutine // serveAPI loops through all initialized servers and spawns goroutine
// with Server method for each. It sets createMux() as Handler also. // with Server method for each. It sets createMux() as Handler also.
func (s *Server) serveAPI() error { func (s *Server) serveAPI() error {
s.initRouterSwapper()
var chErrors = make(chan error, len(s.servers)) var chErrors = make(chan error, len(s.servers))
for _, srv := range s.servers { for _, srv := range s.servers {
srv.srv.Handler = s.routerSwapper srv.srv.Handler = s.routerSwapper
@ -149,24 +147,25 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
} }
} }
// AddRouters initializes a list of routers for the server. // InitRouter initializes the list of routers for the server.
func (s *Server) AddRouters(routers ...router.Router) { // This method also enables the Go profiler if enableProfiler is true.
func (s *Server) InitRouter(enableProfiler bool, routers ...router.Router) {
for _, r := range routers { for _, r := range routers {
s.addRouter(r) s.routers = append(s.routers, r)
} }
}
// addRouter adds a new router to the server. m := s.createMux()
func (s *Server) addRouter(r router.Router) { if enableProfiler {
s.routers = append(s.routers, r) profilerSetup(m)
}
s.routerSwapper = &routerSwapper{
router: m,
}
} }
// createMux initializes the main router the server uses. // createMux initializes the main router the server uses.
func (s *Server) createMux() *mux.Router { func (s *Server) createMux() *mux.Router {
m := mux.NewRouter() m := mux.NewRouter()
if utils.IsDebugEnabled() {
profilerSetup(m, "/debug/")
}
logrus.Debugf("Registering routers") logrus.Debugf("Registering routers")
for _, apiRouter := range s.routers { for _, apiRouter := range s.routers {
@ -194,23 +193,14 @@ func (s *Server) Wait(waitChan chan error) {
waitChan <- nil waitChan <- nil
} }
func (s *Server) initRouterSwapper() { // DisableProfiler reloads the server mux without adding the profiler routes.
s.routerSwapper = &routerSwapper{ func (s *Server) DisableProfiler() {
router: s.createMux(), s.routerSwapper.Swap(s.createMux())
}
} }
// Reload reads configuration changes and modifies the // EnableProfiler reloads the server mux adding the profiler routes.
// server according to those changes. func (s *Server) EnableProfiler() {
// Currently, only the --debug configuration is taken into account. m := s.createMux()
func (s *Server) Reload(debug bool) { profilerSetup(m)
debugEnabled := utils.IsDebugEnabled() s.routerSwapper.Swap(m)
switch {
case debugEnabled && !debug: // disable debug
utils.DisableDebug()
s.routerSwapper.Swap(s.createMux())
case debug && !debugEnabled: // enable debug
utils.EnableDebug()
s.routerSwapper.Swap(s.createMux())
}
} }

View File

@ -282,14 +282,23 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
"graphdriver": d.GraphDriverName(), "graphdriver": d.GraphDriverName(),
}).Info("Docker daemon") }).Info("Docker daemon")
initRouters(api, d) initRouter(api, d)
reload := func(config *daemon.Config) { reload := func(config *daemon.Config) {
if err := d.Reload(config); err != nil { if err := d.Reload(config); err != nil {
logrus.Errorf("Error reconfiguring the daemon: %v", err) logrus.Errorf("Error reconfiguring the daemon: %v", err)
return return
} }
api.Reload(config.Debug)
debugEnabled := utils.IsDebugEnabled()
switch {
case debugEnabled && !config.Debug: // disable debug
utils.DisableDebug()
api.DisableProfiler()
case config.Debug && !debugEnabled: // enable debug
utils.EnableDebug()
api.EnableProfiler()
}
} }
setupConfigReloadTrap(*configFile, cli.flags, reload) setupConfigReloadTrap(*configFile, cli.flags, reload)
@ -386,8 +395,9 @@ func loadDaemonCliConfig(config *daemon.Config, daemonFlags *flag.FlagSet, commo
return config, nil return config, nil
} }
func initRouters(s *apiserver.Server, d *daemon.Daemon) { func initRouter(s *apiserver.Server, d *daemon.Daemon) {
s.AddRouters(container.NewRouter(d), s.InitRouter(utils.IsDebugEnabled(),
container.NewRouter(d),
image.NewRouter(d), image.NewRouter(d),
network.NewRouter(d), network.NewRouter(d),
systemrouter.NewRouter(d), systemrouter.NewRouter(d),