Skip to content

Config param to disable websocket rate limit #562

New issue

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

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

Already on GitHub? # to your account

Merged
merged 6 commits into from
Apr 2, 2025
Merged
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 components/webapi/component.go
Original file line number Diff line number Diff line change
@@ -303,6 +303,7 @@ func provide(c *dig.Container) error {
ParamsWebAPI.Limits.Jsonrpc.WebsocketRateLimitBurst,
ParamsWebAPI.Limits.Jsonrpc.WebsocketConnectionCleanupDuration,
ParamsWebAPI.Limits.Jsonrpc.WebsocketClientBlockDuration,
ParamsWebAPI.Limits.Jsonrpc.WebsocketRateLimitEnabled,
),
)

1 change: 1 addition & 0 deletions components/webapi/params.go
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ type ParametersJSONRPC struct {

WebsocketRateLimitMessagesPerSecond int `default:"20" usage:"the websocket rate limit (messages per second)"`
WebsocketRateLimitBurst int `default:"5" usage:"the websocket burst limit"`
WebsocketRateLimitEnabled bool `default:"true" usage:"enable rate limiting on the websocket"`
WebsocketConnectionCleanupDuration time.Duration `default:"5m" usage:"defines in which interval stale connections will be cleaned up"`
WebsocketClientBlockDuration time.Duration `default:"5m" usage:"the duration a misbehaving client will be blocked"`
}
1 change: 1 addition & 0 deletions config_defaults.json
Original file line number Diff line number Diff line change
@@ -136,6 +136,7 @@
"maxLogsInResult": 10000,
"websocketRateLimitMessagesPerSecond": 20,
"websocketRateLimitBurst": 5,
"websocketRateLimitEnabled": true,
"websocketConnectionCleanupDuration": "5m",
"websocketClientBlockDuration": "5m"
}
18 changes: 10 additions & 8 deletions documentation/docs/configuration.md
Original file line number Diff line number Diff line change
@@ -464,14 +464,15 @@ Example:

### <a id="webapi_limits_jsonrpc"></a> Jsonrpc

| Name | Description | Type | Default value |
| ----------------------------------- | -------------------------------------------------------------- | ------ | ------------- |
| maxBlocksInLogsFilterRange | Maximum amount of blocks in eth_getLogs filter range | int | 1000 |
| maxLogsInResult | Maximum amount of logs in eth_getLogs result | int | 10000 |
| websocketRateLimitMessagesPerSecond | The websocket rate limit (messages per second) | int | 20 |
| websocketRateLimitBurst | The websocket burst limit | int | 5 |
| websocketConnectionCleanupDuration | Defines in which interval stale connections will be cleaned up | string | "5m" |
| websocketClientBlockDuration | The duration a misbehaving client will be blocked | string | "5m" |
| Name | Description | Type | Default value |
| ----------------------------------- | -------------------------------------------------------------- | ------- | ------------- |
| maxBlocksInLogsFilterRange | Maximum amount of blocks in eth_getLogs filter range | int | 1000 |
| maxLogsInResult | Maximum amount of logs in eth_getLogs result | int | 10000 |
| websocketRateLimitMessagesPerSecond | The websocket rate limit (messages per second) | int | 20 |
| websocketRateLimitBurst | The websocket burst limit | int | 5 |
| websocketRateLimitEnabled | Enable rate limiting on the websocket | boolean | true |
| websocketConnectionCleanupDuration | Defines in which interval stale connections will be cleaned up | string | "5m" |
| websocketClientBlockDuration | The duration a misbehaving client will be blocked | string | "5m" |

Example:

@@ -500,6 +501,7 @@ Example:
"maxLogsInResult": 10000,
"websocketRateLimitMessagesPerSecond": 20,
"websocketRateLimitBurst": 5,
"websocketRateLimitEnabled": true,
"websocketConnectionCleanupDuration": "5m",
"websocketClientBlockDuration": "5m"
}
4 changes: 4 additions & 0 deletions packages/evm/jsonrpc/server.go
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ type Parameters struct {
Logs LogsLimits
WebsocketRateLimitMessagesPerSecond int
WebsocketRateLimitBurst int
WebsocketRateLimitEnabled bool
WebsocketConnectionCleanupDuration time.Duration
WebsocketClientBlockDuration time.Duration
}
@@ -26,6 +27,7 @@ func NewParameters(
websocketRateLimitBurst int,
websocketConnectionCleanupDuration time.Duration,
websocketClientBlockDuration time.Duration,
WebsocketRateLimitEnabled bool,
) *Parameters {
return &Parameters{
Logs: LogsLimits{
@@ -34,6 +36,7 @@ func NewParameters(
},
WebsocketRateLimitMessagesPerSecond: websocketRateLimitMessagesPerSecond,
WebsocketRateLimitBurst: websocketRateLimitBurst,
WebsocketRateLimitEnabled: WebsocketRateLimitEnabled,
WebsocketConnectionCleanupDuration: websocketConnectionCleanupDuration,
WebsocketClientBlockDuration: websocketClientBlockDuration,
}
@@ -47,6 +50,7 @@ func ParametersDefault() *Parameters {
},
WebsocketRateLimitMessagesPerSecond: 20,
WebsocketRateLimitBurst: 5,
WebsocketRateLimitEnabled: true,
WebsocketConnectionCleanupDuration: 5 * time.Minute,
WebsocketClientBlockDuration: 5 * time.Minute,
}
11 changes: 10 additions & 1 deletion packages/webapi/services/evm_websocket.go
Original file line number Diff line number Diff line change
@@ -104,7 +104,16 @@ func (w *websocketContext) getRateLimiter(remoteIP string) *activityRateLimiter
return w.rateLimiters[remoteIP]
}

limiter := rate.NewLimiter(rate.Limit(w.jsonRPCParams.WebsocketRateLimitMessagesPerSecond), w.jsonRPCParams.WebsocketRateLimitBurst)
limit := rate.Limit(w.jsonRPCParams.WebsocketRateLimitMessagesPerSecond)
burst := w.jsonRPCParams.WebsocketRateLimitBurst

if !w.jsonRPCParams.WebsocketRateLimitEnabled {
limit = rate.Inf
burst = 0
}

limiter := rate.NewLimiter(limit, burst)

w.rateLimiters[remoteIP] = newActivityRateLimiter(limiter)

return w.rateLimiters[remoteIP]
26 changes: 26 additions & 0 deletions packages/webapi/services/ratelimit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package services

import (
"github.com/iotaledger/hive.go/log"
"github.com/iotaledger/wasp/packages/evm/jsonrpc"
"github.com/stretchr/testify/require"
"testing"
)

// TestWebsocketContext_RateLimitDisabled ensures no rate limiting occurs when
// WebsocketRateLimitEnabled is set to false.
func TestWebsocketContext_RateLimitDisabled(t *testing.T) {
testLogger := log.NewLogger(log.WithName("TestWebsocketContext_RateLimitDisabled"))
params := jsonrpc.ParametersDefault()
params.WebsocketRateLimitEnabled = false

wsCtx := newWebsocketContext(testLogger, params)

remoteIP := "127.0.0.1"
rateLimiter := wsCtx.getRateLimiter(remoteIP)

// Because the rate limiter is disabled, every call to .Allow() should return true.
for i := 0; i < 50; i++ {
require.True(t, rateLimiter.Allow(), "disabled rate limiter should always allow")
}
}
Loading