Skip to content

Commit

Permalink
fix: cleaned up config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Maruf Alom committed Aug 17, 2020
1 parent 99d8bd8 commit 894ca88
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"minimum-stability": "dev",
"require": {
"php": "^7.2.5",
"illuminate/support": "^v7.3.0",
"illuminate/support": "^v7.3.0"
},
"require-dev": {
"orchestra/testbench": "^5.3.0"
Expand Down
32 changes: 6 additions & 26 deletions config/dejavu.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
<?php

/**
* Configuration for Laravel DejaVu package
* currently it is only PHP Redis by default.
*/

/**
* @param string $key
* @return array
*/
function getRedisServers(string $key): array {
return array_map('trim', explode(',', env($key)));
}

$sentinelsIp = getRedisServers('REDIS_SENTINELS_IPS');
$sentinelsPort = getRedisServers('REDIS_SENTINELS_PORTS');
$sentinelsTimeout = getRedisServers('REDIS_SENTINELS_TIMEOUTS');

$sentinels = [];

foreach ($sentinelsIp as $key => $ip) {
$sentinel['host'] = $ip;
$sentinel['port'] = isset($sentinelsPort[$key]) ? (int)$sentinelsPort[$key] : 26379;
$sentinel['timeout'] = isset($sentinelsTimeout[$key]) ? (float) $sentinelsTimeout[$key] : 1.0;

$sentinels[] = $sentinel;
}


return [
'redis_auth' => env('REDIS_PASS', ''),
'sentinels' => $sentinels,
'redis_auth' => env('REDIS_PASS', null),
'persistant_id' => env('PERSISTENT_ID', 'dejavu'),
'sentinels_ips' => env('REDIS_SENTINELS_IPS', '172.16.0.11'),
'sentinels_ports' => env('REDIS_SENTINELS_PORTS', '26379'),
'sentinels_timeouts' => env('REDIS_SENTINELS_TIMEOUTS', '26379'),
];
17 changes: 14 additions & 3 deletions src/Client/RedisInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ public function __construct(array $redis)
public function connect() : \Redis
{
$redis = new \Redis();
$persistentId = self::PERSISTENT_ID . '_' . $this->getIp();

$redis->pconnect(
$this->getIp(),
$this->getPort(),
self::TIMEOUT,
$persistentId
$this->getPersistantId()
);
if (getenv('REDIS_PASS') !== null) {
$redis->auth(getenv('REDIS_PASS'));
}

$redis->auth(getenv('REDIS_PASS'));
$redis->setOption(\Redis::OPT_READ_TIMEOUT, 60);
$redis->config('SET', 'timeout', '10');

Expand Down Expand Up @@ -85,6 +86,16 @@ public function random(array $redis): array
return $redis[array_rand($redis)];
}

/**
* Get persistant id
*
* @return string
*/
protected function getPersistantId() : string
{
return config('dejavu.persistant_id') . '_' . $this->getIp();
}

/**
* Get IP of instance
*
Expand Down
30 changes: 29 additions & 1 deletion src/Client/RedisSentinelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function getInstance(): ?\RedisSentinel
throw new RedisException('phpredis not found. Please install phpredis');
}

$sentinels = config('dejavu.sentinels');
$sentinels = self::getSentinels();

if ($sentinels === null) {
throw new RedisException('No sentinels found. Did you publish the config file?');
Expand Down Expand Up @@ -69,4 +69,32 @@ public static function getInstance(): ?\RedisSentinel

throw new RedisException('None of redis sentinels are alive');
}

protected static function getSentinels() : array
{
$sentinelsIp = self::getRedisServers('dejavu.sentinels_ips');
$sentinelsPort = self::getRedisServers('dejavu.sentinels_ports');
$sentinelsTimeout = self::getRedisServers('dejavu.sentinels_timeouts');

$sentinels = [];

foreach ($sentinelsIp as $key => $ip) {
$sentinel['host'] = $ip;
$sentinel['port'] = isset($sentinelsPort[$key]) ? (int)$sentinelsPort[$key] : 26379;
$sentinel['timeout'] = isset($sentinelsTimeout[$key]) ? (float) $sentinelsTimeout[$key] : 1.0;

$sentinels[] = $sentinel;
}

return $sentinels;
}

/**
* @param string $key
* @return array
*/
protected static function getRedisServers(string $key)
{
return array_map('trim', explode(',', config($key)));
}
}
1 change: 1 addition & 0 deletions src/DejaVuServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function register(): void
__DIR__.'/../config/dejavu.php','dejavu'
);
}

/**
* Publishing package related files
*
Expand Down

0 comments on commit 894ca88

Please sign in to comment.