使用
from rest_framework.throttling import AnonRateThrottle
from rest_framework.generics import ListAPIView,CreateAPIView,UpdateAPIView,DestroyAPIView,RetrieveAPIView
from api.serializer.articleserializer import ArticleSerializer
from api import models
class ArticleAPIView(ListAPIView):
authentication_classes = []
throttle_classes = [AnonRateThrottle, ]
queryset = models.Article.objects.all()
serializer_class = ArticleSerializer
setting.py频率限制
REST_FRAMEWORK = {
"DEFAULT_THROTTLE_RATES":{
"anon":'10/m'
}
}
节流的原理
当我们请求进来,走到我们频率组件的时候,DRF内部会有一个字典来记录访问者的IP,
以这个访问者的IP为key,value为一个列表,存放访问者每次访问的时间,
把每次访问最新时间放入列表的最前面,记录这样一个数据结构后,通过什么方式限流呢~~
如果我们设置的是10秒内只能访问5次,
1,判断访问者的IP是否在这个请求IP的字典里
2,保证这个列表里都是最近10秒内的访问的时间
判断当前请求时间和列表里最早的(也就是最后的)
如果差大于10秒,说明请求以及不是最近10秒内的,删除掉,继续判断倒数第二个,直到差值小于10秒
3,判断列表的长度(即访问次数),是否大于我们设置的5次,
如果大于就限流,否则放行,并把时间放入列表的最前面
节流的源码分析
执行流程
1.请求进来执行dispatch方法中的initialize_request方法
def initialize_request(self, request, *args, **kwargs):
parser_context = self.get_parser_context(request)
<span class="hljs-keyword">return</span> Request(
request,
parsers=self.get_parsers(),
authenticators=self.get_authenticators(),
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)
</code></pre>
2.执行inital
def initial(self, request, *args, **kwargs):
.......略过的代码
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme
self.perform_authentication(request)
self.check_permissions(request)
self.check_throttles(request)
3.执行check_throttles(request)函数
def check_throttles(self, request):
throttle_durations = []
for throttle in self.get_throttles():
if not throttle.allow_request(request, self):
throttle_durations.append(throttle.wait())
3.1get_throttles函数
def get_throttles(self):
return [throttle() for throttle in self.throttle_classes]
3.2执行allow_request(request, self)
4.然后就执行了节流的原理的逻辑代码
<span class="hljs-keyword">return</span> Request(
request,
parsers=self.get_parsers(),
authenticators=self.get_authenticators(),
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)
</code></pre>
2.执行inital
def initial(self, request, *args, **kwargs):
.......略过的代码
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme
self.perform_authentication(request)
self.check_permissions(request)
self.check_throttles(request)
3.执行check_throttles(request)函数
def check_throttles(self, request):
throttle_durations = []
for throttle in self.get_throttles():
if not throttle.allow_request(request, self):
throttle_durations.append(throttle.wait())
3.1get_throttles函数
def get_throttles(self):
return [throttle() for throttle in self.throttle_classes]
3.2执行allow_request(request, self)
4.然后就执行了节流的原理的逻辑代码
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· Java轻量级代码工程
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战