redis / redis-io

Application running http://redis.io

Home Page:http://redis.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

maybe it's better to check return value of INCR command in the rate limiter example

ZhangChaoWN opened this issue · comments

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

this issue should be open in repo redis-doc instead of redis-io

this issue could be fixed by redis/redis-doc#1219

close because there are pull request already