ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

drf -- 限流组件Throttling

2022-05-04 19:09:42  阅读:168  来源: 互联网

标签:THROTTLE Throttling -- DEFAULT framework 限流 rest throttle


限流组件Throttling

  • 可以对接口访问的频次进行限制,以减轻服务器压力,或者实现特定的业务。一般用于付费购买次数,投票等场景使用.

可选限流类

  • 1.AnonRateThrottle :限制所有匿名未认证用户,使用IP区分用户。
    • 使用DEFAULT_THROTTLE_RATES['anon'] 来设置频次
  • 2.UserRateThrottle:限制认证用户,使用User id 来区分。
    • 使用DEFAULT_THROTTLE_RATES['user'] 来设置频次

前两类的使用,在配置文件中配置,

x REST_FRAMEWORK = {    'DEFAULT_THROTTLE_CLASSES': ( # 启用的限制类        
                              'rest_framework.throttling.AnonRateThrottle',        
                              'rest_framework.throttling.UserRateThrottle'    ),    
                        'DEFAULT_THROTTLE_RATES': {   # 限制频率        
                                'anon': '100/day',        
                                'user': '1000/day'    
                        }
}

#DEFAULT_THROTTLE_RATES 可以使用 `second`, `minute`, `hour` 或`day`来指明周期,例如:

'DEFAULT_THROTTLE_RATES': {  # 限制频率
        'anon': '3/minute',
        'user': '10/minute',
        'access': '5/minute', # 这个是自定义限流的频率配置
    }

前两类的使用,在视图函数中配置

  • 可以在具体视图中通过throttle_classess属性来配置,如
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView

class ExampleView(APIView):
    throttle_classes = [UserRateThrottle,]
    ...
  • 3.ScopedRateThrottle :限制用户对于每个视图的访问频次,使用ip或user id
    • <1>.给对应的视图类的 throttle_scope类属性 定义一个名字
    • <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'
    }
}

实例代码:

#全局配置中设置访问频率,settings.py代码:
REST_FRAMEWORK = {
    # 权限[全局配置,会被局部配置覆盖]
    # 'DEFAULT_PERMISSION_CLASSES': (
    #     'rest_framework.permissions.IsAuthenticated',
    # ),

    # 限流
    # 'DEFAULT_THROTTLE_CLASSES': (  # 全局启用的限制类
    #     'rest_framework.throttling.AnonRateThrottle', # 匿名用户,游客
    #     'rest_framework.throttling.UserRateThrottle'  # 登录用户
    # ),
    'DEFAULT_THROTTLE_RATES': {  # 限制频率
        'anon': '3/minute',
        'user': '10/minute',
        'access': '5/minute', # 这个是自定义限流的频率配置
    }
}

#视图代码:

from students.models import Student
from students.serializers import StudentModelSerializer
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import AllowAny,IsAuthenticated,IsAuthenticatedOrReadOnly,IsAdminUser
from .permission import ISMingGe
from rest_framework.throttling import UserRateThrottle,AnonRateThrottle,ScopedRateThrottle
class Students8APIView(ModelViewSet):
    serializer_class = StudentModelSerializer
    queryset = Student.objects.all()
    # 权限配置
    permission_classes = [AllowAny]
    # 限流配置
    # throttle_classes = [AnonRateThrottle,UserRateThrottle]
    # 自定义限流配置
    throttle_classes = [ScopedRateThrottle]
    throttle_scope = 'access'

标签:THROTTLE,Throttling,--,DEFAULT,framework,限流,rest,throttle
来源: https://www.cnblogs.com/zhiqianggege/p/16221940.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有