Skip to content

Commit 720877d

Browse files
authored
fix: h2c placement in health server (#108)
1 parent e894994 commit 720877d

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

http/server.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func (c *HealthServerConfig) AddLivezChecks(checks ...healthz.HealthChecker) {
148148
// HealthServer is an HTTP server with healthz capabilities.
149149
type HealthServer struct {
150150
srv *Server
151+
mux *http.ServeMux
151152

152153
shudownCh chan struct{}
153154

@@ -165,10 +166,15 @@ type HealthServer struct {
165166

166167
// NewHealthServer returns an HTTP server with healthz capabilities.
167168
func NewHealthServer(ctx context.Context, cfg HealthServerConfig, opts ...SrvOptFunc) *HealthServer {
168-
srv := NewServer(ctx, cfg.Addr, cfg.Handler, opts...)
169+
// Setup the mux early so H2C can attach properly.
170+
mux := http.NewServeMux()
171+
mux.Handle("/", cfg.Handler)
172+
173+
srv := NewServer(ctx, cfg.Addr, mux, opts...)
169174

170175
return &HealthServer{
171176
srv: srv,
177+
mux: mux,
172178
shudownCh: make(chan struct{}),
173179
readyzChecks: cfg.ReadyzChecks,
174180
livezChecks: cfg.LivezChecks,
@@ -215,17 +221,13 @@ func (s *HealthServer) Serve(errFn func(error)) {
215221
}
216222

217223
func (s *HealthServer) installChecks() {
218-
mux := http.NewServeMux()
219-
s.installLivezChecks(mux)
224+
s.installLivezChecks(s.mux)
220225

221226
// When shutdown is started, the readyz check should start failing.
222227
if err := s.AddReadyzChecks(shutdownCheck{ch: s.shudownCh}); err != nil {
223228
s.log.Error("Could not install readyz shutdown check", lctx.Err(err))
224229
}
225-
s.installReadyzChecks(mux)
226-
227-
mux.Handle("/", s.srv.srv.Handler)
228-
s.srv.srv.Handler = mux
230+
s.installReadyzChecks(s.mux)
229231
}
230232

231233
func (s *HealthServer) installReadyzChecks(mux *http.ServeMux) {

0 commit comments

Comments
 (0)