Browse code

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>

David Calavera authored on 2016/02/17 03:58:24
Showing 3 changed files
... ...
@@ -9,8 +9,10 @@ import (
9 9
 	"github.com/gorilla/mux"
10 10
 )
11 11
 
12
-func profilerSetup(mainRouter *mux.Router, path string) {
13
-	var r = mainRouter.PathPrefix(path).Subrouter()
12
+const debugPathPrefix = "/debug/"
13
+
14
+func profilerSetup(mainRouter *mux.Router) {
15
+	var r = mainRouter.PathPrefix(debugPathPrefix).Subrouter()
14 16
 	r.HandleFunc("/vars", expVars)
15 17
 	r.HandleFunc("/pprof/", pprof.Index)
16 18
 	r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
... ...
@@ -72,8 +72,6 @@ func (s *Server) Close() {
72 72
 // serveAPI loops through all initialized servers and spawns goroutine
73 73
 // with Server method for each. It sets createMux() as Handler also.
74 74
 func (s *Server) serveAPI() error {
75
-	s.initRouterSwapper()
76
-
77 75
 	var chErrors = make(chan error, len(s.servers))
78 76
 	for _, srv := range s.servers {
79 77
 		srv.srv.Handler = s.routerSwapper
... ...
@@ -149,24 +147,25 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
149 149
 	}
150 150
 }
151 151
 
152
-// AddRouters initializes a list of routers for the server.
153
-func (s *Server) AddRouters(routers ...router.Router) {
152
+// InitRouter initializes the list of routers for the server.
153
+// This method also enables the Go profiler if enableProfiler is true.
154
+func (s *Server) InitRouter(enableProfiler bool, routers ...router.Router) {
154 155
 	for _, r := range routers {
155
-		s.addRouter(r)
156
+		s.routers = append(s.routers, r)
156 157
 	}
157
-}
158 158
 
159
-// addRouter adds a new router to the server.
160
-func (s *Server) addRouter(r router.Router) {
161
-	s.routers = append(s.routers, r)
159
+	m := s.createMux()
160
+	if enableProfiler {
161
+		profilerSetup(m)
162
+	}
163
+	s.routerSwapper = &routerSwapper{
164
+		router: m,
165
+	}
162 166
 }
163 167
 
164 168
 // createMux initializes the main router the server uses.
165 169
 func (s *Server) createMux() *mux.Router {
166 170
 	m := mux.NewRouter()
167
-	if utils.IsDebugEnabled() {
168
-		profilerSetup(m, "/debug/")
169
-	}
170 171
 
171 172
 	logrus.Debugf("Registering routers")
172 173
 	for _, apiRouter := range s.routers {
... ...
@@ -194,23 +193,14 @@ func (s *Server) Wait(waitChan chan error) {
194 194
 	waitChan <- nil
195 195
 }
196 196
 
197
-func (s *Server) initRouterSwapper() {
198
-	s.routerSwapper = &routerSwapper{
199
-		router: s.createMux(),
200
-	}
197
+// DisableProfiler reloads the server mux without adding the profiler routes.
198
+func (s *Server) DisableProfiler() {
199
+	s.routerSwapper.Swap(s.createMux())
201 200
 }
202 201
 
203
-// Reload reads configuration changes and modifies the
204
-// server according to those changes.
205
-// Currently, only the --debug configuration is taken into account.
206
-func (s *Server) Reload(debug bool) {
207
-	debugEnabled := utils.IsDebugEnabled()
208
-	switch {
209
-	case debugEnabled && !debug: // disable debug
210
-		utils.DisableDebug()
211
-		s.routerSwapper.Swap(s.createMux())
212
-	case debug && !debugEnabled: // enable debug
213
-		utils.EnableDebug()
214
-		s.routerSwapper.Swap(s.createMux())
215
-	}
202
+// EnableProfiler reloads the server mux adding the profiler routes.
203
+func (s *Server) EnableProfiler() {
204
+	m := s.createMux()
205
+	profilerSetup(m)
206
+	s.routerSwapper.Swap(m)
216 207
 }
... ...
@@ -282,14 +282,23 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
282 282
 		"graphdriver": d.GraphDriverName(),
283 283
 	}).Info("Docker daemon")
284 284
 
285
-	initRouters(api, d)
285
+	initRouter(api, d)
286 286
 
287 287
 	reload := func(config *daemon.Config) {
288 288
 		if err := d.Reload(config); err != nil {
289 289
 			logrus.Errorf("Error reconfiguring the daemon: %v", err)
290 290
 			return
291 291
 		}
292
-		api.Reload(config.Debug)
292
+
293
+		debugEnabled := utils.IsDebugEnabled()
294
+		switch {
295
+		case debugEnabled && !config.Debug: // disable debug
296
+			utils.DisableDebug()
297
+			api.DisableProfiler()
298
+		case config.Debug && !debugEnabled: // enable debug
299
+			utils.EnableDebug()
300
+			api.EnableProfiler()
301
+		}
293 302
 	}
294 303
 
295 304
 	setupConfigReloadTrap(*configFile, cli.flags, reload)
... ...
@@ -386,8 +395,9 @@ func loadDaemonCliConfig(config *daemon.Config, daemonFlags *flag.FlagSet, commo
386 386
 	return config, nil
387 387
 }
388 388
 
389
-func initRouters(s *apiserver.Server, d *daemon.Daemon) {
390
-	s.AddRouters(container.NewRouter(d),
389
+func initRouter(s *apiserver.Server, d *daemon.Daemon) {
390
+	s.InitRouter(utils.IsDebugEnabled(),
391
+		container.NewRouter(d),
391 392
 		image.NewRouter(d),
392 393
 		network.NewRouter(d),
393 394
 		systemrouter.NewRouter(d),