Skip to content

How can I set random limit? #404

Answered by alisaifee
anilmwr asked this question in Q&A
Discussion options

You must be logged in to vote

There's nothing built in that would support this, but you could use your own after_request hook to populate the headers yourself. Something like this might work:

import random

from flask import Flask

from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

app = Flask(__name__)
limiter = Limiter(
    get_remote_address, app=app, default_limits=["20/day"]
)


@app.after_request
def custom_headers(response):
    if limiter.current_limit:
        response.headers[
            "X-RateLimit-Reset"
        ] = limiter.current_limit.reset_at + random.randint(1, 10)
        response.headers["X-RateLimit-Remaining"] = max(
            0, limiter.current_limit.remaining -

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by alisaifee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants