Skip to content

Commit

Permalink
(NOBIDS) gracefully shutdown frontend (#2929)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrush authored Aug 12, 2024
1 parent 59fe647 commit 3246ca8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 15 additions & 3 deletions cmd/explorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/gob"
"encoding/hex"
"errors"
"flag"
"fmt"
"math/big"
Expand Down Expand Up @@ -58,6 +59,8 @@ func init() {
gob.Register(types.DataTableSaveState{})
}

var frontendHttpServer *http.Server

func main() {
configPath := flag.String("config", "", "Path to the config file, if empty string defaults will be used")
versionFlag := flag.Bool("version", false, "Show version and exit")
Expand Down Expand Up @@ -639,17 +642,17 @@ func main() {
if utils.Config.Frontend.HttpIdleTimeout == 0 {
utils.Config.Frontend.HttpIdleTimeout = time.Minute
}
srv := &http.Server{
frontendHttpServer = &http.Server{
Addr: cfg.Frontend.Server.Host + ":" + cfg.Frontend.Server.Port,
WriteTimeout: utils.Config.Frontend.HttpWriteTimeout,
ReadTimeout: utils.Config.Frontend.HttpReadTimeout,
IdleTimeout: utils.Config.Frontend.HttpIdleTimeout,
Handler: n,
}

logrus.Printf("http server listening on %v", srv.Addr)
logrus.Printf("http server listening on %v", frontendHttpServer.Addr)
go func() {
if err := srv.ListenAndServe(); err != nil {
if err := frontendHttpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.WithError(err).Fatal("Error serving frontend")
}
}()
Expand All @@ -670,5 +673,14 @@ func main() {

utils.WaitForCtrlC()

if frontendHttpServer != nil {
logrus.Infof("shutting down frontendHttpServer")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
if err := frontendHttpServer.Shutdown(ctx); err != nil {
logrus.WithError(err).Error("error shutting down frontend server")
}
}

logrus.Println("exiting...")
}
3 changes: 2 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sort"
"strconv"
"strings"
"syscall"
"time"
"unicode/utf8"

Expand Down Expand Up @@ -395,7 +396,7 @@ func GWeiBytesToEther(gwei []byte) decimal.Decimal {
// WaitForCtrlC will block/wait until a control-c is pressed
func WaitForCtrlC() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-c
}

Expand Down

0 comments on commit 3246ca8

Please sign in to comment.