go: die even more reliably on error
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-05-03 05:58:09 +02:00
parent 1359750c97
commit 672132a109
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ

73
run.go
View File

@ -139,42 +139,31 @@ func run() error {
// channel used to check whether the app had troubles starting up.
started := make(chan error, 1)
defer close(started)
go func(ok chan error) {
p := setting.Port()
h := setting.Host()
address := h + ":" + strconv.Itoa(p)
if err = e.Start(address); err != nil && err != http.ErrServerClosed {
log.Error("troubles running the server, bailing", "error", err.Error())
if err := e.Start(address); err != nil && err != http.ErrServerClosed {
log.Error("troubles running the server, bailing...", "error", err)
shutdownTimeout := 3 * time.Second
ctx, cancel := context.WithTimeout(
context.Background(), shutdownTimeout,
)
started <- err
defer func() {
cancel()
started <- err
close(started)
}()
log.Info("shutting down the server")
err = e.Shutdown(ctx)
if err == ctx.Err() && err != nil {
log.Info("RIP this is taking too long")
started <- err
e.Server.Close()
return
}
return
}
started <- err
started <- nil
}(started)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
signal.Notify(quit, syscall.SIGTERM)
signal.Notify(quit, syscall.SIGHUP)
// non-blocking channel receive.
select {
case err := <-started:
@ -182,33 +171,25 @@ func run() error {
return err
}
default:
}
case <-quit:
shutdownTimeout := 10 * time.Second
quit := make(chan os.Signal, 1)
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer func() {
log.Infof("Interrupt received, gracefully shutting down the server (timeout %s)", shutdownTimeout)
cancel()
signal.Notify(quit, os.Interrupt)
signal.Notify(quit, syscall.SIGTERM)
signal.Notify(quit, syscall.SIGHUP)
<-quit
signal.Stop(quit)
shutdownTimeout := 10 * time.Second
close(quit)
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer func() {
log.Infof("Interrupt received, gracefully shutting down the server (timeout %s)", shutdownTimeout)
cancel()
log.Info("Bye!")
}()
signal.Stop(quit)
close(quit)
log.Info("Bye!")
}()
if err = e.Shutdown(ctx); err != nil {
log.Error("There was an error shutting the server down")
return err
if err = e.Shutdown(ctx); err != nil {
log.Error("There was an error shutting the server down")
return err
}
}
return nil