• 
      

    Add generic support for rate limiting.

    Review Request #14631 — Created Oct. 9, 2025 and submitted

    Information

    Djblets
    release-5.x

    Reviewers

    Djblets has long had rate limiting support for user sessions, allowing
    separate rate limits for anonymous users and authenticated users for
    normal sessions and API. Outside of this, though, we had no mechanism
    for rate limiting other operations.

    This change introduces that generic rate limiting support via a new
    djblets.protect.ratelimit module. This is a modernized port of the old
    rate limit code, which can be used with arbitrary keys and validity
    windows to provide rate limit functionality for anything.

    The main function is check_rate_limit, which takes in a parsed or
    string rate limit, a partial cache key, and a flag indicating whether to
    increment the count toward the rate limit in cache (defaults to True).

    The existing auth rate limiting code has been reworked as a wrapper
    around this, focusing on the settings and checks for rate limiting
    user sessions.

    Unit tests pass.

    Summary ID
    Add generic support for rate limiting.
    Djblets has long had rate limiting support for user sessions, allowing separate rate limits for anonymous users and authenticated users for normal sessions and API. Outside of this, though, we had no mechanism for rate limiting other operations. This change introduces that generic rate limiting support via a new `djblets.protect.ratelimit` module. This is a modernized port of the old rate limit code, which can be used with arbitrary keys and validity windows to provide rate limit functionality for anything. The main function is `check_rate_limit`, which takes in a parsed or string rate limit, a partial cache key, and a flag indicating whether to increment the count toward the rate limit in cache (defaults to `True`). The existing auth rate limiting code has been reworked as a wrapper around this, focusing on the settings and checks for rate limiting user sessions.
    7f8c2b3f4589007a21864b0e5935cb677bdf2b28
    Description From Last Updated

    djblets.http.requests doesn't currently exist. Did you forget to add this file?

    daviddavid

    Can we use .format() instead of %-formatting here?

    daviddavid

    This type is wrong

    daviddavid

    Can we pass the timeout as a kwarg here?

    daviddavid

    too many blank lines (2) Column: 9 Error code: E303

    reviewbotreviewbot

    Can Any be imported inside of TYPE_CHECKING?

    daviddavid

    Can you add ValueError and ImproperlyConfigured to a Raises section in the docs here.

    maubinmaubin

    Can you add ValueError to a Raises section in the docs here.

    maubinmaubin

    This should say list of str and mention the user_id_or_ip part of the return value.

    maubinmaubin

    This needs a Raises section for ValueError.

    maubinmaubin

    This should say rate_limit instead of default_limit.

    maubinmaubin

    We should maybe mention this in the arg description for rate_limit, that it can be set to None for no …

    maubinmaubin
    Checks run (1 failed, 1 succeeded)
    flake8 failed.
    JSHint passed.

    flake8

    david
    1. 
        
    2. djblets/auth/ratelimit.py (Diff revision 1)
       
       
      Show all issues

      djblets.http.requests doesn't currently exist. Did you forget to add this file?

      1. The change was sitting in draft form. Published as /r/14630/

    3. djblets/auth/ratelimit.py (Diff revision 1)
       
       
       
      Show all issues

      Can we use .format() instead of %-formatting here?

    4. djblets/protect/ratelimit.py (Diff revision 1)
       
       
       
      Show all issues

      This type is wrong

    5. djblets/protect/ratelimit.py (Diff revision 1)
       
       
      Show all issues

      Can we pass the timeout as a kwarg here?

      1. Yeah, copy/paste from old code.

    6. 
        
    chipx86
    david
    1. 
        
    2. djblets/auth/ratelimit.py (Diff revision 2)
       
       
      Show all issues

      Can Any be imported inside of TYPE_CHECKING?

    3. 
        
    maubin
    1. 
        
    2. djblets/auth/ratelimit.py (Diff revision 2)
       
       
      Show all issues

      Can you add ValueError and ImproperlyConfigured to a Raises section in the docs here.

    3. djblets/auth/ratelimit.py (Diff revision 2)
       
       
      Show all issues

      Can you add ValueError to a Raises section in the docs here.

    4. djblets/auth/ratelimit.py (Diff revision 2)
       
       
       
      Show all issues

      This should say list of str and mention the user_id_or_ip part of the return value.

    5. djblets/protect/ratelimit.py (Diff revision 2)
       
       
      Show all issues

      This needs a Raises section for ValueError.

    6. djblets/protect/ratelimit.py (Diff revision 2)
       
       
      Show all issues

      This should say rate_limit instead of default_limit.

    7. djblets/protect/ratelimit.py (Diff revision 2)
       
       
      Show all issues

      We should maybe mention this in the arg description for rate_limit, that it can be set to None for no rate limiting.

    8. 
        
    chipx86
    maubin
    1. Ship It!
    2. 
        
    chipx86
    Review request changed
    Status:
    Completed
    Change Summary:
    Pushed to release-5.x (5b8a3e6)