feat!: support custom r/w gRPC options (#11889)

This commit is contained in:
Aleksandr Bezobchuk
2022-05-06 11:04:58 -04:00
committed by GitHub
parent 514dcb5cee
commit e3e65acf0f
6 changed files with 80 additions and 19 deletions
+24 -7
View File
@@ -2,13 +2,13 @@ package grpc
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"net"
"time"
"google.golang.org/grpc"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/server/grpc/gogoreflection"
reflection "github.com/cosmos/cosmos-sdk/server/grpc/reflection/v2alpha1"
"github.com/cosmos/cosmos-sdk/server/types"
@@ -16,14 +16,27 @@ import (
)
// StartGRPCServer starts a gRPC server on the given address.
func StartGRPCServer(clientCtx client.Context, app types.Application, address string) (*grpc.Server, error) {
func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config.GRPCConfig) (*grpc.Server, error) {
maxSendMsgSize := cfg.MaxSendMsgSize
if maxSendMsgSize == 0 {
maxSendMsgSize = config.DefaultGRPCMaxSendMsgSize
}
maxRecvMsgSize := cfg.MaxRecvMsgSize
if maxRecvMsgSize == 0 {
maxRecvMsgSize = config.DefaultGRPCMaxRecvMsgSize
}
grpcSrv := grpc.NewServer(
grpc.ForceServerCodec(codec.NewProtoCodec(clientCtx.InterfaceRegistry).GRPCCodec()),
grpc.MaxSendMsgSize(maxSendMsgSize),
grpc.MaxRecvMsgSize(maxRecvMsgSize),
)
app.RegisterGRPCServer(grpcSrv)
// reflection allows consumers to build dynamic clients that can write
// to any cosmos-sdk application without relying on application packages at compile time
// Reflection allows consumers to build dynamic clients that can write to any
// Cosmos SDK application without relying on application packages at compile
// time.
err := reflection.Register(grpcSrv, reflection.Config{
SigningModes: func() map[string]int32 {
modes := make(map[string]int32, len(clientCtx.TxConfig.SignModeHandler().Modes()))
@@ -39,10 +52,12 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, address st
if err != nil {
return nil, err
}
// Reflection allows external clients to see what services and methods
// the gRPC server exposes.
gogoreflection.Register(grpcSrv)
listener, err := net.Listen("tcp", address)
listener, err := net.Listen("tcp", cfg.Address)
if err != nil {
return nil, err
}
@@ -58,7 +73,9 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, address st
select {
case err := <-errCh:
return nil, err
case <-time.After(types.ServerStartTime): // assume server started successfully
case <-time.After(types.ServerStartTime):
// assume server started successfully
return grpcSrv, nil
}
}