DRF限流

发布时间 2023-07-18 09:14:21作者: 蕝戀

https://www.django-rest-framework.org/api-guide/throttling/

限制、控制客户端可以向 API 发出的请求的速率。

设置限流策略

默认限制策略可以使用 DEFAULT_THROTTLE_CLASSESDEFAULT_THROTTLE_RATES 设置进行全局设置。例如:

REST_FRAMEWORK = {
    # 设置限流器类
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.AnonRateThrottle',
        'rest_framework.throttling.UserRateThrottle'
    ],
    # 限流速率
    'DEFAULT_THROTTLE_RATES': {
        'anon': '100/day',  # anon:这个key名是AnonRateThrottle类的专属,表示AnonRateThrottle类的限流速率
        'user': '1000/day'  
    }
}

DEFAULT_THROTTLE_RATES 中使用的速率描述可以包括 secondminutehourday 作为节流周期。

视图类局部设置

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)

AnonRateThrottle匿名用户限制

AnonRateThrottle 只会限制未经身份验证的用户。请求的 IP 地址用于生成用于限制的唯一密钥。

限制速率由以下之一确定(按优先级顺序)

  • 类属性rate。可以通过子类化AnonRateThrottle并覆盖rate属性来提供限制速率
  • DEFAULT_THROTTLE_RATES['anon'] 设置。

UserRateThrottle 用户速率限制

UserRateThrottle 会将用户通过 API 限制为给定的请求率。用户 ID 用于生成唯一的密钥来进行限制。未经身份验证的请求将回退到使用传入请求的 IP 地址来生成唯一的密钥来进行限制。

限制速率由以下之一确定(按优先级顺序)

  • 类属性rate。可以通过子类化AnonRateThrottle并覆盖rate属性来提供限制速率
  • DEFAULT_THROTTLE_RATES['user'] 设置。

一个视图类可以由多个UserRateThrottle,需要子类化多个UserRateThrottle,然后重写scope属性。

使用场景:

针对每用户每分钟限制100请求

针对每用户每小时限制1000请求

class UserMinRateThrottle(UserRateThrottle):
    scope = 'limit_per_minute'

class UserHourRateThrottle(UserRateThrottle):
    scope = 'limit_per_minute'
    
    
REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_CLASSES': [
        'example.throttles.UserMinRateThrottle',
        'example.throttles.UserHourRateThrottle'
    ],
    'DEFAULT_THROTTLE_RATES': {
        'limit_per_minute': '60/min',
        'limit_per_minute': '1000/hour'
    }
}

ScopedRateThrottle 范围速率限制

用于限制特定的API访问,仅当正在访问的视图包含 .throttle_scope 属性时才会应用此限制。

然后,通过将请求的“throttle_scope ”与唯一的用户 ID 或 IP 地址连接起来,形成唯一的限制键。

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'
    }
}

用户对 ContactListViewContactDetailView 的请求每天的请求总数将被限制为 1000 个。

用户对 UploadView 的请求将被限制为每天 20 个请求。

高并发注意事项

内置的节流阀实现对竞争条件开放,因此在高并发情况下,它们可能允许一些额外的请求通过。

如果您的项目依赖于保证并发请求期间的请求数量,则您将需要实现自己的节流类。

自定义限流类

https://www.django-rest-framework.org/api-guide/throttling/#custom-throttles