forezp / distributed-limit

一个分布式限流的解决方案!
Apache License 2.0
105 stars 54 forks source link

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

Open CaiusBallad opened 3 years ago

CaiusBallad commented 3 years ago
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的过期时间,这本质上还是计数器算法吧?