Skip to content

Commit

Permalink
feat: support custom r/w gRPC options (backport #1017) (#1019)
Browse files Browse the repository at this point in the history
* feat: support custom r/w gRPC options (backport #1017)

* chore: update changelog
  • Loading branch information
zemyblue authored May 30, 2023
1 parent 94b5a52 commit 8a1dac4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features
* (x/auth) [\#1011](https://github.com/Finschia/finschia-sdk/pull/1011) add the api for querying next account number
* (server/grpc) [\#1019](https://github.com/Finschia/finschia-sdk/pull/1019) support custom r/w gRPC options (backport #1017)

### Improvements

Expand Down
29 changes: 25 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 All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand Down
6 changes: 6 additions & 0 deletions server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 8 additions & 0 deletions server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###
###############################################################################
Expand Down
30 changes: 24 additions & 6 deletions server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,34 @@ 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"
sdk "github.com/Finschia/finschia-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) {
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()))
Expand All @@ -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
}
Expand All @@ -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
}
}
16 changes: 8 additions & 8 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 8a1dac4

Please sign in to comment.