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

Rate limiting #236

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Rate limiting #236

wants to merge 1 commit into from

Conversation

jkisor
Copy link
Contributor

@jkisor jkisor commented Jan 10, 2024

To config, edit use RateLimiter, limit: 100, seconds: 60 in app.rb. The default is 100 requests per 60 seconds per IP address.

To test, Run in production mode. RACK_ENV=production rackup config.ru

Here's a log when using use RateLimiter, limit: 2, seconds: 15

127.0.0.1 - - [09/Jan/2024:18:59:31 -0500] "GET / HTTP/1.1" 200 34396 0.3447
127.0.0.1 - - [09/Jan/2024:18:59:34 -0500] "GET / HTTP/1.1" 200 34396 0.1299
127.0.0.1 - - [09/Jan/2024:18:59:35 -0500] "GET / HTTP/1.1" 429 19 0.0022

127.0.0.1 - - [09/Jan/2024:19:00:01 -0500] "GET / HTTP/1.1" 200 34396 0.1048
127.0.0.1 - - [09/Jan/2024:19:00:02 -0500] "GET / HTTP/1.1" 200 34396 0.1015
127.0.0.1 - - [09/Jan/2024:19:00:03 -0500] "GET / HTTP/1.1" 429 19 0.0021
Screenshot 2024-01-09 185623

@@ -28,6 +28,7 @@
class TreeStats < Sinatra::Base
configure :production do
use Sentry::Rack::CaptureExceptions
use RateLimiter, limit: 100, seconds: 60
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This means 100 requests are allowed every 60 seconds per IP address.
These numbers aren't based on any real data.

We could configure by environment variable.

private

def build_key(ip)
"rate_limiter:#{ip}:#{Time.now.to_i / @seconds}"
Copy link
Contributor Author

@jkisor jkisor Jan 10, 2024

Choose a reason for hiding this comment

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

You may decide on a different key.

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.

None yet

1 participant