Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Success count now gets cleared when using Redis #74

Merged
merged 3 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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