Django框架对访问流量的控制:THROTTLE

Django的限流Throtting:

1.对接口访问的频次进行限制,减少对服务器的压力

进行全局配置:

REST_FRAMEWORK = {
    "DEFAULT_THROTTLE_CLASS":(
    	'rest_framework.throttling.AnonRateThrottle',#未登录用户
        'rest_framework.throttling.UserRateThrottle'#登录用户
    ),
    "DEFAULT_THROTTLE_RATES':{
    'anon':'100/day',    #未登录用户 每天100次
    'user':'1000/day'	  #登录用户  每天1000次
}
}
#DEFAULT_THROTTLE_RATES也可以使用second,minutes,hour或day指明周期

2.指明指定视图的访问次数:

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'
    }
}
posted @ 2020-03-20 16:40  醉醺醺的  阅读(598)  评论(0编辑  收藏  举报