From 3943e7dc217efca9dd04fc99ae34bc557a40bf77 Mon Sep 17 00:00:00 2001 From: Chris F Ravenscroft Date: Sat, 7 Oct 2023 18:04:53 -0700 Subject: [PATCH] Graceful shutdown (#346) * build: make build vars optional and passable via env * build: use . for BUILD_FILES var to allow buildvcs flag * build: create mkbindir, build and sha256 targets * build: reuse build target for each platform specific compilation * Ensure same go version for main and plugins * chore: release 2.3.0 Release-As: 2.3.0 * release-please * Fix(plugins)!: Rename the groups table to ldapgroups (#326) The groups table name is a reserved keyword in mysql and requires that the table name be escaped with backticks in order to use the groups name. Other databases such as postgres do not support the use of the backticks in the query statements. This change renames the groups table to ldapgroups in oder to avoid this problem altogether. This does have a dependency on the backend sql plugins to alter the schema to match the new name. Signed-off-by: Billy Olsen Co-authored-by: Chris F Ravenscroft * chore: update docker sqlite with ldapgroups (#338) * feat: Update migration code to support table names (#339) * chore(dev): release 2.3.0 (#337) * ci: change pr name pattern for release please * ci: switch to use master as default branch and plan to drop dev * fix: exploit release-please for tagging in code * fix: use runtime/debug to fetch git build info * fix: use newer function to fetch git info for version * build: drop build tags * feat: graceful shutdown (#343) * fix: start servers on separate goroutines * fix: listen to signals for graceful shutdowns * fix: exit with 0 in case of programmed termination --------- Signed-off-by: Billy Olsen Co-authored-by: shipperizer Co-authored-by: Billy Olsen --- v2/glauth.go | 56 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/v2/glauth.go b/v2/glauth.go index 1fea784..1bc19e9 100644 --- a/v2/glauth.go +++ b/v2/glauth.go @@ -3,6 +3,15 @@ package main import ( "bytes" "fmt" + "net/http" + "os" + "os/signal" + "path/filepath" + "reflect" + "strings" + "syscall" + "time" + "github.com/GeertJohan/yubigo" "github.com/arl/statsviz" docopt "github.com/docopt/docopt-go" @@ -18,12 +27,6 @@ import ( "github.com/rs/zerolog" "gopkg.in/amz.v3/aws" "gopkg.in/amz.v3/s3" - "net/http" - "os" - "path/filepath" - "reflect" - "strings" - "time" ) const programName = "glauth" @@ -112,40 +115,45 @@ func startService() { server.Logger(log), server.Config(activeConfig), ) + if err != nil { log.Error().Err(err).Msg("could not create server") os.Exit(1) } if activeConfig.LDAP.Enabled { - // Don't block if also starting a LDAPS server afterwards - shouldBlock := !activeConfig.LDAPS.Enabled - - if shouldBlock { + go func() { if err := s.ListenAndServe(); err != nil { log.Error().Err(err).Msg("could not start LDAP server") os.Exit(1) } - } else { - go func() { - if err := s.ListenAndServe(); err != nil { - log.Error().Err(err).Msg("could not start LDAP server") - os.Exit(1) - } - }() - } + }() } if activeConfig.LDAPS.Enabled { - // Always block here - if err := s.ListenAndServeTLS(); err != nil { - log.Error().Err(err).Msg("could not start LDAPS server") - os.Exit(1) - } + go func() { + if err := s.ListenAndServeTLS(); err != nil { + log.Error().Err(err).Msg("could not start LDAPS server") + os.Exit(1) + } + }() } + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + + // Block until we receive our signal. + <-c + + // Doesn't block if no connections, but will otherwise wait + // until the timeout deadline. + s.Shutdown() + + // Optionally, you could run srv.Shutdown in a goroutine and block on + // <-ctx.Done() if your application should wait for other services + // to finalize based on context cancellation. log.Info().Msg("AP exit") - os.Exit(1) + os.Exit(0) } func startConfigWatcher() {