-
Notifications
You must be signed in to change notification settings - Fork 10
Feature/rate limiter #134
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
base: main
Are you sure you want to change the base?
Feature/rate limiter #134
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||
|
|
||||||
| """ | ||||||
| Rate limiting configuration for Lenny. | ||||||
| This module provides rate limiting settings that are compatible between | ||||||
| Nginx limit_req and slowapi, ensuring consistent TTL values. | ||||||
| :copyright: (c) 2015 by AUTHORS | ||||||
| :license: see LICENSE for more details | ||||||
| """ | ||||||
|
|
||||||
| import os | ||||||
| from slowapi import Limiter, _rate_limit_exceeded_handler | ||||||
| from slowapi.util import get_remote_address | ||||||
| from slowapi.errors import RateLimitExceeded | ||||||
|
|
||||||
| # Rate limit window in seconds - must be compatible between nginx and slowapi | ||||||
| # Using 60 seconds (1 minute) as a standard window | ||||||
| RATE_LIMIT_WINDOW = int(os.environ.get('RATE_LIMIT_WINDOW', 60)) | ||||||
|
|
||||||
| def _parse_rate_limit(env_var: str, default: int) -> int: | ||||||
| """ | ||||||
| Parse rate limit from environment variable. | ||||||
| Supports both integer format (100) and string format ('100/minute'). | ||||||
| Returns the integer count of requests. | ||||||
| """ | ||||||
| value = os.environ.get(env_var, str(default)) | ||||||
| if '/' in str(value): | ||||||
| return int(value.split('/')[0]) | ||||||
| return int(value) | ||||||
|
Comment on lines
+21
to
+30
|
||||||
|
|
||||||
| # Supports both integer (100) and string ('100/minute') formats for backward compatibility | ||||||
| RATE_LIMIT_GENERAL_COUNT = _parse_rate_limit('RATE_LIMIT_GENERAL', 100) | ||||||
| RATE_LIMIT_LENIENT_COUNT = _parse_rate_limit('RATE_LIMIT_LENIENT', 300) | ||||||
| RATE_LIMIT_STRICT_COUNT = _parse_rate_limit('RATE_LIMIT_STRICT', 20) | ||||||
|
|
||||||
| RATE_LIMIT_GENERAL = f'{RATE_LIMIT_GENERAL_COUNT}/minute' | ||||||
| RATE_LIMIT_LENIENT = f'{RATE_LIMIT_LENIENT_COUNT}/minute' | ||||||
| RATE_LIMIT_STRICT = f'{RATE_LIMIT_STRICT_COUNT}/minute' | ||||||
|
|
||||||
| # Create the limiter instance | ||||||
| limiter = Limiter( | ||||||
| key_func=get_remote_address, | ||||||
| default_limits=[RATE_LIMIT_GENERAL], | ||||||
|
||||||
| default_limits=[RATE_LIMIT_GENERAL], | |
| default_limits=[], |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |||||
| ) | ||||||
| from lenny.core.readium import ReadiumAPI | ||||||
| from lenny.core.models import Item | ||||||
| from lenny.core.ratelimit import limiter, RATE_LIMIT_GENERAL, RATE_LIMIT_LENIENT, RATE_LIMIT_STRICT | ||||||
|
||||||
| from lenny.core.ratelimit import limiter, RATE_LIMIT_GENERAL, RATE_LIMIT_LENIENT, RATE_LIMIT_STRICT | |
| from lenny.core.ratelimit import limiter, RATE_LIMIT_GENERAL, RATE_LIMIT_STRICT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RATE_LIMIT_WINDOWis defined but never used anywhere in this module or imported elsewhere. Consider removing it or documenting its intended use case if it's meant for future Nginx configuration compatibility.