标签:split insert 循环 settings 将不 情况 对比 无法 sel
节流类似于权限,它确定是否应授权请求。节流指示临时状态,并用于控制客户端可以向API发出的请求的速率。
还有情况可能是 ,由于某些服务特别耗费资源,因此您需要在API的不同部分施加不同的约束。
频率限制在认证、权限之后
在settings配置文件中设置规定时间段内可以访问的次数
REST_FRAMEWORK = {
"DEFAULT_THROTTLE_RATES": {"anon": '10/m'},
}
在需要节流的类中加throttle_classes
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.throttling import AnonRateThrottle,BaseThrottle
class ArticleView(APIView):
throttle_classes = [AnonRateThrottle,]
def get(self,request,*args,**kwargs):
return Response('文章列表')
class ArticleDetailView(APIView):
def get(self,request,*args,**kwargs):
return Response('文章列表')
请求过来先执行dispatch方法
class APIView(View):
permission_classes = api_settings.DEFAULT_PERMISSION_CLASSES
def dispatch(self, request, *args, **kwargs):
# 封装request对象...
self.initial(request, *args, **kwargs)
# 通过反射执行视图中的方法...
initial方法过渡
def initial(self, request, *args, **kwargs):
# 版本的处理...
# 认证...
# 权限判断
self.check_throttles(request) # 节流
将所有的节流类实例化成对象列表
def get_throttles(self):
return [throttle() for throttle in self.throttle_classes]
循环执行每个对象的allow_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())
执行allow_request方法,AnonRateThrottle没有allow_request,去父类找
class AnonRateThrottle(SimpleRateThrottle):
scope = 'anon'
def get_cache_key(self, request, view):
if request.user.is_authenticated:
return None # Only throttle unauthenticated requests.
return self.cache_format % {
'scope': self.scope,
'ident': self.get_ident(request)
}
执行SimpleRateThrottle类中allow_request方法
class SimpleRateThrottle(BaseThrottle):
cache = default_cache
timer = time.time
cache_format = 'throttle_%(scope)s_%(ident)s'
scope = None
THROTTLE_RATES = api_settings.DEFAULT_THROTTLE_RATES
def __init__(self):
if not getattr(self, 'rate', None):
self.rate = self.get_rate()
self.num_requests, self.duration = self.parse_rate(self.rate)
def parse_rate(self, rate):
if rate is None:
return (None, None)
num, period = rate.split('/')
num_requests = int(num)
duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
return (num_requests, duration)
def allow_request(self, request, view):
if self.rate is None:
return True
# 获取请求用户的IP
self.key = self.get_cache_key(request, view)
if self.key is None:
return True
# 根据IP获取他的所有访问记录,[]
self.history = self.cache.get(self.key, [])
self.now = self.timer()
while self.history and self.history[-1] <= self.now - self.duration:
self.history.pop()
if len(self.history) >= self.num_requests:
return self.throttle_failure()
return self.throttle_success()
def throttle_success(self):
self.history.insert(0, self.now)
self.cache.set(self.key, self.history, self.duration)
return True
def throttle_failure(self):
return False
def wait(self):
if self.history:
remaining_duration = self.duration - (self.now - self.history[-1])
else:
remaining_duration = self.duration
available_requests = self.num_requests - len(self.history) + 1
if available_requests <= 0:
return None
return remaining_duration / float(available_requests)
标签:split insert 循环 settings 将不 情况 对比 无法 sel
原文地址:https://www.cnblogs.com/liubing8/p/11941224.html