DRF中关于限流的配置方式
使用限流可以对接口访问的频次进行限制,以减轻服务器压力。
1.使用
可以在配置文件中,使用DEFAULT_THROTTLE_CLASSES和DEFAULT_THROTTLE_RATES进行全局配置
REST_FRAMEWORK = {
# 节流的配置列表
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle', #匿名未认证用户,使用IP区分用户
'rest_framework.throttling.UserRateThrottle' #使用User id 来区分
),
#节流率
'DEFAULT_THROTTLE_RATES': {
'anon': '3/day',# 匿名用户每天三次
'user': '5/day'# 登录用户每天五次
}
}
DEFAULT_THROTTLE_RATES可以使用second,minute,hour或day来指明周期。
2.可选限流类
1) AnonRateThrottle限制所有匿名未认证用户,使用IP区分用户。
使用DEFAULT_THROTTLE_RATES['anon']
来设置频次
2)UserRateThrottle限制认证用户,使用User id 来区分。
使用DEFAULT_THROTTLE_RATES['user']
来设置频次
3)ScopedRateThrottle限制用户对于每个视图的访问频次,使用ip或user id。
例如:
class ContactListView(APIView):
throttle_scope = 'contacts'
...
class ContactDetailView(APIView):
throttle_scope = 'contacts'
...
class UploadView(APIView):
throttle_scope = 'uploads'
...
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.ScopedRateThrottle',
),
'DEFAULT_THROTTLE_RATES': {
'contacts': '1000/day',
'uploads': '20/day'
}
}
3.指定视图设置
from book.models import BookInfo
from book.serializers import BookInfoModelSerializer
from rest_framework.viewsets import ModelViewSet
from rest_framework.throttling import AnonRateThrottle
class BookModelViewSet(ModelViewSet):
queryset = BookInfo.objects.all()
serializer_class = BookInfoModelSerializer
#限流设置
throttle_classes = [AnonRateThrottle]