【发布时间】:2020-09-10 00:54:07
【问题描述】:
我正在尝试将所有用户的限制速率设置为每 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.throttle_rates
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)
你知道问题出在哪里吗?
【问题讨论】:
标签: python django django-rest-framework