Skip to content

Commit ec4931d

Browse files
committed
Merge branch 'master' into 3.1
# Conflicts: # src/redis/tests/RedisTest.php
2 parents ab75f64 + df1c2a1 commit ec4931d

File tree

3 files changed

+66
-26
lines changed

3 files changed

+66
-26
lines changed

src/RedisConnection.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Redis;
2424
use RedisCluster;
2525
use RedisException;
26-
use RedisSentinel;
2726
use Throwable;
2827

2928
/**
@@ -231,36 +230,28 @@ protected function createRedisSentinel(): Redis
231230
$readTimeout = $this->config['sentinel']['read_timeout'] ?? 0;
232231
$masterName = $this->config['sentinel']['master_name'] ?? '';
233232
$auth = $this->config['sentinel']['auth'] ?? null;
234-
// fixes bug for phpredis
235-
// https://github.com/phpredis/phpredis/issues/2098
236-
$extendConfig = [];
237-
if (! empty($auth)) {
238-
$extendConfig[] = $auth;
239-
}
240233

241234
shuffle($nodes);
242235

243236
$host = null;
244237
$port = null;
245238
foreach ($nodes as $node) {
246239
try {
247-
$nodeUrlArray = parse_url($node);
248-
$sentinelHost = $nodeUrlArray['host'] ?? null;
249-
$sentinelPort = $nodeUrlArray['port'] ?? null;
250-
if (! $sentinelHost || ! $sentinelPort) {
240+
$resolved = parse_url($node);
241+
if (! isset($resolved['host'], $resolved['port'])) {
251242
$this->log(sprintf('The redis sentinel node [%s] is invalid.', $node), LogLevel::ERROR);
252243
continue;
253244
}
254-
255-
$sentinel = new RedisSentinel(
256-
$sentinelHost,
257-
intval($sentinelPort),
258-
$timeout,
259-
$persistent,
260-
$retryInterval,
261-
$readTimeout,
262-
...$extendConfig
263-
);
245+
$options = [
246+
'host' => $resolved['host'],
247+
'port' => (int) $resolved['port'],
248+
'connectTimeout' => $timeout,
249+
'persistent' => $persistent,
250+
'retryInterval' => $retryInterval,
251+
'readTimeout' => $readTimeout,
252+
...($auth ? ['auth' => $auth] : []),
253+
];
254+
$sentinel = $this->container->get(RedisSentinelFactory::class)->create($options);
264255
$masterInfo = $sentinel->getMasterAddrByName($masterName);
265256
if (is_array($masterInfo) && count($masterInfo) >= 2) {
266257
[$host, $port] = $masterInfo;

src/RedisSentinelFactory.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Redis;
13+
14+
use RedisSentinel;
15+
16+
class RedisSentinelFactory
17+
{
18+
protected bool $isOlderThan6 = false;
19+
20+
public function __construct()
21+
{
22+
$this->isOlderThan6 = version_compare(phpversion('redis'), '6.0.0', '<');
23+
}
24+
25+
public function create(array $options = []): RedisSentinel
26+
{
27+
if ($this->isOlderThan6) {
28+
return new RedisSentinel(
29+
$options['host'],
30+
(int) $options['port'],
31+
(float) $options['connectTimeout'],
32+
$options['persistent'],
33+
(int) $options['retryInterval'],
34+
(float) $options['readTimeout'],
35+
...(isset($options['auth']) ? [$options['auth']] : []),
36+
);
37+
}
38+
39+
// https://github.com/phpredis/phpredis/blob/develop/sentinel.md#examples-for-version-60-or-later
40+
return new RedisSentinel($options); /* @phpstan-ignore-line */
41+
}
42+
}

tests/RedisTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
*/
5252
class RedisTest extends TestCase
5353
{
54+
protected bool $isOlderThan6 = false;
55+
56+
protected function setUp(): void
57+
{
58+
$this->isOlderThan6 = version_compare(phpversion('redis'), '6.0.0', '<');
59+
}
60+
5461
protected function tearDown(): void
5562
{
5663
Mockery::close();
@@ -66,10 +73,10 @@ public function testRedisConnect()
6673
$this->assertSame('host', $host->getName());
6774
$this->assertSame('port', $port->getName());
6875
$this->assertSame('timeout', $timeout->getName());
69-
if (version_compare(phpversion('redis'), '6.0', '>=')) {
70-
$this->assertSame('persistent_id', $retryInterval->getName());
71-
} else {
76+
if ($this->isOlderThan6) {
7277
$this->assertSame('retry_interval', $retryInterval->getName());
78+
} else {
79+
$this->assertSame('persistent_id', $retryInterval->getName());
7380
}
7481

7582
$this->assertTrue($redis->connect('127.0.0.1', 6379, 0.0, null, 0, 0));
@@ -177,7 +184,7 @@ public function testRedisClusterConstructor()
177184
if ($parameter->getName() === 'seeds') {
178185
$this->assertSame('array', $parameter->getType()->getName());
179186
} else {
180-
if (version_compare(phpversion('redis'), '6.0', '>=')) {
187+
if (! $this->isOlderThan6) {
181188
if (is_array($type)) {
182189
foreach ($parameter->getType()->getTypes() as $namedType) {
183190
$this->assertTrue(in_array($namedType->getName(), $type));
@@ -224,7 +231,7 @@ public function testRedisSentinelParams()
224231
$method = $rel->getMethod('__construct');
225232
$count = count($method->getParameters());
226233

227-
if (version_compare(phpversion('redis'), '6.0', '>=')) {
234+
if (! $this->isOlderThan6) {
228235
$this->assertSame(1, $count);
229236
$this->assertSame('options', $method->getParameters()[0]->getName());
230237
} else {

0 commit comments

Comments
 (0)