File tree 2 files changed +17
-1
lines changed
2 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -101,3 +101,9 @@ class AccountInFreezeError(BaseHTTPException):
101
101
"This email account has been deleted within the past 30 days"
102
102
"and is temporarily unavailable for new account registration."
103
103
)
104
+
105
+
106
+ class CompilanceRateLimitError (BaseHTTPException ):
107
+ error_code = "compilance_rate_limit"
108
+ description = "Rate limit exceeded for downloading compliance report."
109
+ code = 429
Original file line number Diff line number Diff line change 6
6
wait_fixed )
7
7
8
8
from extensions .ext_database import db
9
+ from libs .helper import RateLimiter
9
10
from models .account import TenantAccountJoin , TenantAccountRole
10
11
11
12
12
13
class BillingService :
13
14
base_url = os .environ .get ("BILLING_API_URL" , "BILLING_API_URL" )
14
15
secret_key = os .environ .get ("BILLING_API_SECRET_KEY" , "BILLING_API_SECRET_KEY" )
15
16
17
+ compliance_download_rate_limiter = RateLimiter ("compliance_download_rate_limiter" , 3 , 60 )
18
+
16
19
@classmethod
17
20
def get_info (cls , tenant_id : str ):
18
21
params = {"tenant_id" : tenant_id }
@@ -107,11 +110,18 @@ def get_compliance_download_link(
107
110
ip : str ,
108
111
device_info : str ,
109
112
):
113
+ limiter_key = f"{ account_id } :{ tenant_id } "
114
+ if cls .compliance_download_rate_limiter .is_rate_limited (limiter_key ):
115
+ from controllers .console .error import CompilanceRateLimitError
116
+ raise CompilanceRateLimitError ()
117
+
110
118
json = {
111
119
"doc_name" : doc_name ,
112
120
"account_id" : account_id ,
113
121
"tenant_id" : tenant_id ,
114
122
"ip_address" : ip ,
115
123
"device_info" : device_info ,
116
124
}
117
- return cls ._send_request ("POST" , "/compliance/download" , json = json )
125
+ res = cls ._send_request ("POST" , "/compliance/download" , json = json )
126
+ cls .compliance_download_rate_limiter .increment (limiter_key )
127
+ return res
You can’t perform that action at this time.
0 commit comments