Skip to content

Commit

Permalink
Merge pull request #74 from ariasmn/redis-success-cleanup
Browse files Browse the repository at this point in the history
Success count now gets cleared when using Redis
  • Loading branch information
ackintosh committed May 12, 2021
2 parents aa4fa2b + bbef016 commit adb021d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/Ganesha/Storage/Adapter/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ public function setConfiguration(Configuration $configuration): void
*/
public function load(string $service): int
{
$expires = microtime(true) - $this->configuration->timeWindow();

if ($this->redis->zRemRangeByScore($service, '-inf', $expires) === false) {
throw new StorageException('Failed to remove expired elements. service: ' . $service);
}
$this->removeExpiredElements($service);

$r = $this->redis->zCard($service);

Expand Down Expand Up @@ -105,6 +101,9 @@ public function save(string $service, int $count): void
public function increment(string $service): void
{
$t = microtime(true);

$this->removeExpiredElements($service);

$this->redis->zAdd($service, $t, $t);
}

Expand Down Expand Up @@ -178,4 +177,18 @@ public function reset(): void
{
// TODO: Implement reset() method.
}

/**
* @param string $service
*
* @throws StorageException
*/
private function removeExpiredElements(string $service): void
{
$expires = microtime(true) - $this->configuration->timeWindow();

if ($this->redis->zRemRangeByScore($service, '-inf', $expires) === false) {
throw new StorageException('Failed to remove expired elements. service: ' . $service);
}
}
}
16 changes: 15 additions & 1 deletion tests/Ackintosh/Ganesha/Storage/Adapter/AbstractRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public function incrementAndLoad()
$this->assertSame(2, $result);
}

/**
* @test
* @expectedException \Ackintosh\Ganesha\Exception\StorageException
* @expectedExceptionMessageRegExp /\AFailed to remove expired elements/
*/
public function incrementThrowsExceptionWhenFailedToRunzRemRangeByScore()
{
$mock = $this->getMockBuilder(\Redis::class)->getMock();
$mock->method('zRemRangeByScore')
->willReturn(false);

$this->createAdapterWithMock($mock)->increment($this->service);
}

/**
* @test
* @expectedException \Ackintosh\Ganesha\Exception\StorageException
Expand Down Expand Up @@ -156,7 +170,7 @@ public function loadThrowsExceptionWhenFailedToRunzCard()
public function loadThrowsException()
{
$mock = $this->getMockBuilder(\Redis::class)->getMock();
$mock->method('zRemRangeByScore')
$mock->method('zCard')
->willThrowException(new \RedisException('exception test'));

$this->createAdapterWithMock($mock)->load($this->service);
Expand Down
10 changes: 9 additions & 1 deletion tests/Ackintosh/Ganesha/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ public function getLastFailureTimeWithSlidingTimeWindow()
getenv('GANESHA_EXAMPLE_REDIS') ? getenv('GANESHA_EXAMPLE_REDIS') : 'localhost'
);
$r->flushAll();
$storage = new Storage(new Redis(($r)), new Ganesha\Storage\StorageKeys(), null);

$redisAdapter = new Redis($r);
$context = new Ganesha\Context(
Ganesha\Strategy\Rate::class,
$redisAdapter,
new Configuration(['timeWindow' => 3])
);
$redisAdapter->setContext($context);
$storage = new Storage($redisAdapter, new Ganesha\Storage\StorageKeys(), null);

$service = 'test';
$storage->incrementFailureCount($service);
Expand Down

0 comments on commit adb021d

Please sign in to comment.