Django Rest Framework源碼剖析(三)-----頻率控制

1、簡介

承接上篇文章Django Rest Framework源碼剖析(二)-----權限,當服務的接口被頻繁調用,致使資源緊張怎麼辦呢?固然或許有不少解決辦法,好比:負載均衡、提升服務器配置、經過代理限制訪問頻率等,可是django rest framework自身就提供了訪問頻率的控制,能夠從代碼自己作控制。html

2、頻率控制內部原理概述

django rest framework 中頻率控制基本原理基於訪問次數和時間,經過計算實現,固然咱們也能夠本身定義頻率控制方法。基本原理以下:python

啓用頻率,DRF內部會有一個字典記錄來訪者的IP,以及訪問時間最近幾(經過配置)次的訪問時間,這樣確保每次列表中最後一個元素都是該用戶請求的最先時間,形式以下:算法

{ IP1:[第三次請求時間,第二次請求時間,第一次請求時間,], IP2:[第二次請求時間,第一次請求時間,], ..... }

舉例說明,好比我如今配置了5秒內只能訪問2次,每次請求到達頻率控制時候先判斷請求者IP是否已經在這個請求字典中,若存在,在判斷用戶請求5秒內的請求次數,若次數小於等於2,則容許請求,若大於2,則超過頻率,不容許請求。sql

關於請求頻率的的算法(以5秒內最多訪問兩次爲例):數據庫

1.首先刪除掉列表裏5秒以前的請求,循環判斷當前請求時間和最先請求時間之差記做t1,若t1大於5則表明列表中最先的請求已經在5秒外了,刪除掉,繼續判斷倒數第二個請求,直到t1小於5.django

2.當確保請求列表中只有5秒內請求時候,接着判斷其請求次數(列表長度),若長度大於2,則證實超過5秒內訪問超過2次了,則不容許,不然,經過並將這次訪問時間插入到列表最前面,做爲最新訪問時間。api

3、基本使用

一樣,先來了解下頻率控制的使用方法,後面在分析源碼緩存

1.在utils目錄下新創建文件,throttle.py,添加頻率控制爲每分鐘只能訪問5次服務器

#!/usr/bin/env python3 #_*_ coding:utf-8 _*_ #Author:wd
from rest_framework.throttling import SimpleRateThrottle class VisitThrottle(SimpleRateThrottle): """5秒內最多訪問三次""" scope = "WD"  #settings配置文件中的key,用於獲取配置的頻率

    def get_cache_key(self, request, view): return self.get_ident(request)

2.settings.py中配置全局頻率控制app

REST_FRAMEWORK = { #頻率控制配置
    "DEFAULT_THROTTLE_CLASSES":['utils.throttle.VisitThrottle'],   #全局配置,
    "DEFAULT_THROTTLE_RATES":{ 'WD':'5/m',         #速率配置每分鐘不能超過5次訪問,WD是scope定義的值,
 } }

urls.py

from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^api/v1/auth', views.AuthView.as_view()), url(r'^api/v1/order', views.OrderView.as_view()), ]

models.py

from django.db import models class UserInfo(models.Model): user_type_choice = ( (1,"普通用戶"), (2,"會員"), ) user_type = models.IntegerField(choices=user_type_choice) username = models.CharField(max_length=32,unique=True) password = models.CharField(max_length=64) class UserToken(models.Model): user = models.OneToOneField(to=UserInfo) token = models.CharField(max_length=64)

訂單視圖

class OrderView(APIView): '''查看訂單'''
    from utils.permissions import MyPremission authentication_classes = [Authentication,]    #添加認證
    permission_classes = [MyPremission,]           #添加權限控制
    def get(self,request,*args,**kwargs): #request.user
        #request.auth
        ret = {'code':1000,'msg':"你的訂單已經完成",'data':"買了一個mac"} return JsonResponse(ret,safe=True)

使用postman驗證以下圖,能夠看到頻率限制已經起做用了。

4、頻率控制源碼剖析

在前面幾篇文章中已經分析了DRF的認證、權限源碼,頻率控制也同樣也從APIView的dispatch方法提及,參考註解:

dispatch()

