Skip to content

Commit 7175e9e

Browse files
committed
Merge branch 'master' into 3.0-merge
# Conflicts: # .github/workflows/test.yml # composer.json # phpunit.xml # src/config-apollo/src/Client.php # src/http-server/src/Request.php # src/redis/src/RedisConnection.php # src/utils/composer.json
2 parents 75575be + dea5124 commit 7175e9e

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

src/RedisConnection.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Hyperf\Contract\StdoutLoggerInterface;
1717
use Hyperf\Pool\Connection as BaseConnection;
1818
use Hyperf\Pool\Exception\ConnectionException;
19+
use Hyperf\Redis\Exception\InvalidRedisConnectionException;
1920
use Psr\Container\ContainerInterface;
2021

2122
/**
@@ -193,25 +194,37 @@ protected function createRedisSentinel(): \Redis
193194
$retryInterval = $this->config['retry_interval'] ?? 0;
194195
$readTimeout = $this->config['sentinel']['read_timeout'] ?? 0;
195196
$masterName = $this->config['sentinel']['master_name'] ?? '';
197+
shuffle($nodes);
196198

197-
$host = '';
198-
$port = 0;
199+
$host = null;
200+
$port = null;
199201
foreach ($nodes as $node) {
200-
[$sentinelHost, $sentinelPort] = explode(':', $node);
201-
$sentinel = new \RedisSentinel(
202-
$sentinelHost,
203-
intval($sentinelPort),
204-
$timeout,
205-
$persistent,
206-
$retryInterval,
207-
$readTimeout
208-
);
209-
$masterInfo = $sentinel->getMasterAddrByName($masterName);
210-
if (is_array($masterInfo) && count($masterInfo) >= 2) {
211-
[$host, $port] = $masterInfo;
212-
break;
202+
try {
203+
[$sentinelHost, $sentinelPort] = explode(':', $node);
204+
$sentinel = new \RedisSentinel(
205+
$sentinelHost,
206+
intval($sentinelPort),
207+
$timeout,
208+
$persistent,
209+
$retryInterval,
210+
$readTimeout
211+
);
212+
$masterInfo = $sentinel->getMasterAddrByName($masterName);
213+
if (is_array($masterInfo) && count($masterInfo) >= 2) {
214+
[$host, $port] = $masterInfo;
215+
break;
216+
}
217+
} catch (\Throwable $exception) {
218+
$logger = $this->container->get(StdoutLoggerInterface::class);
219+
$logger->warning('Redis sentinel connection failed, caused by ' . $exception->getMessage());
220+
continue;
213221
}
214222
}
223+
224+
if ($host === null && $port === null) {
225+
throw new InvalidRedisConnectionException('Connect sentinel redis server failed.');
226+
}
227+
215228
$redis = $this->createRedis($host, $port, $timeout);
216229
} catch (\Throwable $e) {
217230
throw new ConnectionException('Connection reconnect failed ' . $e->getMessage());

tests/RedisTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ public function testNewRedisCluster()
172172
}
173173
}
174174

175+
public function testShuffleNodes()
176+
{
177+
$nodes = ['127.0.0.1:6379', '127.0.0.1:6378', '127.0.0.1:6377'];
178+
179+
shuffle($nodes);
180+
181+
$this->assertIsArray($nodes);
182+
$this->assertSame(3, count($nodes));
183+
}
184+
175185
private function getRedis()
176186
{
177187
$container = $this->getContainer();

0 commit comments

Comments
 (0)