我正在尝试将所有用户的限制速率设置为每15分钟100个请求
问题是,当我重写AnonRateThrottle
和UserRateThrottle
时,节流根本不起作用
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': { # I've lowered the rates to test it
'anon': '2/min',
'user': '2/min'
}
}
工作完美
这不起作用:
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
'DEFAULT_THROTTLE_CLASSES': [
'api.throttle_rates.AnonHundredPerFifteenMinutesThrottle',
'api.throttle_rates.UserHundredPerFifteenMinutesThrottle',
],
}
api.节流率
from rest_framework.throttling import AnonRateThrottle, UserRateThrottle
class AnonHundredPerFifteenMinutesThrottle(AnonRateThrottle):
def parse_rate(self, rate):
return (2, 60)
class UserHundredPerFifteenMinutesThrottle(UserRateThrottle):
def parse_rate(self, rate):
return (2,60)
你知道问题出在哪里吗
如果您查看allow_request函数
self.rate
始终是None
,因为您没有设置它, 因此,请求是允许的相关问题 更多 >
编程相关推荐