From 894ca88a540dadece48b3be2f34af09473e19046 Mon Sep 17 00:00:00 2001 From: Maruf Alom Date: Mon, 17 Aug 2020 11:25:13 +0600 Subject: [PATCH] fix: cleaned up config file --- composer.json | 2 +- config/dejavu.php | 32 ++++++------------------------ src/Client/RedisInstance.php | 17 +++++++++++++--- src/Client/RedisSentinelClient.php | 30 +++++++++++++++++++++++++++- src/DejaVuServiceProvider.php | 1 + 5 files changed, 51 insertions(+), 31 deletions(-) diff --git a/composer.json b/composer.json index 62d98ca..5bc88d4 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/config/dejavu.php b/config/dejavu.php index c9ebae8..2a0542c 100644 --- a/config/dejavu.php +++ b/config/dejavu.php @@ -1,33 +1,13 @@ $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'), ]; \ No newline at end of file diff --git a/src/Client/RedisInstance.php b/src/Client/RedisInstance.php index 99dba01..e02d63c 100644 --- a/src/Client/RedisInstance.php +++ b/src/Client/RedisInstance.php @@ -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'); @@ -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 * diff --git a/src/Client/RedisSentinelClient.php b/src/Client/RedisSentinelClient.php index 46e72c3..66cde24 100644 --- a/src/Client/RedisSentinelClient.php +++ b/src/Client/RedisSentinelClient.php @@ -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?'); @@ -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))); + } } \ No newline at end of file diff --git a/src/DejaVuServiceProvider.php b/src/DejaVuServiceProvider.php index 68d84b0..fe43963 100644 --- a/src/DejaVuServiceProvider.php +++ b/src/DejaVuServiceProvider.php @@ -24,6 +24,7 @@ public function register(): void __DIR__.'/../config/dejavu.php','dejavu' ); } + /** * Publishing package related files *