Created
March 16, 2012 11:22
-
-
Save Suor/2049656 to your computer and use it in GitHub Desktop.
Redis rate limiting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FUNCTION LIMIT_API_CALL(ip) | |
ts = CURRENT_UNIX_TIME() | |
_, current, _ = MULTI | |
SETNX(ip, ts * 10) | |
INCR(ip, 1) | |
EXPIRE(ip, 1) | |
EXEC | |
IF current - ts * 10 > 10 THEN | |
ERROR "too many requests per second" | |
ELSE | |
PERFORM_API_CALL() | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment