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

[api] Add ratelimit for websocket API #4031

Merged
merged 5 commits into from
Apr 15, 2024
Merged

[api] Add ratelimit for websocket API #4031

merged 5 commits into from
Apr 15, 2024

Conversation

millken
Copy link
Contributor

@millken millken commented Dec 21, 2023

Description

This PR introduces a rate limit for the websocket API to control the maximum number of messages a client can send per second. This is to ensure the stability and performance of the API by preventing any potential abuse or overload.

api:
  ...
  websocketMaxRateMessages: 5 #the maximum number of messages per second per client

Fixes #(issue)

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

Copy link

codecov bot commented Dec 21, 2023

Codecov Report

Attention: Patch coverage is 66.66667% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 76.19%. Comparing base (d716bb9) to head (f7cecb5).
Report is 26 commits behind head on master.

❗ Current head f7cecb5 differs from pull request most recent head ebe7716. Consider uploading reports for the commit ebe7716 to get more accurate results

Files Patch % Lines
api/websocket.go 57.14% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4031      +/-   ##
==========================================
- Coverage   76.51%   76.19%   -0.32%     
==========================================
  Files         340      332       -8     
  Lines       29273    28330     -943     
==========================================
- Hits        22397    21587     -810     
+ Misses       5761     5643     -118     
+ Partials     1115     1100      -15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@envestcc envestcc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can rate limit be implemented in gateway?

@millken
Copy link
Contributor Author

millken commented Dec 22, 2023

Can rate limit be implemented in gateway?

No, because the websocket is a long connection, the gateway cannot ratelimit requests inside the connection.

api/serverV2.go Outdated
@@ -65,7 +66,8 @@ func NewServerV2(

wrappedWeb3Handler := otelhttp.NewHandler(newHTTPHandler(web3Handler), "web3.jsonrpc")

wrappedWebsocketHandler := otelhttp.NewHandler(NewWebsocketHandler(web3Handler), "web3.websocket")
limiter := rate.NewLimiter(rate.Limit(cfg.WebsocketMaxRateMessages), 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it means allowing 5 req/s and only 1 req at the same time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it means allowing 5 req/s and only 1 req at the same time?

once a client is connected, the WebSocket server will limit the request rate to 5 req/s for this client. There is no limit to the number of client sides connected to the server( can limit in gateway)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, what does the second param 1 means?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, what does the second param 1 means?

The second parameter determines the maximum burst events that the limiter can tolerate. For example, if the value of b is 10, then even if 10 events suddenly occur in a short time. set to 1 means that the rate limit is based on the setting of the first parameter, and too many bursts are not allowed.

"net/http"
"sync"
"time"

"github.com/gorilla/websocket"
"go.uber.org/zap"
"golang.org/x/time/rate"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golang.org/x/time/rate already meets our needs, i think no need to import a third-party library

Copy link

sonarcloud bot commented Jan 5, 2024

Quality Gate Failed Quality Gate failed

Failed conditions

6.6% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

api/config.go Outdated Show resolved Hide resolved
Copy link

sonarcloud bot commented Apr 15, 2024

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@millken millken merged commit 0291751 into iotexproject:master Apr 15, 2024
3 checks passed
@millken millken deleted the websockets_ratelimit branch April 15, 2024 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants