Skip to content

Commit

Permalink
feat: support send and recv msg size for gRPC (backport #170) (#210)
Browse files Browse the repository at this point in the history
* feat: support send and recv msg size for gRPC (#170)

(cherry picked from commit ca48456)

# Conflicts:
#	server/start.go

* codec add

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>
  • Loading branch information
3 people authored Apr 27, 2022
1 parent f1a3f95 commit 3fa7fa4
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 3fa7fa4

Please sign in to comment.