Imagine you have a web app, which is dployed to multiple machines. No state is stored between requests in the actual webapp, although databases etc are of course allowed.
You need to write a function which rate limits access to a given URL within your app, for a specific user. The limits are: Max 5 hits per second, max 10 hits per 20 seconds, max 25 hits per 60 seconds, although this should be simple for the user of the function to change. If any of the limits is exceeded, access is denied.
The programming language doesn’t matter, how would you do this? I’m only interested in the function, which returns true of false, not the actual web app. You can use any third party database / library / whatever. No record of the rate limiting needs to be stored long term.
Here’s my attempt, which I’ll reveal once everyone else who wants to has had a shot:
$ md5 rate_limit.py
MD5 (rate_limit.py) = 0220011c8421748197c620bf333a315a
Bonus points for clarity, efficiency, etc.
Have fun
You need to write a function which rate limits access to a given URL within your app, for a specific user. The limits are: Max 5 hits per second, max 10 hits per 20 seconds, max 25 hits per 60 seconds, although this should be simple for the user of the function to change. If any of the limits is exceeded, access is denied.
The programming language doesn’t matter, how would you do this? I’m only interested in the function, which returns true of false, not the actual web app. You can use any third party database / library / whatever. No record of the rate limiting needs to be stored long term.
Here’s my attempt, which I’ll reveal once everyone else who wants to has had a shot:
$ md5 rate_limit.py
MD5 (rate_limit.py) = 0220011c8421748197c620bf333a315a
Bonus points for clarity, efficiency, etc.
Have fun