django-ratelimit
django-ratelimit copied to clipboard
Configure via settings/groups
One benefit of the group= kwarg I identified in #48 is that it acts as a natural key to use to define at least default values for the decorator elsewhere, i.e.: settings. E.g.:
# settings.py
RATELIMIT_GROUPS = {
'mygroup': {
'key': 'ip',
'rate': '100/h',
'method': 'POST',
'block': True,
},
'some.mod.view': {
'key': 'user-or-ip',
'rate': 'some.mode.view_rate',
}
}
# some/mod.py
@ratelimit()
def view(request):
pass
@ratelimit(group='mygroup')
def someview(request)
pass
def someotherview(request):
if is_ratelimited(request, group='mygroup'):
# This gets much easier.
pass
The setting would override the defaults but could be overridden by the call site, so the precedence is:
- call site (either
@ratelimitdecorator oris_ratelimitedhelper) RATELIMIT_GROUPSsetting- ratelimit's defaults.
It makes it much, much easier to do a few things:
- Update a shared ratelimit everywhere
- Confidently use a shared ratelimit in multiple contexts
- Temporarily disable ratelimits with fewer touch points
The way counters are constructed, overriding any of the values in the decorator would cause the group= to count separately—but that's true now, so it's probably something that just needs better documentation.
Nice idea.