Source code analysis of Django rest framework (3) -- throttling

Posted by synical21 on Sat, 04 Apr 2020 10:06:33 +0200

Add throttling

Custom throttling method

  • Limit access to 3 times within 60s

(1) Create a new throttle.py under the API folder. The code is as follows:

# utils/throttle.py

from rest_framework.throttling import BaseThrottle
import time
VISIT_RECORD = {}   #Save access records

class VisitThrottle(BaseThrottle):
    '''60s Can only access 3 times in'''
    def __init__(self):
        self.history = None   #Initialize access record

    def allow_request(self,request,view):
        #Get users ip (get_ident)
        remote_addr = self.get_ident(request)
        ctime = time.time()
        #If current IP Not in the access record, add to the record
        if remote_addr not in VISIT_RECORD:
            VISIT_RECORD[remote_addr] = [ctime,]     #Save the form of key value pair
            return True    #True Indicates accessible
        #Get current ip Historical access to
        history = VISIT_RECORD.get(remote_addr)
        #Initialize access record
        self.history = history

        #If there is historical access record, and the earliest access record is more than 60 from the current time s,Delete the earliest access record,
        #As long as True,I've been looping through the oldest access records
        while history and history[-1] < ctime - 60:
            history.pop()
        #If the access record does not exceed three times, insert the current access record to the first location( pop Delete last)
        if len(history) < 3:
            history.insert(0,ctime)
            return True

    def wait(self):
        '''How long will it take to visit'''
        ctime = time.time()
        return 60 - (ctime - self.history[-1])

(2) Global configuration throttling in settings

#Overall situation
REST_FRAMEWORK = {
    #throttle
    "DEFAULT_THROTTLE_CLASSES":['API.utils.throttle.VisitThrottle'],
}

(3) Now visit auth to see the results:

  • If the number of visits exceeds three in 60 s, access will be restricted
  • Prompt how much time is left to access

Next visit

 

Source code analysis of throttling

 (1)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
        #Original request Processing, enriching some functions
        #Request(
        #     request,
        #     parsers=self.get_parsers(),
        #     authenticators=self.get_authenticators(),
        #     negotiator=self.get_content_negotiator(),
        #     parser_context=parser_context
        # )
        #request(Original request,[BasicAuthentications Object,])
        #Getting native request,request._request
        #Get the object of the authentication class, request.authticators
        #1.encapsulation request
        request = self.initialize_request(request, *args, **kwargs)
        self.request = request
        self.headers = self.default_response_headers  # deprecate?

        try:
            #2.Authentication
            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)initial

 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
        #4.Implementation authentication
        self.perform_authentication(request)
        #5.Authority judgement
        self.check_permissions(request)
        #6.Control access frequency
        self.check_throttles(request)

(3)check_throttles

There's a "allow me request" in it

    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):
                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)thtottle_classes

 

Built in throttle class

Above is the custom throttling written. drf has many throttling classes built in, which is more convenient to use.

(1)BaseThrottle

  • I want to write the allow? Request and wait methods
  • Get? Ident is to get ip
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

 

(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   #This value can be customized. You can write anything
    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)

 

We can implement throttling by inheriting the SimpleRateThrottle class. It will be simpler because SimpleRateThrottle has been written for us

(1)throttle.py

from rest_framework.throttling import SimpleRateThrottle

class VisitThrottle(SimpleRateThrottle):
    '''Anonymous users 60 s Can only be accessed three times (according to ip)'''
    scope = 'NBA'   #The value in this is defined by yourself, settings It is configured according to this value Rate

    def get_cache_key(self, request, view):
        #adopt ip Restrictor throttling
        return self.get_ident(request)

class UserThrottle(SimpleRateThrottle):
    '''Login user 60 s Can visit 10 times'''
    scope = 'NBAUser'    #The value in this is defined by yourself, settings It is configured according to this value Rate

    def get_cache_key(self, request, view):
        return request.user.username

(2)settings.py

#Overall situation
REST_FRAMEWORK = {
    #throttle
    "DEFAULT_THROTTLE_CLASSES":['API.utils.throttle.UserThrottle'],   #Global configuration, login user throttling limit (10/m)
    "DEFAULT_THROTTLE_RATES":{
        'NBA':'3/m',         #User 3 not logged in/m,NBA Namely scope Defined values
        'NBAUser':'10/m',    #Login user 10/m,NBAUser Namely scope Defined values
    }
}

(3)views.py

Local configuration method

class AuthView(APIView):
    .
    .    
    .
    # The default throttle is the login user (10/m),AuthView There is no need to log in, and anonymous users' throttling is used here (3/m)
    throttle_classes = [VisitThrottle,]
   .
.
from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from rest_framework.views import APIView
from API import models
from rest_framework.request import Request
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
from API.utils.permission import SVIPPremission,MyPremission
from API.utils.throttle import  VisitThrottle

ORDER_DICT = {
    1:{
        'name':'apple',
        'price':15
    },
    2:{
        'name':'dog',
        'price':100
    }
}

def md5(user):
    import hashlib
    import time
    #Current time, equivalent to generating a random string
    ctime = str(time.time())
    m = hashlib.md5(bytes(user,encoding='utf-8'))
    m.update(bytes(ctime,encoding='utf-8'))
    return m.hexdigest()


class AuthView(APIView):
    '''For user login authentication'''

    authentication_classes = []      #It is empty, which means no authentication is required
    permission_classes = []          #No, it's empty, which means you don't need permission
    # The default throttle is the login user (10/m),AuthView There is no need to log in, and anonymous users' throttling is used here (3/m)
    throttle_classes = [VisitThrottle,]

    def post(self,request,*args,**kwargs):
        ret = {'code':1000,'msg':None}
        try:
            user = request._request.POST.get('username')
            pwd = request._request.POST.get('password')
            obj = models.UserInfo.objects.filter(username=user,password=pwd).first()
            if not obj:
                ret['code'] = 1001
                ret['msg'] = 'Wrong user name or password'
            #Create for user token
            token = md5(user)
            #Update if it exists, create if it doesn't exist
            models.UserToken.objects.update_or_create(user=obj,defaults={'token':token})
            ret['token'] = token
        except Exception as e:
            ret['code'] = 1002
            ret['msg'] = 'Request exception'
        return JsonResponse(ret)


class OrderView(APIView):
    '''
    //Order related business (only SVIP users can view it)
    '''

    def get(self,request,*args,**kwargs):
        self.dispatch
        #request.user
        #request.auth
        ret = {'code':1000,'msg':None,'data':None}
        try:
            ret['data'] = ORDER_DICT
        except Exception as e:
            pass
        return JsonResponse(ret)


class UserInfoView(APIView):
    '''
       //Order related business (common users and VIP users can see it)
       '''
    permission_classes = [MyPremission,]    #Without global permission configuration, you need to write your own local permission here
    def get(self,request,*args,**kwargs):

        print(request.user)
        return HttpResponse('User information')
views.py

Explain:

 

summary

Basic use

Overall situation

   #throttle
    "DEFAULT_THROTTLE_CLASSES":['API.utils.throttle.UserThrottle'],   #Global configuration, login user throttling limit (10/m)
    "DEFAULT_THROTTLE_RATES":{
        'NBA':'3/m',         #User 3 not logged in/m,NBA Namely scope Defined values
        'NBAUser':'10/m',    #Login user 10/m,NBAUser Namely scope Defined values
    }
}

local

throttle_classes = [VisitThrottle,]

Topics: Python Django Attribute encoding