项目作者: romantomjak

项目描述 :
A fixed window rate limiting based on Redis
高级语言: Python
项目地址: git://github.com/romantomjak/django-redis-ratelimit.git
创建时间: 2017-10-31T23:37:17Z
项目社区:https://github.com/romantomjak/django-redis-ratelimit

开源协议:MIT License

下载


django-redis-ratelimit

Build Status

A fixed window rate limiting based on Redis


Requirements

  • Python >= 3.6
  • Django >= 1.11
  • Redis

Installation

To install django-redis-ratelimit, simply:

  1. $ pip install django-redis-ratelimit

NB! django-redis-ratelimit requires a running Redis server. See Redis’s quickstart
for installation instructions.

Getting started

First, add the middleware to your settings.py:

  1. MIDDLEWARE = [
  2. # ...
  3. 'redis_ratelimit.middleware.RateLimitMiddleware',
  4. ]

this will make sure that end user sees the HTTP 429 response.

Next, apply the ratelimit decorator to the view:

  1. from django.http import HttpResponse
  2. from redis_ratelimit import ratelimit
  3. @ratelimit(rate='5/m')
  4. def index(request):
  5. return HttpResponse("Hello World!")

Memory requirements

For this example we will assume that each key takes up roughly 250 bytes and each value is 4 bytes:

  1. 250 + 4 * 1 million unique hits = ~254 Megabytes

Notes

License

MIT