Skip to content

Commit

Permalink
[rpc] Add Rate Limiter and cache for RPCs (#3711)
Browse files Browse the repository at this point in the history
* Fix 1.0.4 migration func for rate limiter
  • Loading branch information
AlexiaChen authored May 21, 2021
1 parent ce6e4c1 commit 5e09b89
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 25 deletions.
4 changes: 3 additions & 1 deletion cmd/harmony/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ type wsConfig struct {
}

type rpcOptConfig struct {
DebugEnabled bool // Enables PrivateDebugService APIs, including the EVM tracer
DebugEnabled bool // Enables PrivateDebugService APIs, including the EVM tracer
RateLimterEnabled bool // Enable Rate limiter for RPC
RequestsPerSecond int // for RPC rate limiter
}

type devnetConfig struct {
Expand Down
8 changes: 8 additions & 0 deletions cmd/harmony/config_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ func init() {
confTree.Set("HTTP.RosettaPort", defaultConfig.HTTP.RosettaPort)
}

if confTree.Get("RPCOpt.RateLimterEnabled") == nil {
confTree.Set("RPCOpt.RateLimterEnabled", defaultConfig.RPCOpt.RateLimterEnabled)
}

if confTree.Get("RPCOpt.RequestsPerSecond") == nil {
confTree.Set("RPCOpt.RequestsPerSecond", defaultConfig.RPCOpt.RequestsPerSecond)
}

if confTree.Get("P2P.IP") == nil {
confTree.Set("P2P.IP", defaultConfig.P2P.IP)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/harmony/config_migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ Version = "1.0.3"
Enabled = true
IP = "127.0.0.1"
Port = 9800
`)

V1_0_4ConfigDefault = []byte(`
Expand Down
4 changes: 3 additions & 1 deletion cmd/harmony/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ var defaultConfig = harmonyConfig{
Port: nodeconfig.DefaultWSPort,
},
RPCOpt: rpcOptConfig{
DebugEnabled: false,
DebugEnabled: false,
RateLimterEnabled: true,
RequestsPerSecond: nodeconfig.DefaultRPCRateLimit,
},
BLSKeys: blsConfig{
KeyDir: "./.hmy/blskeys",
Expand Down
21 changes: 21 additions & 0 deletions cmd/harmony/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ var (

rpcOptFlags = []cli.Flag{
rpcDebugEnabledFlag,
rpcRateLimiterEnabledFlag,
rpcRateLimitFlag,
}

blsFlags = append(newBLSFlags, legacyBLSFlags...)
Expand Down Expand Up @@ -628,12 +630,31 @@ var (
DefValue: defaultConfig.RPCOpt.DebugEnabled,
Hidden: true,
}

rpcRateLimiterEnabledFlag = cli.BoolFlag{
Name: "rpc.ratelimiter",
Usage: "enable rate limiter for RPCs",
DefValue: defaultConfig.RPCOpt.RateLimterEnabled,
}

rpcRateLimitFlag = cli.IntFlag{
Name: "rpc.ratelimit",
Usage: "the number of requests per second for RPCs",
DefValue: defaultConfig.RPCOpt.RequestsPerSecond,
}
)

func applyRPCOptFlags(cmd *cobra.Command, config *harmonyConfig) {
if cli.IsFlagChanged(cmd, rpcDebugEnabledFlag) {
config.RPCOpt.DebugEnabled = cli.GetBoolFlagValue(cmd, rpcDebugEnabledFlag)
}
if cli.IsFlagChanged(cmd, rpcRateLimiterEnabledFlag) {
config.RPCOpt.RateLimterEnabled = cli.GetBoolFlagValue(cmd, rpcRateLimiterEnabledFlag)
}
if cli.IsFlagChanged(cmd, rpcRateLimitFlag) {
config.RPCOpt.RequestsPerSecond = cli.GetIntFlagValue(cmd, rpcRateLimitFlag)
}

}

// bls flags
Expand Down
36 changes: 35 additions & 1 deletion cmd/harmony/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func TestHarmonyFlags(t *testing.T) {
RosettaEnabled: false,
RosettaPort: 9700,
},
RPCOpt: rpcOptConfig{
DebugEnabled: false,
RateLimterEnabled: true,
RequestsPerSecond: 300,
},
WS: wsConfig{
Enabled: true,
IP: "127.0.0.1",
Expand Down Expand Up @@ -535,7 +540,36 @@ func TestRPCOptFlags(t *testing.T) {
{
args: []string{"--rpc.debug"},
expConfig: rpcOptConfig{
DebugEnabled: true,
DebugEnabled: true,
RateLimterEnabled: true,
RequestsPerSecond: 300,
},
},

{
args: []string{},
expConfig: rpcOptConfig{
DebugEnabled: false,
RateLimterEnabled: true,
RequestsPerSecond: 300,
},
},

{
args: []string{"--rpc.ratelimiter", "--rpc.ratelimit", "1000"},
expConfig: rpcOptConfig{
DebugEnabled: false,
RateLimterEnabled: true,
RequestsPerSecond: 1000,
},
},

{
args: []string{"--rpc.ratelimiter=false", "--rpc.ratelimit", "1000"},
expConfig: rpcOptConfig{
DebugEnabled: false,
RateLimterEnabled: false,
RequestsPerSecond: 1000,
},
},
}
Expand Down
16 changes: 9 additions & 7 deletions cmd/harmony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,15 @@ func setupNodeAndRun(hc harmonyConfig) {

// Parse RPC config
nodeConfig.RPCServer = nodeconfig.RPCServerConfig{
HTTPEnabled: hc.HTTP.Enabled,
HTTPIp: hc.HTTP.IP,
HTTPPort: hc.HTTP.Port,
WSEnabled: hc.WS.Enabled,
WSIp: hc.WS.IP,
WSPort: hc.WS.Port,
DebugEnabled: hc.RPCOpt.DebugEnabled,
HTTPEnabled: hc.HTTP.Enabled,
HTTPIp: hc.HTTP.IP,
HTTPPort: hc.HTTP.Port,
WSEnabled: hc.WS.Enabled,
WSIp: hc.WS.IP,
WSPort: hc.WS.Port,
DebugEnabled: hc.RPCOpt.DebugEnabled,
RateLimiterEnabled: hc.RPCOpt.RateLimterEnabled,
RequestsPerSecond: hc.RPCOpt.RequestsPerSecond,
}

// Parse rosetta config
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/lint v0.0.0-20200302205851-738671d3881b
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
golang.org/x/tools v0.0.0-20210106214847-113979e3529a
google.golang.org/grpc v1.33.2
google.golang.org/protobuf v1.25.0
Expand Down
3 changes: 3 additions & 0 deletions internal/configs/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ type RPCServerConfig struct {
WSPort int

DebugEnabled bool

RateLimiterEnabled bool
RequestsPerSecond int
}

// RosettaServerConfig is the config for the rosetta server
Expand Down
5 changes: 5 additions & 0 deletions internal/configs/node/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const (
DefaultPrometheusPort = 9900
)

const (
// DefaultRateLimit for RPC, the number of requests per second
DefaultRPCRateLimit = 300
)

const (
// rpcHTTPPortOffset is the port offset for RPC HTTP requests
rpcHTTPPortOffset = 500
Expand Down
Loading

0 comments on commit 5e09b89

Please sign in to comment.