Django Rest framework的限流实现流程

目录

一 什么是throttle
二 Django REST framework是如何实现throttle的
三 Django REST framework中throttle源码流程

一 什么是throttle

节流也类似于权限,它用来决定一个请求是否被授权。节流表示着一种临时的状态,常常用来控制客户端对一个
API的请求速率。例如,你可以通过限流来限制一个用户在每分钟对一个API的最多访问次数为60次,每天的访问次数为1000次。
 

二 Django REST framework是如何实现throttle的

  1. 在Django REST framework中主要是通过throttling.py文件里面的几个类来实现限流功能的。

  2. 在整个流程上是在dispatch中的调用的initial方法中的self.check_throttles(request)调用到throttle类中的方法

  3. throttle策略的配置:
    全局配置settings.py

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_CLASSES': (  # 定义限流类
        'rest_framework.throttling.AnonRateThrottle',
        'rest_framework.throttling.UserRateThrottle'
    ),
    'DEFAULT_THROTTLE_RATES': {   # 定义限流速率,支持秒、分、时、天的限制
        'anon': '100/day',
        'user': '1000/day'
    }
}
  1. 把限流策略设置在视图函数上
    CBV
from rest_framework.response import Response
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView
class ExampleView(APIView):
    throttle_classes = (UserRateThrottle,)
    def get(self, request, format=None):
        content = {
            'status': 'request was permitted'
        }
        return Response(content)

FBV

@api_view(['GET'])
@throttle_classes([UserRateThrottle])
def example_view(request, format=None):
    content = {
        'status': 'request was permitted'
    }
    return Response(content)

 

三 Django REST framework中throttle源码流程

    1. 调用check_throttles方法,在这个方法中会遍历通过self.get_throttles()获取到的限流对象列表,默认列表里面是空的。也就是说默认不会有限流的策略。

    2. 在视图函数里面配置参数,让其应用上限流策略。我们这里以UserRateThrottle这个限流方法为例。(配置如第二节中的settings.py配置和FBV配置),在这里继续第二步的操作,执行UserRateThrottle对象的allow_request方法。
      由于UserRateThrottle这个类本身没有allow_request方法,所以在其父类SimpleRateThrottle中找这个方法.

    3. 执行allow_request方法,会首先判断是否定义了self.rate。根据self.rate执行,最终回去查找self.scope属性,而且这个属性是必须的。

    4. 在UserRateThrottle中查找到定义的scope="user", 接着执行self.key语句。这条语句最终调用了UserRateThrottle里面的get_cache_key方法。
      此例中我们没有配置authenticate,所有会执行get_cache_key里面的get_indet方法,并最终返回了scope和ident被self.key接收(返回格式:throttle_user_127.0.0.1)。

    5. 返回self.key之后继续执行self.history,self.history会返回客户端的访问记录列表,并根据rate的配置去判断是否是要pop某一条访问记录。并最终根据列表长度和允许的长度做对比,判断客户端当前是否具有访问权限。

    6. 若最终check_throttles返回True,则继续执行dispatch。dispatch之后的操作请参考之前写的django rest framework流程。如果返回False,则会继续执行self.throttled(request, throttle.wait())。

    7. 执行父类SimpleRateThrottle里面的wait方法。这个方法主要用来向客户端返回还需要多少时间可以继续访问的提示。

  1. 原文转载: https://www.cnblogs.com/eric_yi/p/8424424.html
原文地址:https://www.cnblogs.com/hanzeng1993/p/12710977.html