forezp / distributed-limit

一个分布式限流的解决方案!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

看了下项目里的lua脚本,这是令牌桶算法吗?

CaiusBallad opened this issue · comments

commented
private String buildLuaScript() {
    StringBuilder lua = new StringBuilder();
    lua.append( " local key = KEYS[1]" );
    lua.append( "\nlocal limit = tonumber(ARGV[1])" );
    lua.append( "\nlocal curentLimit = tonumber(redis.call('get', key) or \"0\")" );
    lua.append( "\nif curentLimit + 1 > limit then" );
    lua.append( "\nreturn 0" );
    lua.append( "\nelse" );
    lua.append( "\n redis.call(\"INCRBY\", key, 1)" );
    lua.append( "\nredis.call(\"EXPIRE\", key, ARGV[2])" );
    lua.append( "\nreturn curentLimit + 1" );
    lua.append( "\nend" );
    return lua.toString();
}

设置了redis的过期时间,这本质上还是计数器算法吧?