Skip to main content

A Django framework for application-layer rate limiting

Project description

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

Build Status

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
    
    # Force throttling when DEBUG=True
    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/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-throttle-requests-0.5.6.tar.gz (6.5 kB view details)

Uploaded Source

File details

Details for the file django-throttle-requests-0.5.6.tar.gz.

File metadata

  • Download URL: django-throttle-requests-0.5.6.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.5

File hashes

Hashes for django-throttle-requests-0.5.6.tar.gz
Algorithm Hash digest
SHA256 fa80b67b7d2e0775cc363e308a98fb0a020c6c97cfe5269fc16e215a05578e26
MD5 7efdae084250f0006ae32c08b033b2d5
BLAKE2b-256 21f35dd6c255537ce2af5c214e563d3df744fc7c2a32dda49105e2a3f8d2234d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page