Skip to content

Commit

Permalink
Make RPC concurrency configurable (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak authored Sep 11, 2023
1 parent c5ac16e commit 2196b70
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
4 changes: 4 additions & 0 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
metricsPortF = "metrics-port"
grpcF = "grpc"
grpcPortF = "grpc-port"
rpcMaxConcurrencyF = "rpc-max-concurrency"

defaultConfig = ""
defaultHTTP = false
Expand All @@ -68,6 +69,7 @@ const (
defaultMetricsPort = 9090
defaultGRPC = false
defaultGRPCPort = 6064
defaultRPCMaxConcurrency = 1024

configFlagUsage = "The yaml configuration file."
logLevelFlagUsage = "Options: debug, info, warn, error."
Expand All @@ -90,6 +92,7 @@ const (
metricsPortUsage = "The port on which the prometheus endpoint will listen for requests."
grpcUsage = "Enable the HTTP GRPC server on the default port."
grpcPortUsage = "The port on which the GRPC server will listen for requests."
rpcMaxConcurrencyUsage = "Maximum number of RPC requests to be handled concurrently"
)

var Version string
Expand Down Expand Up @@ -196,6 +199,7 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
junoCmd.Flags().Uint16(metricsPortF, defaultMetricsPort, metricsPortUsage)
junoCmd.Flags().Bool(grpcF, defaultGRPC, grpcUsage)
junoCmd.Flags().Uint16(grpcPortF, defaultGRPCPort, grpcPortUsage)
junoCmd.Flags().Int(rpcMaxConcurrencyF, defaultRPCMaxConcurrency, rpcMaxConcurrencyUsage)

return junoCmd
}
39 changes: 25 additions & 14 deletions cmd/juno/juno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestConfigPrecedence(t *testing.T) {
defaultGRPCPort := uint16(6064)
defaultColour := true
defaultPendingPollInterval := time.Duration(0)
defaultRPCMaxConcurrency := 1024

tests := map[string]struct {
cfgFile bool
Expand All @@ -62,6 +63,7 @@ func TestConfigPrecedence(t *testing.T) {
MetricsPort: defaultMetricsPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
"config file path is empty string": {
Expand All @@ -82,6 +84,7 @@ func TestConfigPrecedence(t *testing.T) {
PprofPort: defaultPprofPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
"config file doesn't exist": {
Expand All @@ -106,6 +109,7 @@ func TestConfigPrecedence(t *testing.T) {
PendingPollInterval: defaultPendingPollInterval,
Pprof: defaultPprof,
PprofPort: defaultPprofPort,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
"config file with all settings but without any other flags": {
Expand All @@ -132,6 +136,7 @@ pprof: true
PprofPort: defaultPprofPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
"config file with some settings but without any other flags": {
Expand All @@ -155,6 +160,7 @@ http-port: 4576
PprofPort: defaultPprofPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
"all flags without config file": {
Expand All @@ -163,20 +169,21 @@ http-port: 4576
"--db-path", "/home/.juno", "--network", "goerli", "--pprof",
},
expectedConfig: &node.Config{
LogLevel: utils.DEBUG,
HTTP: defaultHTTP,
HTTPPort: 4576,
Websocket: defaultWS,
WebsocketPort: defaultWSPort,
GRPC: defaultGRPC,
GRPCPort: defaultGRPCPort,
Metrics: defaultMetrics,
MetricsPort: defaultMetricsPort,
DatabasePath: "/home/.juno",
Network: utils.GOERLI,
Pprof: true,
PprofPort: defaultPprofPort,
Colour: defaultColour,
LogLevel: utils.DEBUG,
HTTP: defaultHTTP,
HTTPPort: 4576,
Websocket: defaultWS,
WebsocketPort: defaultWSPort,
GRPC: defaultGRPC,
GRPCPort: defaultGRPCPort,
Metrics: defaultMetrics,
MetricsPort: defaultMetricsPort,
DatabasePath: "/home/.juno",
Network: utils.GOERLI,
Pprof: true,
PprofPort: defaultPprofPort,
Colour: defaultColour,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
"some flags without config file": {
Expand All @@ -200,6 +207,7 @@ http-port: 4576
PprofPort: defaultPprofPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
"all setting set in both config file and flags": {
Expand Down Expand Up @@ -240,6 +248,7 @@ pending-poll-interval: 5s
PprofPort: 6064,
Colour: defaultColour,
PendingPollInterval: time.Millisecond,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
"some setting set in both config file and flags": {
Expand All @@ -265,6 +274,7 @@ network: goerli
PprofPort: defaultPprofPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
"some setting set in default, config file and flags": {
Expand All @@ -287,6 +297,7 @@ network: goerli
PprofPort: defaultPprofPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
RPCMaxConcurrency: defaultRPCMaxConcurrency,
},
},
}
Expand Down
7 changes: 3 additions & 4 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path/filepath"
"reflect"
"runtime"
"time"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -63,6 +62,8 @@ type Config struct {
P2P bool `mapstructure:"p2p"`
P2PAddr string `mapstructure:"p2p-addr"`
P2PBootPeers string `mapstructure:"p2p-boot-peers"`

RPCMaxConcurrency int `mapstructure:"rpc-max-concurrency"`
}

type Node struct {
Expand Down Expand Up @@ -113,9 +114,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
gatewayClient := gateway.NewClient(cfg.Network.GatewayURL(), log).WithUserAgent(ua)

rpcHandler := rpc.New(chain, synchronizer, cfg.Network, gatewayClient, client, vm.New(log), version, log)
// to improve RPC throughput we double GOMAXPROCS
maxGoroutines := 2 * runtime.GOMAXPROCS(0)
jsonrpcServer := jsonrpc.NewServer(maxGoroutines, log).WithValidator(validator.Validator())
jsonrpcServer := jsonrpc.NewServer(cfg.RPCMaxConcurrency, log).WithValidator(validator.Validator())
for _, method := range methods(rpcHandler) {
if err = jsonrpcServer.RegisterMethod(method); err != nil {
return nil, err
Expand Down
8 changes: 5 additions & 3 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ func TestDefaultDbPath(t *testing.T) {

for _, n := range networks {
t.Run(n.String(), func(t *testing.T) {
cfg := &node.Config{Network: n, DatabasePath: ""}
cfg := &node.Config{Network: n, DatabasePath: "", RPCMaxConcurrency: 1}
expectedCfg := node.Config{
Network: n,
DatabasePath: filepath.Join(defaultDataDir, n.String()),
Network: n,
DatabasePath: filepath.Join(defaultDataDir, n.String()),
RPCMaxConcurrency: cfg.RPCMaxConcurrency,
}
snNode, err := node.New(cfg, "1.2.3")
require.NoError(t, err)
Expand Down Expand Up @@ -54,6 +55,7 @@ func TestNewNode(t *testing.T) {
P2P: true,
P2PAddr: "",
P2PBootPeers: "",
RPCMaxConcurrency: 1,
}

_, err := node.New(config, "v0.3")
Expand Down

0 comments on commit 2196b70

Please sign in to comment.