def dispatch(self, request, *args, **kwargs): """ `.dispatch()` is pretty much the same as Django's regular dispatch, but with extra hooks for startup, finalize, and exception handling. """ self.args = args self.kwargs = kwargs #對原始request進行加工,豐富了一些功能
        #Request(
        # request,
        # parsers=self.get_parsers(),
        # authenticators=self.get_authenticators(),
        # negotiator=self.get_content_negotiator(),
        # parser_context=parser_context
        # )
        #request(原始request,[BasicAuthentications對象,])
        #獲取原生request,request._request
        #獲取認證類的對象,request.authticators
        #1.封裝request
        request = self.initialize_request(request, *args, **kwargs) self.request = request self.headers = self.default_response_headers  # deprecate?

        try: self.initial(request, *args, **kwargs) # Get the appropriate handler method
            if request.method.lower() in self.http_method_names: handler = getattr(self, request.method.lower(), self.http_method_not_allowed) else: handler = self.http_method_not_allowed response = handler(request, *args, **kwargs) except Exception as exc: response = self.handle_exception(exc) self.response = self.finalize_response(request, response, *args, **kwargs) return self.response

2.執行inital方法,initial方法中執行check_throttles則開始頻率控制

def initial(self, request, *args, **kwargs): """ Runs anything that needs to occur prior to calling the method handler. """ self.format_kwarg = self.get_format_suffix(**kwargs) # Perform content negotiation and store the accepted info on the request
        neg = self.perform_content_negotiation(request) request.accepted_renderer, request.accepted_media_type = neg # Determine the API version, if versioning is in use.
        version, scheme = self.determine_version(request, *args, **kwargs) request.version, request.versioning_scheme = version, scheme # Ensure that the incoming request is permitted
        #2.實現認證
 self.perform_authentication(request) #3.權限判斷
 self.check_permissions(request) #4.頻率限制
        self.check_throttles(request)

3.下面是check_throttles源碼,與認證、權限同樣採用列表對象方式,經過判斷allow_request方法返回值判斷頻率是否經過

def check_throttles(self, request): """ Check if request should be throttled. Raises an appropriate exception if the request is throttled. """
        for throttle in self.get_throttles(): #循環頻率控制類結果
            if not throttle.allow_request(request, self): #判斷其中的allow_requestf返回結果,true則頻率經過,不然返回等待多少秒能夠訪問
                self.throttled(request, throttle.wait())

4.get_throttles方法,採用列表生成式生成頻率控制對象,與認證、權限一直

def get_throttles(self): """ Instantiates and returns the list of throttles that this view uses. """
        return [throttle() for throttle in self.throttle_classes] #列表生成式生成控制頻率對象列表

5.self.throttle_classes屬性獲取

class APIView(View): # The following policies may be set at either globally, or per-view.
    renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES parser_classes = api_settings.DEFAULT_PARSER_CLASSES authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES  throttle_classes = api_settings.DEFAULT_THROTTLE_CLASSES #頻率控制全局配置 permission_classes = api_settings.DEFAULT_PERMISSION_CLASSES content_negotiation_class = api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS metadata_class = api_settings.DEFAULT_METADATA_CLASS versioning_class = api_settings.DEFAULT_VERSIONING_CLASS

6.經過以上分析,知道了頻率控制是經過判斷每一個類中的allow_request放法的返回值來判斷頻率是否經過,下面咱們來看看咱們所使用的SimpleRateThrottle怎麼實現的,分析部分請看註解:

SimpleRateThrottle類源碼:

