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

45
run.go

@ -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,
)
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
}
}
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,16 +171,7 @@ func run() error {
return err
}
default:
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
signal.Notify(quit, syscall.SIGTERM)
signal.Notify(quit, syscall.SIGHUP)
<-quit
case <-quit:
shutdownTimeout := 10 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
@ -210,6 +190,7 @@ func run() error {
log.Error("There was an error shutting the server down")
return err
}
}
return nil
}