Upgrade Notes¶
See also the CHANGELOG.
From 3.x to 4.0¶
Quickly:
Rename imports from
from ratelimittofrom django_ratelimitCheck all uses of the
@ratelimitdecorator. If theblockargument is not set, addblock=Falseto retain the current behavior.block=Truemay optionally be removed.Django versions below 3.2 and Python versions below 3.7 are no longer supported.
Package name changed¶
To disambiguate with other ratelimit packages on PyPI and resolve distro
packaging issues, the package name has been changed from ratelimit to
django_ratelimit. See issue 214 for more information on this change.
When upgrading, import paths need to change to use the new package name.
Old:
from ratelimit.decorators import ratelimit
from ratelimit import ALL, UNSAFE
New:
from django_ratelimit.decorators import ratelimit
from django_ratelimit import ALL, UNSAFE
Default decorator behavior changed¶
In previous versions, the @ratelimit decorator did not block traffic that
exceeded the rate limits by default. This has been reversed, and now the
default behavior is to block requests once a rate limit has been exceeded.
The old behavior of annotating the request object with a .limited property
can be restored by explicitly setting block=False on the decorator.
Historically, the first use cases Django Ratelimit was built to support were HTML views like login and password-reset pages, rather than APIs. In these cases, rate limiting is often done based on user input like the username or email address. Instead of blocking requests, which could lead to a denial-of-service (DOS) attack against particular users, it is common to trigger some additional security measures to prevent brute-force attacks, like a CAPTCHA, temporary account lock, or even notify those users via email.
However, it has become obvious that the majority of views using the
@ratelimit decorator tend to be either specific pages or API endpoints that
do not present a DOS attack vector against other users, and that a more
intuitive default behavior is to block requests that exceed the limits. Since
there tend to only be a couple of pages or routes for uses like authentication,
it makes more sense to opt those uses out of blocking, than opt all the
others in.
From 2.0 to 3.0¶
Quickly:
Ratelimit now supports Django >=1.11 and Python >=3.4.
@ratelimitno longer works directly on class methods, add@method_decorator.RatelimitMixinis gone, migrate to@method_decorator.Moved
is_ratelimtedmethod fromratelimit.utilstoratelimit.core.
@ratelimit decorator on class methods¶
In 3.0, the decorator has been simplified and must now be used with
Django’s excellent @method_decorator utility. Migrating should be
relatively straight-forward:
from django.views.generic import View
from ratelimit.decorators import ratelimit
class MyView(View):
@ratelimit(key='ip', rate='1/m', method='GET')
def get(self, request):
pass
changes to
from django.utils.decorators import method_decorator
from django.views.generic import View
from ratelimit.decorators import ratelimit
class MyView(View):
@method_decorator(ratelimit(key='ip', rate='1/m', method='GET'))
def get(self, request):
pass
RatelimitMixin¶
RatelimitMixin is a vestige of an older version of Ratelimit that
did not support multiple rates per method. As such, it is significantly
less powerful than the current @ratelimit decorator. To migrate to
the decorator, use the @method_decorator from Django:
class MyView(RatelimitMixin, View):
ratelimit_key = 'ip'
ratelimit_rate = '10/m'
ratelimit_method = 'GET'
def get(self, request):
pass
becomes
class MyView(View):
@method_decorator(ratelimit(key='ip', rate='10/m', method='GET'))
def get(self, request):
pass
The major benefit is that it is now possible to apply multiple limits to the same method, as with :ref:`the decorator <usage-decorator>`_.
From <=0.4 to 0.5¶
Quickly:
Rate limits are now counted against fixed, instead of sliding, windows.
Rate limits are no longer shared between methods by default.
Change
ip=Truetokey='ip'.Drop
ip=False.A key must always be specified. If using without an explicit key, add
key='ip'.Change
fields='foo'topost:fooorget:foo.Change
keys=callabletokey=callable.Change
skip_ifto a callablerate=<callable>method (see Rates.Change
RateLimitMixintoRatelimitMixin(note the lowercasel).Change
ratelimit_ip=Truetoratelimit_key='ip'.Change
ratelimit_fields='foo'topost:fooorget:foo.Change
ratelimit_keys=callabletoratelimit_key=callable.
Fixed windows¶
Before 0.5, rates were counted against a sliding window, so if the
rate limit was 1/m, and three requests came in:
1.2.3.4 [09/Sep/2014:12:25:03] ...
1.2.3.4 [09/Sep/2014:12:25:53] ... <RATE LIMITED>
1.2.3.4 [09/Sep/2014:12:25:59] ... <RATE LIMITED>
Even though the third request came nearly two minutes after the first request, the second request moved the window. Good actors could easily get caught in this, even trying to implement reasonable back-offs.
Starting in 0.5, windows are fixed, and staggered throughout a given period based on the key value, so the third request, above would not be rate limited (it’s possible neither would the second one).
Warning
That means that given a rate of X/u, you may see up to 2 * X
requests in a short period of time. Make sure to set X
accordingly if this is an issue.
This change still limits bad actors while being far kinder to good actors.
Staggering windows¶
To avoid a situation where all limits expire at the top of the hour, windows are automatically staggered throughout their period based on the key value. So if, for example, two IP addresses are hitting hourly limits, instead of both of those limits expiring at 06:00:00, one might expire at 06:13:41 (and subsequently at 07:13:41, etc) and the other might expire at 06:48:13 (and 07:48:13, etc).
Using multiple decorators¶
A single @ratelimit decorator used to be able to ratelimit against
multiple keys, e.g., before 0.5:
@ratelimit(ip=True, field='username', keys=mykeysfunc)
def someview(request):
# ...
To simplify both the internals and the question of what limits apply, each decorator now tracks exactly one rate, but decorators can be more reliably stacked (c.f. some examples in the section above).
The pre-0.5 example above would need to become four decorators:
@ratelimit(key='ip')
@ratelimit(key='post:username')
@ratelimit(key='get:username')
@ratelimit(key=mykeysfunc)
def someview(request):
# ...
As documented above, however, this allows powerful new uses, like burst limits and distinct GET/POST limits.