-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom ratelimits per org (#5004)
# What this PR does This PR refactors Throttling for public API and integrations API and allows to specify organization ratelimits. ## Which issue(s) this PR closes Related to [issue link here] <!-- *Note*: If you want the issue to be auto-closed once the PR is merged, change "Related to" to "Closes" in the line above. If you have more than one GitHub issue that this PR closes, be sure to preface each issue link with a [closing keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue). This ensures that the issue(s) are auto-closed once the PR has been merged. --> ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [ ] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes.
- Loading branch information
Showing
15 changed files
with
268 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from rest_framework.throttling import UserRateThrottle | ||
from common.api_helpers.custom_rate_scoped_throttler import CustomRateUserThrottler | ||
|
||
|
||
class DemoAlertThrottler(UserRateThrottle): | ||
class DemoAlertThrottler(CustomRateUserThrottler): | ||
scope = "send_demo_alert" | ||
rate = "30/m" |
54 changes: 13 additions & 41 deletions
54
engine/apps/api/throttlers/phone_verification_throttler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,21 @@ | ||
from common.api_helpers.custom_rate_scoped_throttler import CustomRateScopedThrottler | ||
from common.api_helpers.custom_rate_scoped_throttler import CustomRateOrganizationThrottler, CustomRateUserThrottler | ||
|
||
|
||
class GetPhoneVerificationCodeThrottlerPerUser(CustomRateScopedThrottler): | ||
def get_scope(self): | ||
return "get_phone_verification_code_per_user" | ||
class GetPhoneVerificationCodeThrottlerPerUser(CustomRateUserThrottler): | ||
rate = "5/10m" | ||
scope = "get_phone_verification_code_per_user" | ||
|
||
def get_throttle_limits(self): | ||
return 5, 10 * 60 | ||
|
||
class VerifyPhoneNumberThrottlerPerUser(CustomRateUserThrottler): | ||
rate = "50/10m" | ||
scope = "verify_phone_number_per_user" | ||
|
||
class VerifyPhoneNumberThrottlerPerUser(CustomRateScopedThrottler): | ||
def get_scope(self): | ||
return "verify_phone_number_per_user" | ||
|
||
def get_throttle_limits(self): | ||
return 50, 10 * 60 | ||
class GetPhoneVerificationCodeThrottlerPerOrg(CustomRateOrganizationThrottler): | ||
rate = "50/10m" | ||
scope = "get_phone_verification_code_per_org" | ||
|
||
|
||
class GetPhoneVerificationCodeThrottlerPerOrg(CustomRateScopedThrottler): | ||
def get_scope(self): | ||
return "get_phone_verification_code_per_org" | ||
|
||
def get_throttle_limits(self): | ||
return 50, 10 * 60 | ||
|
||
def get_cache_key(self, request, view): | ||
if request.user.is_authenticated: | ||
ident = request.user.organization.pk | ||
else: | ||
ident = self.get_ident(request) | ||
|
||
return self.cache_format % {"scope": self.scope, "ident": ident} | ||
|
||
|
||
class VerifyPhoneNumberThrottlerPerOrg(CustomRateScopedThrottler): | ||
def get_scope(self): | ||
return "verify_phone_number_per_org" | ||
|
||
def get_throttle_limits(self): | ||
return 50, 10 * 60 | ||
|
||
def get_cache_key(self, request, view): | ||
if request.user.is_authenticated: | ||
ident = request.user.organization.pk | ||
else: | ||
ident = self.get_ident(request) | ||
|
||
return self.cache_format % {"scope": self.scope, "ident": ident} | ||
class VerifyPhoneNumberThrottlerPerOrg(CustomRateOrganizationThrottler): | ||
rate = "50/10m" | ||
scope = "verify_phone_number_per_org" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.