Skip to content

Commit

Permalink
Fixed limiter prefxi bug
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Jul 17, 2019
1 parent 1775a8b commit 7b54d4c
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/limiter/src/Rate/RedisRateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,30 @@ public function getTicket(array $config): bool
$default = $config['default'];

$lua = <<<LUA
local now = tonumber(KEYS[1]);
local sKey = KEYS[2];
local nKey = KEYS[3];
local rate = tonumber(KEYS[4]);
local max = tonumber(KEYS[5]);
local default = tonumber(KEYS[6]);
local sKey = KEYS[1];
local nKey = KEYS[2];
local now = tonumber(ARGV[1]);
local rate = tonumber(ARGV[2]);
local max = tonumber(ARGV[3]);
local default = tonumber(ARGV[4]);
local sNum = redis.call('get', sKey);
sNum = tonumber(sNum);
if(sNum == nil)
if((not sNum) or sNum == nil)
then
sNum = 0
end
sNum = tonumber(sNum);
local nNum = redis.call('get', nKey);
nNum = tonumber(nNum);
if(nNum == nil)
if((not nNum) or nNum == nil)
then
nNum = now
sNum = default
end
nNum = tonumber(nNum);
local newPermits = 0;
if(now > nNum)
then
Expand All @@ -85,21 +87,20 @@ public function getTicket(array $config): bool
LUA;

$args = [
$now,
$sKey,
$nKey,
$now,
$rate,
$max,
$default,
];

$result = Redis::connection($this->pool)->eval($lua, $args, count($args));
$result = Redis::connection($this->pool)->eval($lua, $args, 2);
return (bool)$result;
}

/**
* @param string $name
*
* @param string $key
*
* @return string
Expand All @@ -111,7 +112,6 @@ private function getNextTimeKey(string $name, string $key): string

/**
* @param string $name
*
* @param string $key
*
* @return string
Expand Down

0 comments on commit 7b54d4c

Please sign in to comment.