Skip to content

Commit 59dc1c9

Browse files
Support for SSL encrypted connection to Redis. (#5007)
Co-authored-by: 李铭昕 <[email protected]>
1 parent 58b1c91 commit 59dc1c9

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

publish/redis.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'timeout' => 0.0,
1919
'reserved' => null,
2020
'retry_interval' => 0,
21+
'read_timeout' => 0.0,
2122
'cluster' => [
2223
'enable' => (bool) env('REDIS_CLUSTER_ENABLE', false),
2324
'name' => null,

src/RedisConnection.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
3434
'auth' => null,
3535
'db' => 0,
3636
'timeout' => 0.0,
37+
'reserved' => null,
38+
'retry_interval' => 0,
39+
'read_timeout' => 0.0,
3740
'cluster' => [
3841
'enable' => false,
3942
'name' => null,
4043
'seeds' => [],
4144
'read_timeout' => 0.0,
4245
'persistent' => false,
46+
'context' => [],
4347
],
4448
'sentinel' => [
4549
'enable' => false,
@@ -49,6 +53,7 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
4953
'read_timeout' => 0,
5054
],
5155
'options' => [],
56+
'context' => [],
5257
];
5358

5459
/**
@@ -88,20 +93,21 @@ public function getActiveConnection()
8893
return $this;
8994
}
9095

96+
/**
97+
* @throws \RedisException
98+
* @throws ConnectionException
99+
*/
91100
public function reconnect(): bool
92101
{
93-
$host = $this->config['host'];
94-
$port = $this->config['port'];
95102
$auth = $this->config['auth'];
96103
$db = $this->config['db'];
97-
$timeout = $this->config['timeout'];
98104
$cluster = $this->config['cluster']['enable'] ?? false;
99105
$sentinel = $this->config['sentinel']['enable'] ?? false;
100106

101107
$redis = match (true) {
102108
$cluster => $this->createRedisCluster(),
103109
$sentinel => $this->createRedisSentinel(),
104-
default => $this->createRedis($host, $port, $timeout),
110+
default => $this->createRedis($this->config),
105111
};
106112

107113
$options = $this->config['options'] ?? [];
@@ -160,6 +166,9 @@ protected function createRedisCluster(): \RedisCluster
160166
if (isset($this->config['auth'])) {
161167
$parameters[] = $this->config['auth'];
162168
}
169+
if (! empty($this->config['cluster']['context'])) {
170+
$parameters[] = $this->config['cluster']['context'];
171+
}
163172

164173
$redis = new \RedisCluster(...$parameters);
165174
} catch (\Throwable $e) {
@@ -185,6 +194,9 @@ protected function retry($name, $arguments, \Throwable $exception)
185194
return $result;
186195
}
187196

197+
/**
198+
* @throws ConnectionException
199+
*/
188200
protected function createRedisSentinel(): \Redis
189201
{
190202
try {
@@ -227,7 +239,13 @@ protected function createRedisSentinel(): \Redis
227239
throw new InvalidRedisConnectionException('Connect sentinel redis server failed.');
228240
}
229241

230-
$redis = $this->createRedis($host, $port, $timeout);
242+
$redis = $this->createRedis([
243+
'host' => $host,
244+
'port' => $port,
245+
'timeout' => $timeout,
246+
'retry_interval' => $retryInterval,
247+
'read_timeout' => $readTimeout,
248+
]);
231249
} catch (\Throwable $e) {
232250
throw new ConnectionException('Connection reconnect failed ' . $e->getMessage());
233251
}
@@ -236,14 +254,26 @@ protected function createRedisSentinel(): \Redis
236254
}
237255

238256
/**
239-
* @param string $host
240-
* @param int $port
241-
* @param float $timeout
257+
* @throws ConnectionException
258+
* @throws \RedisException
242259
*/
243-
protected function createRedis($host, $port, $timeout): \Redis
260+
protected function createRedis(array $config): \Redis
244261
{
262+
$parameters = [
263+
$config['host'] ?? '',
264+
$config['port'] ?? 6379,
265+
$config['timeout'] ?? 0.0,
266+
$config['reserved'] ?? null,
267+
$config['retry_interval'] ?? 0,
268+
$config['read_timeout'] ?? 0.0,
269+
];
270+
271+
if (! empty($config['context'])) {
272+
$parameters[] = $config['context'];
273+
}
274+
245275
$redis = new \Redis();
246-
if (! $redis->connect((string) $host, (int) $port, $timeout)) {
276+
if (! $redis->connect(...$parameters)) {
247277
throw new ConnectionException('Connection reconnect failed.');
248278
}
249279
return $redis;

src/RedisProxy.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public function __construct(PoolFactory $factory, string $pool)
2525
$this->poolName = $pool;
2626
}
2727

28+
/**
29+
* WARN: Can't remove this function, because AOP need it.
30+
* @see https://github.com/hyperf/hyperf/issues/1239
31+
* @param string $name
32+
* @param array $arguments
33+
*/
2834
public function __call($name, $arguments)
2935
{
3036
return parent::__call($name, $arguments);

tests/RedisConnectionTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function testRedisConnectionConfig()
4646
'auth' => 'redis',
4747
'db' => 0,
4848
'timeout' => 0.0,
49+
'reserved' => null,
50+
'retry_interval' => 5,
51+
'read_timeout' => 3.0,
4952
'cluster' => [
5053
'enable' => false,
5154
'name' => null,
@@ -54,6 +57,9 @@ public function testRedisConnectionConfig()
5457
],
5558
'read_timeout' => 0.0,
5659
'persistent' => false,
60+
'context' => [
61+
'stream' => ['cafile' => 'foo-cafile', 'verify_peer' => true],
62+
],
5763
],
5864
'sentinel' => [
5965
'enable' => false,
@@ -63,6 +69,9 @@ public function testRedisConnectionConfig()
6369
'read_timeout' => 0,
6470
],
6571
'options' => [],
72+
'context' => [
73+
'stream' => ['cafile' => 'foo-cafile', 'verify_peer' => true],
74+
],
6675
'pool' => [
6776
'min_connections' => 1,
6877
'max_connections' => 30,
@@ -122,6 +131,12 @@ private function getRedisPool()
122131
'host' => 'redis',
123132
'auth' => 'redis',
124133
'port' => 16379,
134+
'read_timeout' => 3.0,
135+
'reserved' => null,
136+
'retry_interval' => 5,
137+
'context' => [
138+
'stream' => ['cafile' => 'foo-cafile', 'verify_peer' => true],
139+
],
125140
'pool' => [
126141
'min_connections' => 1,
127142
'max_connections' => 30,
@@ -136,6 +151,9 @@ private function getRedisPool()
136151
'seeds' => [
137152
'127.0.0.1:6379',
138153
],
154+
'context' => [
155+
'stream' => ['cafile' => 'foo-cafile', 'verify_peer' => true],
156+
],
139157
],
140158
'sentinel' => [
141159
'enable' => false,

tests/RedisTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testRedisConnect()
5454
$this->assertSame('timeout', $timeout->getName());
5555
$this->assertSame('retry_interval', $retryInterval->getName());
5656

57-
$this->assertTrue($redis->connect('127.0.0.1', 6379, 0.0));
57+
$this->assertTrue($redis->connect('127.0.0.1', 6379, 0.0, null, 0, 0));
5858
}
5959

6060
public function testRedisSelect()

0 commit comments

Comments
 (0)