Skip to content

Commit

Permalink
proxyd: Allow disabling backend rate limiting
Browse files Browse the repository at this point in the history
The backend rate limiter is in place to protect upstreams like the sequencer. However, in many cases it isn't needed and it causes unnecessary requests to Redis. This PR allows this to be disabled, and disables this by default.
  • Loading branch information
mslipper committed Oct 13, 2022
1 parent 1108348 commit e9f2c70
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-windows-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/proxyd': minor
---

Allow disabling backend rate limiter
28 changes: 27 additions & 1 deletion proxyd/backend_rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"math"
"sync"
"time"

Expand Down Expand Up @@ -256,5 +257,30 @@ func randStr(l int) string {
return hex.EncodeToString(b)
}

type ServerRateLimiter struct {
type NoopBackendRateLimiter struct{}

var noopBackendRateLimiter = &NoopBackendRateLimiter{}

func (n *NoopBackendRateLimiter) IsBackendOnline(name string) (bool, error) {
return true, nil
}

func (n *NoopBackendRateLimiter) SetBackendOffline(name string, duration time.Duration) error {
return nil
}

func (n *NoopBackendRateLimiter) IncBackendRPS(name string) (int, error) {
return math.MaxInt, nil
}

func (n *NoopBackendRateLimiter) IncBackendWSConns(name string, max int) (bool, error) {
return true, nil
}

func (n *NoopBackendRateLimiter) DecBackendWSConns(name string) error {
return nil
}

func (n *NoopBackendRateLimiter) FlushBackendWSConns(names []string) error {
return nil
}
15 changes: 8 additions & 7 deletions proxyd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ type MetricsConfig struct {
}

type RateLimitConfig struct {
UseRedis bool `toml:"use_redis"`
BaseRate int `toml:"base_rate"`
BaseInterval TOMLDuration `toml:"base_interval"`
ExemptOrigins []string `toml:"exempt_origins"`
ExemptUserAgents []string `toml:"exempt_user_agents"`
ErrorMessage string `toml:"error_message"`
MethodOverrides map[string]*RateLimitMethodOverride `toml:"method_overrides"`
UseRedis bool `toml:"use_redis"`
EnableBackendRateLimiter bool `toml:"enable_backend_rate_limiter"`
BaseRate int `toml:"base_rate"`
BaseInterval TOMLDuration `toml:"base_interval"`
ExemptOrigins []string `toml:"exempt_origins"`
ExemptUserAgents []string `toml:"exempt_user_agents"`
ErrorMessage string `toml:"error_message"`
MethodOverrides map[string]*RateLimitMethodOverride `toml:"method_overrides"`
}

type RateLimitMethodOverride struct {
Expand Down
5 changes: 4 additions & 1 deletion proxyd/integration_tests/testdata/backend_rate_limit.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ max_rps = 2
backends = ["good"]

[rpc_method_mappings]
eth_chainId = "main"
eth_chainId = "main"

[rate_limit]
enable_backend_limiter = true
12 changes: 8 additions & 4 deletions proxyd/proxyd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ func Start(config *Config) (func(), error) {

var lim BackendRateLimiter
var err error
if redisClient == nil {
log.Warn("redis is not configured, using local rate limiter")
lim = NewLocalBackendRateLimiter()
if config.RateLimit.EnableBackendRateLimiter {
if redisClient != nil {
lim = NewRedisRateLimiter(redisClient)
} else {
log.Warn("redis is not configured, using local rate limiter")
lim = NewLocalBackendRateLimiter()
}
} else {
lim = NewRedisRateLimiter(redisClient)
lim = noopBackendRateLimiter
}

// While modifying shared globals is a bad practice, the alternative
Expand Down

0 comments on commit e9f2c70

Please sign in to comment.