Skip to content

Commit b6da6fb

Browse files
refactor(setup): simplify applyListenerConfigs by removing duplicate … (#868)
* refactor(setup): simplify applyListenerConfigs by removing duplicate code --------- Signed-off-by: Elia-Renzoni <[email protected]> Signed-off-by: James Ranson <[email protected]> Co-authored-by: James Ranson <[email protected]>
1 parent 99ac246 commit b6da6fb

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

pkg/daemon/setup/listeners.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,54 +130,46 @@ func applyListenerConfigs(conf, oldConf *config.Config,
130130
conf.Frontend.ConnectionsLimit, nil, router, t2, errorFunc, 0, conf.Frontend.ReadHeaderTimeout)
131131
}
132132

133+
metricsRouter.RegisterRoute("/metrics", nil, nil,
134+
false, metrics.Handler())
135+
metricsRouter.RegisterRoute(conf.MgmtConfig.ConfigHandlerPath, nil, nil,
136+
false, http.HandlerFunc(ch.HandlerFunc(conf)))
137+
133138
// if the Metrics HTTP port is configured, then set up the http listener instance
134139
if conf.Metrics != nil && conf.Metrics.ListenPort > 0 &&
135140
(!hasOldMC || (conf.Metrics.ListenAddress != oldConf.Metrics.ListenAddress ||
136141
conf.Metrics.ListenPort != oldConf.Metrics.ListenPort)) {
137142
lg.DrainAndClose("metricsListener", 0)
138-
metricsRouter.RegisterRoute("/metrics", nil, nil,
139-
false, metrics.Handler())
140-
metricsRouter.RegisterRoute(conf.MgmtConfig.ConfigHandlerPath, nil, nil,
141-
false, http.HandlerFunc(ch.HandlerFunc(conf)))
142143
if conf.MgmtConfig.PprofServer == "both" || conf.MgmtConfig.PprofServer == "metrics" {
143144
pprof.RegisterRoutes("metrics", metricsRouter)
144145
}
145146
go lg.StartListener("metricsListener",
146147
conf.Metrics.ListenAddress, conf.Metrics.ListenPort,
147148
conf.Frontend.ConnectionsLimit, nil, metricsRouter, nil, errorFunc, 0, conf.Frontend.ReadHeaderTimeout)
148149
} else {
149-
metricsRouter.RegisterRoute("/metrics", nil, nil,
150-
false, metrics.Handler())
151-
metricsRouter.RegisterRoute(conf.MgmtConfig.ConfigHandlerPath, nil, nil,
152-
false, http.HandlerFunc(ch.HandlerFunc(conf)))
153150
lg.UpdateRouter("metricsListener", metricsRouter)
154151
}
155152

156153
mr := lm.NewRouter() // management router
154+
mr.RegisterRoute(conf.MgmtConfig.ConfigHandlerPath, nil, nil,
155+
false, http.HandlerFunc(ch.HandlerFunc(conf)))
156+
mr.RegisterRoute(conf.MgmtConfig.ReloadHandlerPath, nil, nil,
157+
false, reloadHandler)
158+
mr.RegisterRoute(conf.MgmtConfig.PurgeByPathHandlerPath, nil, nil,
159+
true, http.HandlerFunc(ph.PathHandler(conf.MgmtConfig.PurgeByPathHandlerPath, &o)))
160+
157161
// if the Management HTTP port is configured, then set up the http listener instance
158162
if conf.MgmtConfig != nil && conf.MgmtConfig.ListenPort > 0 &&
159163
(!hasOldRC || (conf.MgmtConfig.ListenAddress != oldConf.MgmtConfig.ListenAddress ||
160164
conf.MgmtConfig.ListenPort != oldConf.MgmtConfig.ListenPort)) {
161165
lg.DrainAndClose("mgmtListener", time.Millisecond*500)
162-
mr.RegisterRoute(conf.MgmtConfig.ConfigHandlerPath, nil, nil,
163-
false, http.HandlerFunc(ch.HandlerFunc(conf)))
164-
mr.RegisterRoute(conf.MgmtConfig.ReloadHandlerPath, nil, nil,
165-
false, reloadHandler)
166-
mr.RegisterRoute(conf.MgmtConfig.PurgeByPathHandlerPath, nil, nil,
167-
true, http.HandlerFunc(ph.PathHandler(conf.MgmtConfig.PurgeByPathHandlerPath, &o)))
168166
if conf.MgmtConfig.PprofServer == "both" || conf.MgmtConfig.PprofServer == "mgmt" {
169167
pprof.RegisterRoutes("mgmt", mr)
170168
}
171169
go lg.StartListener("mgmtListener",
172170
conf.MgmtConfig.ListenAddress, conf.MgmtConfig.ListenPort,
173171
conf.Frontend.ConnectionsLimit, nil, mr, nil, errorFunc, 0, conf.Frontend.ReadHeaderTimeout)
174172
} else {
175-
mr.RegisterRoute(conf.MgmtConfig.ConfigHandlerPath, nil, nil,
176-
false, http.HandlerFunc(ch.HandlerFunc(conf)))
177-
mr.RegisterRoute(conf.MgmtConfig.ReloadHandlerPath, nil, nil,
178-
false, reloadHandler)
179-
mr.RegisterRoute(conf.MgmtConfig.PurgeByPathHandlerPath, nil, nil,
180-
true, http.HandlerFunc(ph.PathHandler(conf.MgmtConfig.PurgeByPathHandlerPath, &o)))
181173
lg.UpdateRouter("mgmtListener", mr)
182174
}
183175
}

0 commit comments

Comments
 (0)