Skip to content

Commit

Permalink
feat: support send and recv msg size for gRPC (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Apr 4, 2022
1 parent 14651aa commit ca48456
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
Binary file modified contrib/rosetta/node/data.tar.gz
Binary file not shown.
21 changes: 17 additions & 4 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"math"
"strings"

"github.com/spf13/viper"
Expand Down Expand Up @@ -133,6 +134,14 @@ type GRPCConfig struct {

// Address defines the API server to listen on
Address string `mapstructure:"address"`

// MaxRecvMsgSize defines the max message size in bytes the server can receive.
// The default value is 10MB.
MaxRecvMsgSize int `mapstructure:"max-recv-msg-size"`

// MaxSendMsgSize defines the max message size in bytes the server can send.
// The default value is math.MaxInt32.
MaxSendMsgSize int `mapstructure:"max-send-msg-size"`
}

// GRPCWebConfig defines configuration for the gRPC-web server.
Expand Down Expand Up @@ -224,8 +233,10 @@ func DefaultConfig() *Config {
RPCMaxBodyBytes: 1000000,
},
GRPC: GRPCConfig{
Enable: true,
Address: DefaultGRPCAddress,
Enable: true,
Address: DefaultGRPCAddress,
MaxRecvMsgSize: 1024 * 1024 * 10,
MaxSendMsgSize: math.MaxInt32,
},
Rosetta: RosettaConfig{
Enable: false,
Expand Down Expand Up @@ -298,8 +309,10 @@ func GetConfig(v *viper.Viper) Config {
Offline: v.GetBool("rosetta.offline"),
},
GRPC: GRPCConfig{
Enable: v.GetBool("grpc.enable"),
Address: v.GetString("grpc.address"),
Enable: v.GetBool("grpc.enable"),
Address: v.GetString("grpc.address"),
MaxRecvMsgSize: v.GetInt("grpc.max-recv-msg-size"),
MaxSendMsgSize: v.GetInt("grpc.max-send-msg-size"),
},
GRPCWeb: GRPCWebConfig{
Enable: v.GetBool("grpc-web.enable"),
Expand Down
8 changes: 8 additions & 0 deletions server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ enable = {{ .GRPC.Enable }}
# Address defines the gRPC server address to bind to.
address = "{{ .GRPC.Address }}"
# MaxRecvMsgSize defines the max message size in bytes the server can receive.
# The default value is 4MB.
max-recv-msg-size = "{{ .GRPC.MaxRecvMsgSize }}"
# MaxSendMsgSize defines the max message size in bytes the server can send.
# The default value is math.MaxInt32.
max-send-msg-size = "{{ .GRPC.MaxSendMsgSize }}"
###############################################################################
### gRPC Web Configuration ###
###############################################################################
Expand Down
25 changes: 18 additions & 7 deletions server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,31 @@ import (
"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"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// StartGRPCServer starts a gRPC server on the given address.
func StartGRPCServer(clientCtx client.Context, app types.Application, address string) (*grpc.Server, error) {
grpcSrv := grpc.NewServer()
func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config.GRPCConfig) (*grpc.Server, error) {
grpcSrv := grpc.NewServer(
grpc.MaxSendMsgSize(cfg.MaxSendMsgSize),
grpc.MaxRecvMsgSize(cfg.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()))
for _, m := range clientCtx.TxConfig.SignModeHandler().Modes() {
modes[m.String()] = (int32)(m)
}

return modes
}(),
ChainID: clientCtx.ChainID,
Expand All @@ -35,10 +42,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
}
Expand All @@ -47,14 +56,16 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, address st
go func() {
err = grpcSrv.Serve(listener)
if err != nil {
errCh <- fmt.Errorf("failed to serve: %w", err)
errCh <- fmt.Errorf("failed to serve gRPC: %w", err)
}
}()

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
}
}
19 changes: 10 additions & 9 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import (
"runtime/pprof"
"time"

"github.com/cosmos/cosmos-sdk/codec"

"github.com/spf13/cobra"
"google.golang.org/grpc"

"github.com/tendermint/tendermint/abci/server"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
tmos "github.com/tendermint/tendermint/libs/os"
Expand All @@ -23,18 +19,19 @@ import (
pvm "github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/proxy"
"github.com/tendermint/tendermint/rpc/client/local"

"github.com/cosmos/cosmos-sdk/server/rosetta"
crgserver "github.com/cosmos/cosmos-sdk/server/rosetta/lib/server"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding"
"google.golang.org/grpc/encoding/proto"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
pruningTypes "github.com/cosmos/cosmos-sdk/pruning/types"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc"
"github.com/cosmos/cosmos-sdk/server/rosetta"
crgserver "github.com/cosmos/cosmos-sdk/server/rosetta/lib/server"
"github.com/cosmos/cosmos-sdk/server/types"
)

Expand Down Expand Up @@ -312,7 +309,11 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
grpcClient, err := grpc.Dial(
grpcAddress,
grpc.WithInsecure(),
grpc.WithDefaultCallOptions(grpc.ForceCodec(encoding.GetCodec(proto.Name))),
grpc.WithDefaultCallOptions(
grpc.ForceCodec(encoding.GetCodec(proto.Name)),
grpc.MaxCallRecvMsgSize(config.GRPC.MaxRecvMsgSize),
grpc.MaxCallSendMsgSize(config.GRPC.MaxSendMsgSize),
),
)
if err != nil {
return err
Expand Down Expand Up @@ -343,7 +344,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
grpcWebSrv *http.Server
)
if config.GRPC.Enable {
grpcSrv, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC.Address)
grpcSrv, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func startInProcess(cfg Config, val *Validator) error {
}

if val.AppConfig.GRPC.Enable {
grpcSrv, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC.Address)
grpcSrv, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC)
if err != nil {
return err
}
Expand Down

0 comments on commit ca48456

Please sign in to comment.