Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure via settings/groups #51

Open
jsocol opened this issue Oct 26, 2014 · 1 comment
Open

Configure via settings/groups #51

jsocol opened this issue Oct 26, 2014 · 1 comment

Comments

@jsocol
Copy link
Owner

jsocol commented Oct 26, 2014

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 jsocol added this to the 0.6 milestone Oct 26, 2014
@jsocol jsocol modified the milestones: 0.7, 0.8 Oct 15, 2015
@jplehmann
Copy link

Nice idea.

@jsocol jsocol modified the milestones: 1.2, 3.0 Feb 20, 2019
@jsocol jsocol modified the milestones: 3.0, 3.1 Jan 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants