Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Allow passing already configured redis client to the storage adapter #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public function __construct(array $options = array())
}

$this->options = array_merge(self::$defaultOptions, $options);
$this->redis = new \Redis();

if(isset($this->options['redis']) && $this->options['redis'] instanceof \Redis) {
$this->redis = $this->options['redis'];
}
}

/**
Expand Down Expand Up @@ -80,7 +83,13 @@ function (array $metric) {
*/
private function openConnection()
{
if($this->redis != null) {
return;
}

try {
$this->redis = new \Redis();

if ($this->options['persistent_connections']) {
@$this->redis->pconnect($this->options['host'], $this->options['port'], $this->options['timeout']);
} else {
Expand Down
26 changes: 26 additions & 0 deletions tests/Test/Prometheus/Storage/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,30 @@ public function itShouldThrowAnExceptionOnConnectionFailure()
$redis->flushRedis();
}

public function testReuseRedisClient()
{
$redisClient = $this->getMockBuilder(\Redis::class)->getMock();
$redisStorage = new Redis(['redis' => $redisClient]);

$this->assertAttributeEquals($redisClient, 'redis', $redisStorage);

$redisClient->expects($this->atLeastOnce())
->method('flushAll');

$redisClient->expects($this->never())
->method('connect');

$redisStorage->flushRedis();
}

public function testReuseRedisClientWithDefaultOptions()
{
$redisClient = $this->getMockBuilder(\Redis::class)->getMock();

Redis::setDefaultOptions(['redis' => $redisClient]);

$redisStorage = new Redis();

$this->assertAttributeEquals($redisClient, 'redis', $redisStorage);
}
}