-
Notifications
You must be signed in to change notification settings - Fork 55
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 1 commit
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,51 @@ | ||
| from rest_framework.throttling import SimpleRateThrottle | ||
| from django.conf import settings | ||
| from api.models import Organisation | ||
|
|
||
|
|
||
rohan-chaturvedi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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): | ||
| if not request.user.is_authenticated or not request.auth: | ||
| return None | ||
rohan-chaturvedi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Identify the user or service account | ||
| ident = self.get_ident(request) | ||
| 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}" | ||
|
|
||
| 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): | ||
| return settings.PLAN_RATE_LIMITS.get(plan, settings.PLAN_RATE_LIMITS["DEFAULT"]) | ||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
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) |
||
Uh oh!
There was an error while loading. Please reload this page.