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

feat: per-IP rate limiting #327

Merged
merged 2 commits into from
May 16, 2024
Merged

feat: per-IP rate limiting #327

merged 2 commits into from
May 16, 2024

Conversation

geekbrother
Copy link
Contributor

@geekbrother geekbrother commented May 15, 2024

Description

This PR implements the simple per-IP rate limiting. The rate limiter is implemented in an Axum middleware using the in-memory Moka caching. The default requests per second limit is 10 req/sec which should be enough for the legit users and cut flood requests. Only the X-Forwarded IP is used to prevent blocking the Load Balancer IP.

Resolves #304

How Has This Been Tested?

Due Diligence

  • Breaking change
  • Requires a documentation update
  • Requires a e2e/integration test update

@geekbrother geekbrother self-assigned this May 15, 2024
@arein arein added the accepted The issue has been accepted into the project label May 15, 2024
Copy link

@nopestack nopestack left a comment

Choose a reason for hiding this comment

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

i'm curious about the memory increase using this implementation will incur in the server. Is there a limit to how much memory is allocated to the rate limiter?

@geekbrother
Copy link
Contributor Author

i'm curious about the memory increase using this implementation will incur in the server. Is there a limit to how much memory is allocated to the rate limiter?

Thanks for the comment! The memory is used to store entries of IP addresses and counters. The IP address is coming from X-Forwarded, so from the LB. AWS and other major infrastructures prevent spoofing IP addresses at the router levels (BGP etc.) so if the attacker even has 1M IP addresses it will use a few of megabytes memory. We are not pre-allocating memory in this case, making the size dynamic.

@geekbrother geekbrother merged commit 70e3002 into main May 16, 2024
6 checks passed
@geekbrother geekbrother deleted the feat/rate_limiting branch May 16, 2024 08:14
Copy link
Member

@chris13524 chris13524 left a comment

Choose a reason for hiding this comment

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

I think this PR applies the same 10 req/s rate limit to the push_message handler which needs a different rate limit since this endpoint is used constantly (looks like about 20 req/s) by a single group of relay servers vs individual clients.

Testing it manually it seems to work, but I'm guessing this is luck because there are multiple replicas of both push and relay that would pull the req/s under the threshold.

@nopestack nopestack restored the feat/rate_limiting branch June 11, 2024 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted The issue has been accepted into the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IP-based rate limiting
4 participants