Skip to content

Commit 2ce5407

Browse files
committed
Minor refactor inspired by review comments
Signed-off-by: Ed Snible <snible@us.ibm.com>
1 parent 2d8a5ea commit 2ce5407

4 files changed

Lines changed: 50 additions & 25 deletions

File tree

authbridge/authlib/runtimeutil/runtimeutil.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,44 @@ func StartSignalToggle() {
6767
}()
6868
}
6969

70-
// StartHealthServer serves liveness (/healthz) and readiness (/readyz) on addr
71-
// in a goroutine. Readiness reports 503 while any inbound or outbound plugin is
72-
// still waiting on a dependency (e.g. a credential file that hasn't landed yet).
73-
func StartHealthServer(inboundH, outboundH *pipeline.Holder, addr string) {
70+
// StartHealthServer binds addr, serves liveness (/healthz) and readiness
71+
// (/readyz) in a goroutine, and returns the server for graceful shutdown.
72+
// Readiness reports 503 while any inbound or outbound plugin is still waiting on
73+
// a dependency (e.g. a credential file that hasn't landed yet). A bind failure
74+
// is returned so the caller can decide how to handle it; a serve-time failure
75+
// after bind is logged.
76+
func StartHealthServer(inboundH, outboundH *pipeline.Holder, addr string) (*http.Server, error) {
77+
mux := http.NewServeMux()
78+
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
79+
w.WriteHeader(http.StatusOK)
80+
})
81+
mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
82+
if name := inboundH.NotReadyPlugin(); name != "" {
83+
http.Error(w, "inbound plugin not ready: "+name, http.StatusServiceUnavailable)
84+
return
85+
}
86+
if name := outboundH.NotReadyPlugin(); name != "" {
87+
http.Error(w, "outbound plugin not ready: "+name, http.StatusServiceUnavailable)
88+
return
89+
}
90+
w.WriteHeader(http.StatusOK)
91+
})
92+
srv := &http.Server{
93+
Addr: addr,
94+
Handler: mux,
95+
ReadHeaderTimeout: 10 * time.Second,
96+
}
97+
listener, err := net.Listen("tcp", addr)
98+
if err != nil {
99+
return nil, err
100+
}
74101
go func() {
75-
mux := http.NewServeMux()
76-
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
77-
w.WriteHeader(http.StatusOK)
78-
})
79-
mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
80-
if name := inboundH.NotReadyPlugin(); name != "" {
81-
http.Error(w, "inbound plugin not ready: "+name, http.StatusServiceUnavailable)
82-
return
83-
}
84-
if name := outboundH.NotReadyPlugin(); name != "" {
85-
http.Error(w, "outbound plugin not ready: "+name, http.StatusServiceUnavailable)
86-
return
87-
}
88-
w.WriteHeader(http.StatusOK)
89-
})
90-
slog.Info("health server listening", "addr", addr)
91-
if err := http.ListenAndServe(addr, mux); err != nil {
92-
slog.Warn("health server failed", "error", err)
102+
slog.Info("health server listening", "addr", listener.Addr().String())
103+
if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
104+
slog.Error("health server failed", "error", err)
93105
}
94106
}()
107+
return srv, nil
95108
}
96109

97110
// StartStatServer binds addr for the stats/config-inspection server, serves it

authbridge/cmd/authbridge-cpex/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ func main() {
245245

246246
slog.Info("authbridge-cpex starting", "mode", cfg.Mode, "logLevel", runtimeutil.LogLevel().String())
247247

248-
runtimeutil.StartHealthServer(inboundH, outboundH, ":9091")
248+
healthSrv, healthErr := runtimeutil.StartHealthServer(inboundH, outboundH, ":9091")
249+
if healthErr != nil {
250+
log.Fatalf("health server listen: %v", healthErr)
251+
}
249252

250253
sigCh := make(chan os.Signal, 1)
251254
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
@@ -259,6 +262,7 @@ func main() {
259262
srv.Shutdown(shutdownCtx)
260263
}
261264
statSrv.Shutdown(shutdownCtx)
265+
healthSrv.Shutdown(shutdownCtx)
262266
if sessionAPISrv != nil {
263267
sessionAPISrv.Shutdown(shutdownCtx)
264268
}

authbridge/cmd/authbridge-envoy/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ func main() {
231231

232232
slog.Info("authbridge-envoy starting", "mode", cfg.Mode, "logLevel", runtimeutil.LogLevel().String())
233233

234-
runtimeutil.StartHealthServer(inboundH, outboundH, ":9091")
234+
healthSrv, healthErr := runtimeutil.StartHealthServer(inboundH, outboundH, ":9091")
235+
if healthErr != nil {
236+
log.Fatalf("health server listen: %v", healthErr)
237+
}
235238

236239
sigCh := make(chan os.Signal, 1)
237240
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
@@ -249,6 +252,7 @@ func main() {
249252
srv.GracefulStop()
250253
}
251254
statSrv.Shutdown(shutdownCtx)
255+
healthSrv.Shutdown(shutdownCtx)
252256
if sessionAPISrv != nil {
253257
sessionAPISrv.Shutdown(shutdownCtx)
254258
}

authbridge/cmd/authbridge-proxy/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,10 @@ func main() {
467467

468468
slog.Info("authbridge-proxy starting", "version", version, "mode", cfg.Mode, "logLevel", runtimeutil.LogLevel().String())
469469

470-
runtimeutil.StartHealthServer(inboundH, outboundH, ":9091")
470+
healthSrv, healthErr := runtimeutil.StartHealthServer(inboundH, outboundH, ":9091")
471+
if healthErr != nil {
472+
log.Fatalf("health server listen: %v", healthErr)
473+
}
471474

472475
sigCh := make(chan os.Signal, 1)
473476
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
@@ -484,6 +487,7 @@ func main() {
484487
_ = transparentLn.Close()
485488
}
486489
statSrv.Shutdown(shutdownCtx)
490+
healthSrv.Shutdown(shutdownCtx)
487491
if sessionAPISrv != nil {
488492
sessionAPISrv.Shutdown(shutdownCtx)
489493
}

0 commit comments

Comments
 (0)