class SimpleRateThrottle(BaseThrottle): """ A simple cache implementation, that only requires `.get_cache_key()` to be overridden. The rate (requests / seconds) is set by a `rate` attribute on the View class. The attribute is a string of the form 'number_of_requests/period'. Period should be one of: ('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day') Previous request information used for throttling is stored in the cache. """ cache = default_cache # 存放請求時間,相似與示例中的大字典,這裏使用的是django的緩存 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 get_cache_key(self, request, view):
# 獲取請求的key標識,必需要有不然會報錯,這裏能夠重寫,使用用戶的用戶名、或其餘做爲key,在示例中使用的get_ident方法用戶獲取用戶IP做爲key
""" Should return a unique cache-key which can be used for throttling. Must be overridden. May return `None` if the request should not be throttled. """ raise NotImplementedError('.get_cache_key() must be overridden') def get_rate(self): # 獲取配置文件的配置速率 """ Determine the string representation of the allowed request rate. """ if not getattr(self, 'scope', None): # 經過獲取共有屬性scope來獲取配置的速率 msg = ("You must set either `.scope` or `.rate` for '%s' throttle" % self.__class__.__name__) raise ImproperlyConfigured(msg) try: return self.THROTTLE_RATES[self.scope] except KeyError: msg = "No default throttle rate set for '%s' scope" % self.scope raise ImproperlyConfigured(msg) def parse_rate(self, rate): # 格式化速率 """ Given the request rate string, return a two tuple of: <allowed number of requests>, <period of time in seconds> """ 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]] # 轉換時間爲數字,示例配置的5/m,m轉爲60秒 return (num_requests, duration) def allow_request(self, request, view): # 判斷請求的速率是否經過 """ Implement the check to see if the request should be throttled. On success calls `throttle_success`. On failure calls `throttle_failure`. """ if self.rate is None: return True self.key = self.get_cache_key(request, view) if self.key is None: return True self.history = self.cache.get(self.key, []) self.now = self.timer() # Drop any requests from the history which have now passed the # throttle duration 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): # 頻率經過返回true """ Inserts the current request's timestamp along with the key into the cache. """ self.history.insert(0, self.now) self.cache.set(self.key, self.history, self.duration) return True def throttle_failure(self): # 不經過返回false """ Called when a request to the API has failed due to throttling. """ return False def wait(self): # 返回等待時間 """ Returns the recommended next request time in seconds. """ 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)

get_ident方法源碼,該方法用於獲取請求的IP:

def get_ident(self, request): """ Identify the machine making the request by parsing HTTP_X_FORWARDED_FOR if present and number of proxies is > 0. If not use all of HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR. """ xff = request.META.get('HTTP_X_FORWARDED_FOR') remote_addr = request.META.get('REMOTE_ADDR') #這裏request是封裝之後的requst,django原生的是request._request.META 這樣也能夠獲取
        num_proxies = api_settings.NUM_PROXIES if num_proxies is not None: if num_proxies == 0 or xff is None: return remote_addr addrs = xff.split(',') client_addr = addrs[-min(num_proxies, len(addrs))] return client_addr.strip() return ''.join(xff.split()) if xff else remote_addr
5、內置頻率控制類

DRF內置了多種頻率控制類提供咱們使用,其核心原理都是經過判斷request_allow方法返回值來判斷頻率是否經過,經過wait方法返回等待時間。

1.BaseThrottle:最基本的頻率控制須要重寫allow_request方法和wait方法

class BaseThrottle(object): """ Rate throttling of requests. """

    def allow_request(self, request, view): """ Return `True` if the request should be allowed, `False` otherwise. """
        raise NotImplementedError('.allow_request() must be overridden') def get_ident(self, request): """ Identify the machine making the request by parsing HTTP_X_FORWARDED_FOR if present and number of proxies is > 0. If not use all of HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR. """ xff = request.META.get('HTTP_X_FORWARDED_FOR') remote_addr = request.META.get('REMOTE_ADDR') num_proxies = api_settings.NUM_PROXIES if num_proxies is not None: if num_proxies == 0 or xff is None: return remote_addr addrs = xff.split(',') client_addr = addrs[-min(num_proxies, len(addrs))] return client_addr.strip() return ''.join(xff.split()) if xff else remote_addr def wait(self): """ Optionally, return a recommended number of seconds to wait before the next request. """
        return None
class BaseThrottle(object)

2.SimpleRateThrottle:示例中已經使用,並對源碼和原理進行了分析。

