Login

limit view request rate decorator

Author:
anatoliy.larin
Posted:
May 25, 2009
Language:
Python
Version:
1.0
Tags:
django cache view decorator limit-request-rate closure cell
Score:
0 (after 0 ratings)

Limit rate request decorator for view.

Authenificated user can't request decorated view often then timeout.

Usage:

@limit_request_rate(time_beetween_request_sec)
def my_view(request):
    ...

get_cell_value from here

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import new

from django.core.cache import cache
from django.conf import settings

from django.http import HttpResponse


class HttpResponseServiceUnavailable(HttpResponse):
    status_code = 503


def get_cell_value(cell):
    def make_closure_that_returns_value(use_this_value):
        def closure_that_returns_value():
            return use_this_value
        return closure_that_returns_value
    dummy_function = make_closure_that_returns_value(0)
    dummy_function_code = dummy_function.func_code
    our_function = new.function(dummy_function_code, {}, None, None, (cell,))
    value_from_cell = our_function()
    return value_from_cell


def get_decorated_function(function):
    """ Returns actual decorated function in decorators stack """
    while function.func_closure is not None:
        function = get_cell_value(function.func_closure[0])
    return function
 
class limit_request_rate(object):
    """
    Decorator for view that limit request rate for view for concrete user.
    Anonymous users not limited. 
    @todo: differentiate anonymous users by IP
    """    
    CACHE_VAR_PREFIX = "limit_request_rate_"
    
    def __init__(self, timeout = None):
        self.timeout = timeout or settings.DEFAULT_REQUEST_TIMEOUT
    
    def __call__(self, function):        
        def actual_decorator(request, *args, **kwargs):
            if request.user.is_authenticated():                
                dec_func = get_decorated_function(function)
                cache_key = self.CACHE_VAR_PREFIX + str(request.user.id) + dec_func
.func_name + dec_func.func_code.co_filename
                if not cache.get(cache_key):
                    cache.set(cache_key, 1, self.timeout)
                    return function(request, *args, **kwargs)
                else:
                    return HttpResponseServiceUnavailable()            
            return function(request, *args, **kwargs)
        return actual_decorator

More like this

  1. Decorator to limit request rates to individual views by jensbreit 6 years, 10 months ago
  2. Rate Limiting Decorator by levigross 4 years, 8 months ago
  3. Decorator that limits request methods by schinckel 6 years ago
  4. Retrieve Latitude & Longitude for an Address from Google Geocoder V3 by whardeman 4 years, 9 months ago
  5. PK->objects in view signature by AdamKG 7 years, 4 months ago

Comments

Please login first before commenting.