Skip to content

Commit

Permalink
fix: gracefully shutdown both http servers
Browse files Browse the repository at this point in the history
Signed-off-by: Devin Buhl <[email protected]>
  • Loading branch information
onedr0p committed May 26, 2024
1 parent d45e2b5 commit cbe96cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions cmd/webhook/init/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ func Init(config configuration.Config, p *webhook.Webhook) (*http.Server, *http.
}
}()

metricsRouter := chi.NewRouter()
metricsRouter.Get("/metrics", promhttp.Handler().ServeHTTP)
metricsRouter.Get("/healthz", HealthCheckHandler)
metricsRouter.Get("/readyz", ReadinessHandler)
healthRouter := chi.NewRouter()
healthRouter.Get("/metrics", promhttp.Handler().ServeHTTP)
healthRouter.Get("/healthz", HealthCheckHandler)
healthRouter.Get("/readyz", ReadinessHandler)

metricsServer := createHTTPServer("0.0.0.0:8080", metricsRouter, config.ServerReadTimeout, config.ServerWriteTimeout)
healthServer := createHTTPServer("0.0.0.0:8080", healthRouter, config.ServerReadTimeout, config.ServerWriteTimeout)
go func() {
log.Infof("starting metrics server on addr: '%s' ", metricsServer.Addr)
if err := metricsServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorf("can't serve metrics on addr: '%s', error: %v", metricsServer.Addr, err)
log.Infof("starting health server on addr: '%s' ", healthServer.Addr)
if err := healthServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorf("can't serve health on addr: '%s', error: %v", healthServer.Addr, err)
}
}()

return mainServer, metricsServer
return mainServer, healthServer
}

func createHTTPServer(addr string, hand http.Handler, readTimeout, writeTimeout time.Duration) *http.Server {
Expand All @@ -72,7 +72,7 @@ func createHTTPServer(addr string, hand http.Handler, readTimeout, writeTimeout
}

// ShutdownGracefully gracefully shutdown the http server
func ShutdownGracefully(mainServer *http.Server, metricsServer *http.Server) {
func ShutdownGracefully(mainServer *http.Server, healthServer *http.Server) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sig := <-sigCh
Expand All @@ -85,7 +85,7 @@ func ShutdownGracefully(mainServer *http.Server, metricsServer *http.Server) {
log.Errorf("error shutting down main server: %v", err)
}

if err := metricsServer.Shutdown(ctx); err != nil {
log.Errorf("error shutting down metrics server: %v", err)
if err := healthServer.Shutdown(ctx); err != nil {
log.Errorf("error shutting down health server: %v", err)
}
}
4 changes: 2 additions & 2 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func main() {
log.Fatalf("failed to initialize provider: %v", err)
}

main, metrics := server.Init(config, webhook.New(provider))
server.ShutdownGracefully(main, metrics)
main, health := server.Init(config, webhook.New(provider))
server.ShutdownGracefully(main, health)
}

0 comments on commit cbe96cc

Please sign in to comment.