From 13ae1f685ff5922bab0ab6a619ae4b2e576043a2 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Sat, 27 May 2023 13:01:16 +0900 Subject: [PATCH] feat: support custom r/w gRPC options (#1017) * feat: support custom r/w gRPC options (backport cosmos/cosmos-sdk#11889) * chore: add simple unittest for app config * chore: update changelog --- CHANGELOG.md | 1 + server/config/config.go | 29 +++++++++++++++++++++++++---- server/config/config_test.go | 6 ++++++ server/config/toml.go | 8 ++++++++ server/grpc/server.go | 30 ++++++++++++++++++++++++------ server/start.go | 16 ++++++++-------- testutil/network/util.go | 2 +- 7 files changed, 73 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4b70e2e92..ae5aa36b8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased](https://github.com/Finschia/finschia-sdk/compare/v0.47.0...HEAD) ### Features +* (server/grpc) [\#1017](https://github.com/Finschia/finschia-sdk/pull/1017) support custom r/w gRPC options (backport cosmos/cosmos-sdk#11889) ### Improvements diff --git a/server/config/config.go b/server/config/config.go index 078789503d..a73785f136 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "math" "strings" "github.com/spf13/viper" @@ -26,6 +27,14 @@ const ( // DefaultChanCheckTxSize defines the default size of channel check tx in Baseapp DefaultChanCheckTxSize = 10000 + + // DefaultGRPCMaxRecvMsgSize defines the default gRPC max message size in + // bytes the server can receive. + DefaultGRPCMaxRecvMsgSize = 1024 * 1024 * 10 + + // DefaultGRPCMaxSendMsgSize defines the default gRPC max message size in + // bytes the server can send. + DefaultGRPCMaxSendMsgSize = math.MaxInt32 ) // BaseConfig defines the server's basic configuration @@ -157,6 +166,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. @@ -254,8 +271,10 @@ func DefaultConfig() *Config { RPCMaxBodyBytes: 1000000, }, GRPC: GRPCConfig{ - Enable: true, - Address: DefaultGRPCAddress, + Enable: true, + Address: DefaultGRPCAddress, + MaxRecvMsgSize: DefaultGRPCMaxRecvMsgSize, + MaxSendMsgSize: DefaultGRPCMaxSendMsgSize, }, Rosetta: RosettaConfig{ Enable: false, @@ -339,8 +358,10 @@ func GetConfig(v *viper.Viper) (Config, error) { 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"), diff --git a/server/config/config_test.go b/server/config/config_test.go index 9f68b1f363..4a1e9ba9b1 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -31,3 +31,9 @@ func TestValidateBasic(t *testing.T) { err = cfg.ValidateBasic() require.Error(t, err) } + +func TestSetGRPCMsgSize(t *testing.T) { + cfg := DefaultConfig() + require.Equal(t, DefaultGRPCMaxRecvMsgSize, cfg.GRPC.MaxRecvMsgSize) + require.Equal(t, DefaultGRPCMaxSendMsgSize, cfg.GRPC.MaxSendMsgSize) +} diff --git a/server/config/toml.go b/server/config/toml.go index f22722bfa5..bae2601707 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -194,6 +194,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 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 ### ############################################################################### diff --git a/server/grpc/server.go b/server/grpc/server.go index 2f8204ca3e..58c9896caa 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -8,6 +8,7 @@ import ( "google.golang.org/grpc" "github.com/Finschia/finschia-sdk/client" + "github.com/Finschia/finschia-sdk/server/config" "github.com/Finschia/finschia-sdk/server/grpc/gogoreflection" reflection "github.com/Finschia/finschia-sdk/server/grpc/reflection/v2" "github.com/Finschia/finschia-sdk/server/types" @@ -15,11 +16,26 @@ import ( ) // 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) { + maxSendMsgSize := cfg.MaxSendMsgSize + if maxSendMsgSize == 0 { + maxSendMsgSize = config.DefaultGRPCMaxSendMsgSize + } + + maxRecvMsgSize := cfg.MaxRecvMsgSize + if maxRecvMsgSize == 0 { + maxRecvMsgSize = config.DefaultGRPCMaxRecvMsgSize + } + + grpcSrv := grpc.NewServer( + 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())) @@ -38,7 +54,8 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, address st // 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 } @@ -54,7 +71,8 @@ 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 } } diff --git a/server/start.go b/server/start.go index bca7d5c015..d71cb6f901 100644 --- a/server/start.go +++ b/server/start.go @@ -25,7 +25,7 @@ import ( "github.com/Finschia/finschia-sdk/client/flags" "github.com/Finschia/finschia-sdk/codec" "github.com/Finschia/finschia-sdk/server/api" - "github.com/Finschia/finschia-sdk/server/config" + serverconfig "github.com/Finschia/finschia-sdk/server/config" servergrpc "github.com/Finschia/finschia-sdk/server/grpc" "github.com/Finschia/finschia-sdk/server/rosetta" crgserver "github.com/Finschia/finschia-sdk/server/rosetta/lib/server" @@ -170,10 +170,10 @@ is performed. Note, when enabled, gRPC will also be automatically enabled. cmd.Flags().Bool(flagGRPCOnly, false, "Start the node in gRPC query only mode (no Tendermint process is started)") cmd.Flags().Bool(flagGRPCEnable, true, "Define if the gRPC server should be enabled") - cmd.Flags().String(flagGRPCAddress, config.DefaultGRPCAddress, "the gRPC server address to listen on") + cmd.Flags().String(flagGRPCAddress, serverconfig.DefaultGRPCAddress, "the gRPC server address to listen on") cmd.Flags().Bool(flagGRPCWebEnable, true, "Define if the gRPC-Web server should be enabled. (Note: gRPC must also be enabled.)") - cmd.Flags().String(flagGRPCWebAddress, config.DefaultGRPCWebAddress, "The gRPC-Web server address to listen on") + cmd.Flags().String(flagGRPCWebAddress, serverconfig.DefaultGRPCWebAddress, "The gRPC-Web server address to listen on") cmd.Flags().Uint64(FlagStateSyncSnapshotInterval, 0, "State sync snapshot interval") cmd.Flags().Uint32(FlagStateSyncSnapshotKeepRecent, 2, "State sync snapshot to keep") @@ -182,7 +182,7 @@ is performed. Note, when enabled, gRPC will also be automatically enabled. cmd.Flags().Bool(FlagPrometheus, false, "Enable prometheus metric for app") - cmd.Flags().Uint(FlagChanCheckTxSize, config.DefaultChanCheckTxSize, "The size of the channel check tx") + cmd.Flags().Uint(FlagChanCheckTxSize, serverconfig.DefaultChanCheckTxSize, "The size of the channel check tx") // add support for all Ostracon-specific command line options ostcmd.AddNodeFlags(cmd) @@ -207,7 +207,7 @@ func startStandAlone(ctx *Context, appCreator types.AppCreator) error { app := appCreator(ctx.Logger, db, traceWriter, ctx.Viper) - config, err := config.GetConfig(ctx.Viper) + config, err := serverconfig.GetConfig(ctx.Viper) if err != nil { return err } @@ -273,7 +273,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App return err } - config, err := config.GetConfig(ctx.Viper) + config, err := serverconfig.GetConfig(ctx.Viper) if err != nil { return err } @@ -381,7 +381,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App ) 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 } @@ -471,7 +471,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App return WaitForQuitSignals() } -func startTelemetry(cfg config.Config) (*telemetry.Metrics, error) { +func startTelemetry(cfg serverconfig.Config) (*telemetry.Metrics, error) { if !cfg.Telemetry.Enabled { return nil, nil } diff --git a/testutil/network/util.go b/testutil/network/util.go index 95aade7505..7fcffd865b 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -100,7 +100,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 }