refactor: cleanup server logic (#15041)
This commit is contained in:
+22
-19
@@ -19,15 +19,15 @@ import (
|
||||
"cosmossdk.io/log"
|
||||
sdkmath "cosmossdk.io/math"
|
||||
"cosmossdk.io/math/unsafe"
|
||||
pruningtypes "cosmossdk.io/store/pruning/types"
|
||||
cmtlog "github.com/cometbft/cometbft/libs/log"
|
||||
"github.com/cometbft/cometbft/node"
|
||||
cmtclient "github.com/cometbft/cometbft/rpc/client"
|
||||
dbm "github.com/cosmos/cosmos-db"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pruningtypes "cosmossdk.io/store/pruning/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
|
||||
@@ -261,10 +261,12 @@ type (
|
||||
ValAddress sdk.ValAddress
|
||||
RPCClient cmtclient.Client
|
||||
|
||||
tmNode *node.Node
|
||||
api *api.Server
|
||||
grpc *grpc.Server
|
||||
grpcWeb *http.Server
|
||||
tmNode *node.Node
|
||||
api *api.Server
|
||||
grpc *grpc.Server
|
||||
grpcWeb *http.Server
|
||||
errGroup *errgroup.Group
|
||||
cancelFn context.CancelFunc
|
||||
}
|
||||
|
||||
// ValidatorI expose a validator's context and configuration
|
||||
@@ -734,24 +736,25 @@ func (n *Network) Cleanup() {
|
||||
n.Logger.Log("cleaning up test network...")
|
||||
|
||||
for _, v := range n.Validators {
|
||||
// cancel the validator's context which will signal to the gRPC and API
|
||||
// goroutines that they should gracefully exit.
|
||||
v.cancelFn()
|
||||
|
||||
if err := v.errGroup.Wait(); err != nil {
|
||||
n.Logger.Log("unexpected error waiting for validator gRPC and API processes to exit", "err", err)
|
||||
}
|
||||
|
||||
if v.tmNode != nil && v.tmNode.IsRunning() {
|
||||
_ = v.tmNode.Stop()
|
||||
}
|
||||
|
||||
if v.api != nil {
|
||||
_ = v.api.Close()
|
||||
}
|
||||
|
||||
if v.grpc != nil {
|
||||
v.grpc.Stop()
|
||||
if v.grpcWeb != nil {
|
||||
_ = v.grpcWeb.Close()
|
||||
if err := v.tmNode.Stop(); err != nil {
|
||||
n.Logger.Log("failed to stop validator CometBFT node", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
if v.grpcWeb != nil {
|
||||
_ = v.grpcWeb.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// Give a brief pause for things to finish closing in other processes. Hopefully this helps with the address-in-use errors.
|
||||
// 100ms chosen randomly.
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
if n.Config.CleanupDir {
|
||||
|
||||
+19
-17
@@ -1,12 +1,12 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/cometbft/cometbft/node"
|
||||
"github.com/cometbft/cometbft/p2p"
|
||||
@@ -15,10 +15,10 @@ import (
|
||||
"github.com/cometbft/cometbft/rpc/client/local"
|
||||
cmttypes "github.com/cometbft/cometbft/types"
|
||||
cmttime "github.com/cometbft/cometbft/types/time"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/server/api"
|
||||
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc"
|
||||
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/genutil"
|
||||
@@ -82,12 +82,24 @@ func startInProcess(cfg Config, val *Validator) error {
|
||||
app.RegisterNodeService(val.ClientCtx)
|
||||
}
|
||||
|
||||
if val.AppConfig.GRPC.Enable {
|
||||
grpcSrv, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC)
|
||||
ctx := context.Background()
|
||||
ctx, val.cancelFn = context.WithCancel(ctx)
|
||||
val.errGroup, ctx = errgroup.WithContext(ctx)
|
||||
|
||||
grpcCfg := val.AppConfig.GRPC
|
||||
|
||||
if grpcCfg.Enable {
|
||||
grpcSrv, err := servergrpc.NewGRPCServer(val.ClientCtx, app, grpcCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Start the gRPC server in a goroutine. Note, the provided ctx will ensure
|
||||
// that the server is gracefully shut down.
|
||||
val.errGroup.Go(func() error {
|
||||
return servergrpc.StartGRPCServer(ctx, logger.With("module", "grpc-server"), grpcCfg, grpcSrv)
|
||||
})
|
||||
|
||||
val.grpc = grpcSrv
|
||||
}
|
||||
|
||||
@@ -95,19 +107,9 @@ func startInProcess(cfg Config, val *Validator) error {
|
||||
apiSrv := api.New(val.ClientCtx, logger.With("module", "api-server"), val.grpc)
|
||||
app.RegisterAPIRoutes(apiSrv, val.AppConfig.API)
|
||||
|
||||
errCh := make(chan error)
|
||||
|
||||
go func() {
|
||||
if err := apiSrv.Start(*val.AppConfig); err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
return err
|
||||
case <-time.After(srvtypes.ServerStartTime): // assume server started successfully
|
||||
}
|
||||
val.errGroup.Go(func() error {
|
||||
return apiSrv.Start(ctx, *val.AppConfig)
|
||||
})
|
||||
|
||||
val.api = apiSrv
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user