Skip to content

Instantly share code, notes, and snippets.

@Suor
Created March 16, 2012 11:22
Show Gist options
  • Save Suor/2049656 to your computer and use it in GitHub Desktop.
Save Suor/2049656 to your computer and use it in GitHub Desktop.
Redis rate limiting
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