Skip to content

Commit

Permalink
(NOBIDS) gracefully shutdown frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrush committed Aug 7, 2024
1 parent 2d57933 commit effab09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
17 changes: 14 additions & 3 deletions cmd/explorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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 +641,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 {
logrus.WithError(err).Fatal("Error serving frontend")
}
}()
Expand All @@ -670,5 +672,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).Fatal("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 effab09

Please sign in to comment.