Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable rate limiting on local testnet #2005

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions testnet/launcher/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func (t *Testnet) Start() error {
gateway.WithTenNodeHTTPPort(13010),
gateway.WithTenNodeWSPort(13011),
gateway.WithTenNodeHost("validator-host"),
gateway.WithRateLimitUserComputeTime(0), // disable rate limiting for local network
gateway.WithDockerImage("testnetobscuronet.azurecr.io/obscuronet/obscuro_gateway:latest"),
),
)
Expand Down
21 changes: 15 additions & 6 deletions testnet/launcher/gateway/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package gateway

import "time"

// Option is a function that applies configs to a Config Object
type Option = func(c *Config)

// Config holds the properties that configure the package
type Config struct {
tenNodeHost string
tenNodeHTTPPort int
tenNodeWSPort int
gatewayHTTPPort int
gatewayWSPort int
dockerImage string
tenNodeHost string
tenNodeHTTPPort int
tenNodeWSPort int
gatewayHTTPPort int
gatewayWSPort int
rateLimitUserComputeTime time.Duration
dockerImage string
}

func NewGatewayConfig(opts ...Option) *Config {
Expand Down Expand Up @@ -58,3 +61,9 @@ func WithGatewayWSPort(i int) Option {
c.gatewayWSPort = i
}
}

func WithRateLimitUserComputeTime(d time.Duration) Option {
return func(c *Config) {
c.rateLimitUserComputeTime = d
}
}
1 change: 1 addition & 0 deletions testnet/launcher/gateway/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (n *DockerGateway) Start() error {
"--nodeHost", n.cfg.tenNodeHost,
"--dbType", "sqlite",
"--logPath", "sys_out",
"--rateLimitUserComputeTime", fmt.Sprintf("%d", n.cfg.rateLimitUserComputeTime),
}

_, err := docker.StartNewContainer("gateway", n.cfg.dockerImage, cmds, []int{n.cfg.gatewayHTTPPort, n.cfg.gatewayWSPort}, nil, nil, nil)
Expand Down
36 changes: 25 additions & 11 deletions tools/walletextension/ratelimiter/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var zeroUUID uuid.UUID

// AddRequest adds a new request interval to a user's current requests and returns the UUID.
func (rl *RateLimiter) AddRequest(userID common.Address, interval RequestInterval) uuid.UUID {
// If the userComputeTime is 0, do nothing (rate limiting is disabled)
if rl.GetUserComputeTime() == 0 {
return zeroUUID
}
rl.mu.Lock()
defer rl.mu.Unlock()

Expand All @@ -45,6 +49,11 @@ func (rl *RateLimiter) AddRequest(userID common.Address, interval RequestInterva

// SetRequestEnd updates the end time of a request interval given its UUID.
func (rl *RateLimiter) SetRequestEnd(userID common.Address, id uuid.UUID) {
// If the userComputeTime is 0, do nothing (rate limiting is disabled)
if rl.GetUserComputeTime() == 0 {
return
}

if user, userExists := rl.users[userID]; userExists {
if request, requestExists := user.CurrentRequests[id]; requestExists {
rl.mu.Lock()
Expand All @@ -62,8 +71,8 @@ func (rl *RateLimiter) SetRequestEnd(userID common.Address, id uuid.UUID) {

// CountOpenRequests counts the number of requests without an End time set.
func (rl *RateLimiter) CountOpenRequests(userID common.Address) int {
rl.mu.Lock()
defer rl.mu.Unlock()
rl.mu.RLock()
defer rl.mu.RUnlock()

var count int
if user, exists := rl.users[userID]; exists {
Expand Down Expand Up @@ -100,7 +109,7 @@ func (rl *RateLimiter) SumComputeTime(userID common.Address) time.Duration {
}

type RateLimiter struct {
mu sync.Mutex
mu sync.RWMutex
users map[common.Address]*RateLimitUser
userComputeTime time.Duration
window time.Duration
Expand All @@ -126,15 +135,15 @@ func (rl *RateLimiter) IncrementRateLimitedRequests() {

// GetMaxConcurrentRequest returns the maximum number of concurrent requests allowed.
func (rl *RateLimiter) GetMaxConcurrentRequest() uint32 {
rl.mu.Lock()
defer rl.mu.Unlock()
rl.mu.RLock()
defer rl.mu.RUnlock()
return rl.maxConcurrentRequests
}

// GetUserComputeTime returns the user compute time
func (rl *RateLimiter) GetUserComputeTime() time.Duration {
rl.mu.Lock()
defer rl.mu.Unlock()
rl.mu.RLock()
defer rl.mu.RUnlock()
return rl.userComputeTime
}

Expand All @@ -146,8 +155,13 @@ func NewRateLimiter(rateLimitUserComputeTime time.Duration, rateLimitWindow time
maxConcurrentRequests: concurrentRequestsLimit,
logger: logger,
}
go rl.logRateLimitedStats()
go rl.periodicPrune()

// If the userComputeTime is 0 (rate limiting is disabled) we don't need to prune and log rate limited stats
if rl.GetUserComputeTime() != 0 {
go rl.logRateLimitedStats()
go rl.periodicPrune()
}

return rl
}

Expand Down Expand Up @@ -203,10 +217,10 @@ func (rl *RateLimiter) PruneRequests() {
}
}

// periodically prunes the requests that have ended before the rate limiter's window every 10 * window milliseconds
// periodically prunes the requests that have ended before the rate limiter's window milliseconds
func (rl *RateLimiter) periodicPrune() {
for {
time.Sleep(rl.window * 10)
time.Sleep(rl.window / 2)
rl.PruneRequests()
}
}
Expand Down
Loading