From 342a06850e13e1530df12fbe25086701cbcb89f2 Mon Sep 17 00:00:00 2001 From: momosh-ethernal Date: Tue, 7 Dec 2021 10:38:55 +0100 Subject: [PATCH] Renamed ctx variable --- cmd/heimdalld/main.go | 5 +++-- server/root.go | 23 +++++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/heimdalld/main.go b/cmd/heimdalld/main.go index aed265c52a..2e06942fba 100644 --- a/cmd/heimdalld/main.go +++ b/cmd/heimdalld/main.go @@ -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 @@ -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...") diff --git a/server/root.go b/server/root.go index 6a80afc2c8..d6909d1ea3 100644 --- a/server/root.go +++ b/server/root.go @@ -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() @@ -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) @@ -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 @@ -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 }, }