-
Notifications
You must be signed in to change notification settings - Fork 56
feat: implement plan-based API rate limits #696
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?
Changes from all commits
6e67085
ac1c030
427d080
88b0d4f
e9418a6
94da683
6072d8d
d53d29e
5b36b44
d03cd04
7775600
c62b692
fbc4cd3
2a9f219
194808f
13a39b4
debce9f
9fb5869
9eb321a
7a8c5bb
9658ea7
5980d35
6874524
2323d30
22ea5b6
3b68e4c
dc9de3e
d65d36c
00548fa
9c72103
f843ec5
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,58 @@ | ||
| from rest_framework.throttling import SimpleRateThrottle | ||
| from django.conf import settings | ||
|
|
||
| CLOUD_HOSTED = settings.APP_HOST == "cloud" | ||
|
|
||
|
|
||
| class PlanBasedRateThrottle(SimpleRateThrottle): | ||
| """ | ||
| Limits the rate of API calls based on the Organisation's plan. | ||
| Uses the pre-fetched organisation data from request.auth to avoid DB lookups. | ||
| """ | ||
|
|
||
| scope = "plan_based" | ||
|
|
||
| def get_cache_key(self, request, view): | ||
| # Identify the user or service account | ||
| ident = self.get_ident(request) | ||
|
|
||
| if request.user.is_authenticated and request.auth: | ||
| if request.auth.get("org_member"): | ||
| ident = f"user_{request.auth['org_member'].id}" | ||
| elif request.auth.get("service_account"): | ||
| ident = f"sa_{request.auth['service_account'].id}" | ||
| elif request.auth.get("service_token"): | ||
| ident = f"st_{request.auth['service_token'].id}" | ||
| else: | ||
| ident = f"anon_{ident}" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing fallback in cache key for unmatched auth typesIn |
||
|
|
||
| return self.cache_format % {"scope": self.scope, "ident": ident} | ||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def allow_request(self, request, view): | ||
| """ | ||
| Override allow_request to dynamically set the rate based on the request user's plan. | ||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| # Default fallback (reads from REST_FRAMEWORK['DEFAULT_THROTTLE_RATES']['plan_based']) | ||
| new_rate = self.get_rate() | ||
|
|
||
| if request.user.is_authenticated and request.auth: | ||
| env = request.auth.get("environment") | ||
| if env: | ||
| try: | ||
| plan = env.app.organisation.plan | ||
| new_rate = self.get_rate_for_plan(plan) | ||
| except AttributeError: | ||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pass | ||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Update the throttle configuration for this specific request | ||
| self.rate = new_rate | ||
| self.num_requests, self.duration = self.parse_rate(self.rate) | ||
|
|
||
| return super().allow_request(request, view) | ||
|
|
||
| @staticmethod | ||
| def get_rate_for_plan(plan): | ||
| # If self-hosted return the default rate limit. If not set, this will disable throttling | ||
| if not CLOUD_HOSTED: | ||
| return settings.PLAN_RATE_LIMITS["DEFAULT"] | ||
| return settings.PLAN_RATE_LIMITS.get(plan, settings.PLAN_RATE_LIMITS["DEFAULT"]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plan rate limit fallback fails for unconfigured plansIn Additional Locations (1) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import os | ||
| import django | ||
|
|
||
| # Set environment variables required for settings.py to import successfully | ||
| os.environ.setdefault("ALLOWED_HOSTS", "localhost") | ||
| os.environ.setdefault("ALLOWED_ORIGINS", "http://localhost") | ||
|
|
||
| # Set dummy Redis values so settings.py generates a valid URL (e.g. redis://localhost:6379/1) | ||
| os.environ.setdefault("REDIS_HOST", "localhost") | ||
| os.environ.setdefault("REDIS_PORT", "6379") | ||
|
|
||
| # Set dummy database config | ||
| os.environ.setdefault("DATABASE_HOST", "localhost") | ||
| os.environ.setdefault("DATABASE_PORT", "5432") | ||
| os.environ.setdefault("DATABASE_NAME", "dummy_db") | ||
| os.environ.setdefault("DATABASE_USER", "dummy_user") | ||
| os.environ.setdefault("DATABASE_PASSWORD", "dummy_password") | ||
|
|
||
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings") | ||
|
|
||
|
|
||
| def pytest_configure(): | ||
| django.setup() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pytest==8.3.4 | ||
| pytest-django==4.11.1 | ||
| pytest-cov==7.0.0 | ||
| Faker==37.4.0 | ||
| colorama==0.4.6 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [pytest] | ||
| python_files = tests.py test_*.py *_tests.py |
Uh oh!
There was an error while loading. Please reload this page.