class SimpleRateThrottle(BaseThrottle): """ A simple cache implementation, that only requires `.get_cache_key()` to be overridden. The rate (requests / seconds) is set by a `rate` attribute on the View class. The attribute is a string of the form 'number_of_requests/period'. Period should be one of: ('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day') Previous request information used for throttling is stored in the cache. """ 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 get_cache_key(self, request, view): """ Should return a unique cache-key which can be used for throttling. Must be overridden. May return `None` if the request should not be throttled. """
        raise NotImplementedError('.get_cache_key() must be overridden') def get_rate(self): """ Determine the string representation of the allowed request rate. """
        if not getattr(self, 'scope', None): msg = ("You must set either `.scope` or `.rate` for '%s' throttle" % self.__class__.__name__) raise ImproperlyConfigured(msg) try: return self.THROTTLE_RATES[self.scope] except KeyError: msg = "No default throttle rate set for '%s' scope" % self.scope raise ImproperlyConfigured(msg) def parse_rate(self, rate): """ Given the request rate string, return a two tuple of: <allowed number of requests>, <period of time in seconds> """
        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): """ Implement the check to see if the request should be throttled. On success calls `throttle_success`. On failure calls `throttle_failure`. """
        if self.rate is None: return True self.key = self.get_cache_key(request, view) if self.key is None: return True self.history = self.cache.get(self.key, []) self.now = self.timer() # Drop any requests from the history which have now passed the
        # throttle duration
        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): """ Inserts the current request's timestamp along with the key into the cache. """ self.history.insert(0, self.now) self.cache.set(self.key, self.history, self.duration) return True def throttle_failure(self): """ Called when a request to the API has failed due to throttling. """
        return False def wait(self): """ Returns the recommended next request time in seconds. """
        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)
View Code

3.AnonRateThrottle:匿名用戶頻率控制

class AnonRateThrottle(SimpleRateThrottle): """ Limits the rate of API calls that may be made by a anonymous users. The IP address of the request will be used as the unique cache key. """ 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) }
AnonRateThrottle

4.UserRateThrottle:基於SimpleRateThrottle,對用戶的頻率控制

class UserRateThrottle(SimpleRateThrottle): """ Limits the rate of API calls that may be made by a given user. The user id will be used as a unique cache key if the user is authenticated. For anonymous requests, the IP address of the request will be used. """ scope = 'user'

    def get_cache_key(self, request, view): if request.user.is_authenticated: ident = request.user.pk else: ident = self.get_ident(request) return self.cache_format % { 'scope': self.scope, 'ident': ident }
UserRateThrottle
6、自定義頻率控制

自定義頻率控制無非實現request_allow方法和wait方法,你能夠根據實際需求來定製你的頻率控制,下面是示例:

from rest_framework.throttling import BaseThrottle import time REQUEST_RECORD = {}  # 訪問記錄,可以使用nosql數據庫


class VisitThrottle(BaseThrottle): '''60s內最多能訪問5次'''

    def __init__(self): self.history = None def allow_request(self, request, view): # 獲取用戶ip (get_ident)
        remote_addr = self.get_ident(request) ctime = time.time() if remote_addr not in REQUEST_RECORD: REQUEST_RECORD[remote_addr] = [ctime, ]  # 保持請求的時間,形式{ip:[時間,]}
            return True  # True表示能夠訪問
        # 獲取當前ip的歷史訪問記錄
        history = REQUEST_RECORD.get(remote_addr) self.history = history while history and history[-1] < ctime - 60: # while循環確保每列表中是最新的60秒內的請求
 history.pop() # 訪問記錄小於5次,將本次請求插入到最前面,做爲最新的請求
        if len(history) < 5: history.insert(0, ctime) return True def wait(self): '''返回等待時間''' ctime = time.time() return 60 - (ctime - self.history[-1])
7、總結

1.使用方法:

  • 繼承BaseThrottle類
  • 重寫request_allow方法和wait方法,request_allow方法返回true表明經過,不然拒絕,wait返回等待的時間

2.配置

###全局使用
 REST_FRAMEWORK = { #頻率控制配置
    "DEFAULT_THROTTLE_CLASSES":['utils.throttle.VisitThrottle'],   #全局配置,
    "DEFAULT_THROTTLE_RATES":{ 'WD':'5/m',         #速率配置每分鐘不能超過5次訪問,WD是scope定義的值
 } } ##單一視圖使用
throttle_classes = [VisitThrottle,] ##優先級
單一視圖>全局
相關文章
相關標籤/搜索