Skip to content

Commit

Permalink
Renamed ctx variable
Browse files Browse the repository at this point in the history
  • Loading branch information
momoshell committed Dec 7, 2021
1 parent 52e0532 commit 342a068
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
5 changes: 3 additions & 2 deletions cmd/heimdalld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func startInProcess(shutdownCtx context.Context, serverCtx *server.Context, appC
}
}

// using group ctx makes sense in case that one of
// using group ctx makes sense in case that if one of
// the processes produces error the rest will go and shutdown
g, gCtx := errgroup.WithContext(shutdownCtx)
// start rest
Expand All @@ -372,7 +372,8 @@ func startInProcess(shutdownCtx context.Context, serverCtx *server.Context, appC

// stop phase for Tendermint node
g.Go(func() error {
// wait here for interrup
// wait here for interrupt signal or
// until something in the group returns non-nil error
<-gCtx.Done()
serverCtx.Logger.Info("exiting...")

Expand Down
23 changes: 13 additions & 10 deletions server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

const shutdownTimeout = 10 * time.Second

func StartRestServer(ctx context.Context, cdc *codec.Codec, registerRoutesFn func(ctx client.CLIContext, mux *mux.Router), restCh chan struct{}) error {
func StartRestServer(shutdownCtx context.Context, cdc *codec.Codec, registerRoutesFn func(ctx client.CLIContext, mux *mux.Router), restCh chan struct{}) error {
// init vars for the Light Client Rest server
cliCtx := cliCtx.NewCLIContext().WithCodec(cdc)
router := mux.NewRouter()
Expand Down Expand Up @@ -67,18 +67,19 @@ func StartRestServer(ctx context.Context, cdc *codec.Codec, registerRoutesFn fun
),
)

g, gCtx := errgroup.WithContext(ctx)
g := new(errgroup.Group)
// start serving
g.Go(func() error {
return startRPCServer(ctx, listener, router, logger, cfg)
return startRPCServer(shutdownCtx, listener, router, logger, cfg)
})

// wait for os interrupt, then close Listener
g.Go(func() error {
// wait for os interrupt, then close Listener
<-gCtx.Done()
<-shutdownCtx.Done()
return listener.Close()
})
// wait here

// wait here, blockingly
if err := g.Wait(); err != nil {
if err != http.ErrServerClosed {
logger.Error("Cannot start REST server.", "Error", err)
Expand Down Expand Up @@ -173,20 +174,22 @@ func startRPCServer(shutdownCtx context.Context, listener net.Listener, handler
}

g := new(errgroup.Group)
// start serving
g.Go(func() error {
return s.Serve(listener)
})

// wait for interrupt signal
g.Go(func() error {
// wait for interrupt signal coming from mainCtx
// and then go to server shutdown
<-shutdownCtx.Done()
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()

// and then go to server shutdown
return s.Shutdown(ctx)
})

// wait here and block
if err := g.Wait(); err != nil {
logger.Info("RPC HTTP server stopped", "err", err)
return err
Expand All @@ -198,14 +201,14 @@ func startRPCServer(shutdownCtx context.Context, listener net.Listener, handler
// ServeCommands will generate a long-running rest server
// (aka Light Client Daemon) that exposes functionality similar
// to the cli, but over rest
func ServeCommands(ctx context.Context, cdc *codec.Codec, registerRoutesFn func(ctx client.CLIContext, mux *mux.Router)) *cobra.Command {
func ServeCommands(shutdownCtx context.Context, cdc *codec.Codec, registerRoutesFn func(ctx client.CLIContext, mux *mux.Router)) *cobra.Command {
cmd := &cobra.Command{
Use: "rest-server",
Short: "Start LCD (light-client daemon), a local REST server",
RunE: func(cmd *cobra.Command, args []string) error {
helper.InitHeimdallConfig("")
restCh := make(chan struct{}, 1)
err := StartRestServer(ctx, cdc, registerRoutesFn, restCh)
err := StartRestServer(shutdownCtx, cdc, registerRoutesFn, restCh)
return err
},
}
Expand Down

0 comments on commit 342a068

Please sign in to comment.