maybe it's better to check return value of INCR command in the rate limiter example
ZhangChaoWN opened this issue · comments
Zhang Chao commented
current example
FUNCTION LIMIT_API_CALL(ip)
ts = CURRENT_UNIX_TIME()
keyname = ip+":"+ts
current = GET(keyname)
IF current != NULL AND current > 10 THEN
ERROR "too many requests per second"
ELSE
MULTI
INCR(keyname,1)
EXPIRE(keyname,10)
EXEC
PERFORM_API_CALL()
END
after add return value checking:
FUNCTION LIMIT_API_CALL(ip)
ts = CURRENT_UNIX_TIME()
keyname = ip+":"+ts
current = GET(keyname)
IF current != NULL AND current > 10 THEN
ERROR "too many requests per second"
ELSE
MULTI
current = INCR(keyname,1)
EXPIRE(keyname,10)
EXEC
IF (current <= 10) THEN
PERFORM_API_CALL()
END
END
there are race condition for the example, maybe add return value checking of INCR command would be more precise
Zhang Chao commented
this issue should be open in repo redis-doc instead of redis-io
Zhang Chao commented
this issue could be fixed by redis/redis-doc#1219
Zhang Chao commented
close because there are pull request already