Code Monkey home page Code Monkey logo

Comments (3)

jsocol avatar jsocol commented on May 14, 2024

Ratelimits are calculated on staggered, fixed windows, i.e., for a given set of group/path/methods/key-value/period, ratelimit will calculate some value that will be the same every time it's calculated for period seconds (it uses the value to stagger them so, e.g. not all hour ratelimits expire at the top of the hour).

So, imagine the limit is 1/min, the second attempt (&) will be ratelimited, but the 3rd won't be, even though it's within 1 minute of the first:

period:       |             |
attempts:            ^  &    ^

The reason for this is that if you reset the period every time (what ratelimit used to do) then you can end up blocking even good actors basically forever (e.g. they try every 59 seconds, continually pushing the limit out and never being allowed).

So what's probably happening is that every once in a while, you cross the boundary during that loop. The odds are bad—and your flakiness rate is low—but it's entirely possible.

You could probably simplify this by mocking ratelimit.utils.is_ratelimited to always return True during this test, instead of trying to actually trip the limit. Alternatively, set the number of attempts to twice the limit, to make sure you hit it even if you cross the boundary (but you'll have to drop some of the assertions within the loop).

from django-ratelimit.

samuelcolvin avatar samuelcolvin commented on May 14, 2024

Thanks a lot, makes complete sense, this had occurred to me but I assumed the limit was reset every time.

Thanks for your help.

from django-ratelimit.

samuelcolvin avatar samuelcolvin commented on May 14, 2024

For anyone else having trouble with ratelimt and CI, my solution is:

    wrong_credentials = {'username': '[email protected]', 'password': 'wrong'}
    found_captcha = False
    for _ in range(7):
        response = self.client.post(self.login_url, wrong_credentials)
        self.assertEqual(response.status_code, 200)
        self.assertFalse(response.context['login_form'].is_valid())
        if 'captcha' in response.content:
            found_captcha = True
            break
    self.assertTrue(found_captcha, 'captcha not found on login page after 7 failed login attempts')

from django-ratelimit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.