django-ratelimit icon indicating copy to clipboard operation
django-ratelimit copied to clipboard

Configure via settings/groups

Open jsocol opened this issue 11 years ago • 1 comments

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:

  1. call site (either @ratelimit decorator or is_ratelimited helper)
  2. RATELIMIT_GROUPS setting
  3. 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.

jsocol avatar Oct 26 '14 20:10 jsocol

Nice idea.

jplehmann avatar Mar 22 '16 11:03 jplehmann