- 🇨🇿Czech Republic siva01
I can confirm this issue. I have version Drupal Redis 1.7.0.
Drupal\redis\Flood\PhpRedis
/** * {@inheritdoc} */ public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) { if (!isset($identifier)) { $identifier = $this->requestStack->getCurrentRequest()->getClientIp(); } $key = $this->getPrefix() . ':flood:' . $name . ':' . $identifier; // Count the in the last $window seconds. $number = $this->client->zCount($key, $_SERVER['REQUEST_TIME'], 'inf'); return ($number < $threshold); }
$window isn't used in that function.
I think, that it should look as:
/** * {@inheritdoc} */ public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) { if (!isset($identifier)) { $identifier = $this->requestStack->getCurrentRequest()->getClientIp(); } $key = $this->getPrefix() . ':flood:' . $name . ':' . $identifier; // Count the in the last $window seconds. $number = $this->client->zCount($key, $_SERVER['REQUEST_TIME'] - $window, 'inf'); return ($number < $threshold); }
- First commit to issue fork.
- 🇵🇱Poland krystianbuczak
Hi @siva01,
even with passed $window argument the code seemed to not work properly. I tried to limit 10 requests within 1 minute window and Redis didn't expire my event properly. I have just added my proposal of the issue:
In
register()
methodBEFORE:
$this->client->zAdd($key, $_SERVER['REQUEST_TIME'] + $window, microtime(TRUE)); $this->client->expire($key, $_SERVER['REQUEST_TIME'] + $window);
AFTER
$this->client->zAdd($key, microtime(TRUE), microtime(TRUE)); $this->client->expire($key, $window);
In isAllowed() method
BEFORE
$number = $this->client->zCount($key, $_SERVER['REQUEST_TIME'], 'inf'); return ($number < $threshold);
AFTER
$number = $this->client->zCount($key, microtime(TRUE) - $window, 'inf'); return $number < $threshold;
+ redundant comments removal.
microtime()
is used for consistency.