I have a Tornado web server. I wonder if there is anyway I can control the number of incoming requests? I want to only accept x number of requests from a single client in a given timeframe.
Set a cookie with the expiry as the timeframe you want to be and use this cookie to keep count of requests.
code sample:
lets say you want the timeframe to be of one day, so here is how you would set the cookie. Do this when user logged in (or after any action you want):
set_secure_cookie('requestscount', '0', expires_days=1)
and then check the count value before giving access to the resource:
user_requests = int(get_secure_cookie('requestscount'))
if user_requests < MAX_USER_REQUESTS:
user_requests += 1
set_secure_cookie('requestscount', str(user_requests), expires_days=1)
# serve the resource to user
...
Of course there are other ways. You could keep this count in a database instead of a cookie.