From a7b198923108b01a0e24ec6dd74a7aa9eb53d69a Mon Sep 17 00:00:00 2001 From: Matthieu 'JP' DERASSE Date: Sat, 12 Aug 2023 19:22:33 +0000 Subject: [PATCH] fix(webserver): always panic in case of an issue in webserver --- .drone.yml | 1 - go.mod | 2 +- webserver/webserver.go | 14 +++++++------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.drone.yml b/.drone.yml index 22d548f..514d2ab 100644 --- a/.drone.yml +++ b/.drone.yml @@ -80,7 +80,6 @@ steps: ``` {{commit.message}} ``` 🌐 {{ build.link }} - format: markdown when: status: - failure diff --git a/go.mod b/go.mod index a63bf72..b4da9bd 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module git.dev.m-and-m.ovh/mderasse/gocommon -go 1.21 +go 1.21.0 require ( github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 diff --git a/webserver/webserver.go b/webserver/webserver.go index dd1d75a..ecc83f3 100644 --- a/webserver/webserver.go +++ b/webserver/webserver.go @@ -3,7 +3,6 @@ package webserver import ( "context" "errors" - "fmt" "log" "net/http" "os" @@ -25,10 +24,11 @@ type ExecuteServerParams struct { // ExecuteServer will launche a http.Server and handle the "exit" signals and gracefully stop the server // and call the cleanup function. -func ExecuteServer(ctx context.Context, srv *http.Server, params *ExecuteServerParams) error { +func ExecuteServer(ctx context.Context, srv *http.Server, params *ExecuteServerParams) { if srv == nil { - return fmt.Errorf("missing http.server params") + log.Panicf("missing http.server params") + return } if srv.ReadTimeout == 0 { @@ -44,6 +44,7 @@ func ExecuteServer(ctx context.Context, srv *http.Server, params *ExecuteServerP go func() { if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { log.Panicf("HTTP server error: %v", err) + return } log.Printf("Stopped serving new connections.") }() @@ -57,16 +58,15 @@ func ExecuteServer(ctx context.Context, srv *http.Server, params *ExecuteServerP if err := srv.Shutdown(shutdownCtx); err != nil { log.Panicf("HTTP shutdown error: %v", err) + return } if params != nil && params.Cleanup != nil { if err := params.Cleanup(); err != nil { - log.Printf("Impossible to cleanup correctly after shutdown. error : %v", err) - return nil + log.Panicf("Impossible to cleanup correctly after shutdown. error : %v", err) + return } } log.Print("Graceful shutdown complete.") - - return nil }