Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support send and recv msg size for gRPC (backport #170) #210

Merged
merged 2 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be 10MB

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