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 4242914 commit d45e2b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
27 changes: 13 additions & 14 deletions cmd/webhook/init/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ func ReadinessHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}

// Init server initialization function
// The server will respond to the following endpoints:
// - / (GET): initialization, negotiates headers and returns the domain filter
// - /records (GET): returns the current records
// - /records (POST): applies the changes
// - /adjustendpoints (POST): executes the AdjustEndpoints method
func Init(config configuration.Config, p *webhook.Webhook) *http.Server {
// Init initializes the http server
func Init(config configuration.Config, p *webhook.Webhook) (*http.Server, *http.Server) {
mainRouter := chi.NewRouter()

mainRouter.Get("/", p.Negotiate)
mainRouter.Get("/records", p.Records)
mainRouter.Post("/records", p.ApplyChanges)
Expand All @@ -65,7 +59,7 @@ func Init(config configuration.Config, p *webhook.Webhook) *http.Server {
}
}()

return mainServer
return mainServer, metricsServer
}

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

// ShutdownGracefully gracefully shutdown the http server
func ShutdownGracefully(srv *http.Server) {
func ShutdownGracefully(mainServer *http.Server, metricsServer *http.Server) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sig := <-sigCh

log.Infof("shutting down server due to received signal: %v", sig)
log.Infof("shutting down servers due to received signal: %v", sig)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
if err := srv.Shutdown(ctx); err != nil {
log.Errorf("error shutting down server: %v", err)
defer cancel()

if err := mainServer.Shutdown(ctx); err != nil {
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)
}
cancel()
}
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)
}

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

0 comments on commit d45e2b5

Please sign in to comment.