Giter Site home page Giter Site logo

alimp5 / django-throttle-requests Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sobotklp/django-throttle-requests

1.0 0.0 0.0 91 KB

a framework for implementing application-level request throttling for Django

License: MIT License

Python 100.00%

django-throttle-requests's Introduction

django-throttle-requests

a framework for implementing application-specific rate-limiting middleware for Django projects

Build Status PyPi

What this module is intended for:

Implementing application-level (or just below) rate-limiting rules. Often, these rules would be expressed as "max # requests within a defined time period". E.g.:

  • an IP address may make at most 1500 requests/day
  • users with an OAuth access token may make 500 reads/hour and 200 writes/hour

What it is not intended for:

A token bucket or leaky bucket filter: intended primarily for traffic shaping, those algorithms are implemented by firewalls and servers such as nginx.

Installation

  1. Install the library with pip:

    sudo pip install django-throttle-requests
  2. Add the directory throttle to your project's PYTHONPATH.
  3. Insert the following configuration into your project's settings:

    THROTTLE_ZONES = {
        'default': {
            'VARY':'throttle.zones.RemoteIP',
            'NUM_BUCKETS':2,  # Number of buckets worth of history to keep. Must be at least 2
            'BUCKET_INTERVAL':15 * 60,  # Period of time to enforce limits.
            'BUCKET_CAPACITY':50,  # Maximum number of requests allowed within BUCKET_INTERVAL
        },
    }
    
    # Where to store request counts.
    THROTTLE_BACKEND = 'throttle.backends.cache.CacheBackend'
    
    # Optional after Redis backend is chosen ('throttle.backends.redispy.RedisBackend')
    THROTTLE_REDIS_HOST = 'localhost'
    THROTTLE_REDIS_PORT = 6379
    THROTTLE_REDIS_DB = 0  
    THROTTLE_REDIS_AUTH = 'pass'
    
    # Normally, throttling is disabled when DEBUG=True. Use this to force it to enabled.
    THROTTLE_ENABLED = True
  4. Use the @throttle decorator to enforce throttling rules on a view:

    from throttle.decorators import throttle
    
    @throttle(zone='default')
    def myview(request):
       ...
  5. Also works with class-based views:

    from django.views.generic import View
    from django.utils.decorators import method_decorator
    
    from throttle.decorators import throttle
    
    class TestView(View):
    
        @method_decorator(throttle(zone='default'))
        def dispatch(self, *args, **kwargs):
            return super(TestView, self).dispatch(*args, **kwargs)
    
        def head(self, request):
            ...
    
        def get(self, request):
            ...
Code

https://github.com/sobotklp/django-throttle-requests

Documentation

https://readthedocs.org/projects/django-throttle-requests/

django-throttle-requests's People

Contributors

sobotklp avatar ebsaral avatar alimp5 avatar bvsn avatar edelvalle avatar jvankoten avatar grncdr avatar rain0r avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.