例如:对于客户和员工设置不同的节流率。
可以使用UserRateThrottle
完成吗?例如:
REST_FRAMEWORK['DEFAULT_THROTTLE_CLASSES'] = ('backend.throttles.EmployeeThrottle')
REST_FRAMEWORK['DEFAULT_THROTTLE_RATES'] = {
'user': config.THROTTLE_RATE,
'employee': config.EMPLOYEE_THROTTLE_RATE
}
from rest_framework.throttling import UserRateThrottle
class EmployeeThrottle(UserRateThrottle):
*WHAT TO DO HERE?*
我可以在此EmployeeThrottle
类中以某种方式获取请求,并根据该请求内容设置范围。
编辑:例如:BurstRateThrottle
中的UserRateThrottle,如果以某种方式我可以获得请求,我可以根据此设置范围。
答案 0 :(得分:0)
如果这是每用户类型,那么您应该使用class UserRateThrottle(throttling.BaseThrottle):
scope = 'user'
def allow_request(self, request, view):
return request.user.type == 'user'
class EmployeeRateThrottle(throttling.BaseThrottle):
scope = 'employee'
def allow_request(self, request, view):
return request.user.type == 'employee'
作为基类。我觉得这样的事情会起作用吗?
class X{
public void print(X x){System.out.println("xx");}
public void print(Y y){System.out.println("xy");}
}
class Y extends X{
public void print(X x){System.out.println("yx");}
public void print(Y y){System.out.println("yy");}
public static void main(String[] args){
X x = new Y();
x.print(x);
System.out.println(x.getClass());
}
}
答案 1 :(得分:0)
from rest_framework.throttling import UserRateThrottle
class CustomThrottle(UserRateThrottle):
def __init__(self):
pass
def allow_request(self, request, view):
employee_scope = getattr(view, 'employee', None)
user_scope = getattr(view, 'user', None)
if request.user.profile.is_employee:
if not employee_scope:
return True
self.scope = employee_scope
else:
if not user_scope:
return True
self.scope = user_scope
self.rate = self.get_rate()
self.num_requests, self.duration = self.parse_rate(self.rate)
return super().allow_request(request, view)