Skip to content

Commit 33a9c96

Browse files
committed
Merge branch 'master' into 1.0-merge
2 parents 8152496 + d428052 commit 33a9c96

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
lines changed

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
},
1313
"require": {
1414
"php": ">=7.2",
15-
"psr/container": "^1.0",
16-
"hyperf/contract": "~1.0.0",
17-
"hyperf/pool": "~1.0.0",
18-
"hyperf/utils": "~1.0.0"
15+
"hyperf/contract": "~1.1.0",
16+
"hyperf/pool": "~1.1.0",
17+
"hyperf/utils": "~1.1.0",
18+
"psr/container": "^1.0"
1919
},
2020
"require-dev": {
21-
"hyperf/di": "~1.0.0",
21+
"hyperf/di": "~1.1.0",
2222
"malukenho/docheader": "^0.1.6",
2323
"mockery/mockery": "^1.0",
2424
"phpunit/phpunit": "^7.0.0",
@@ -42,7 +42,7 @@
4242
},
4343
"extra": {
4444
"branch-alias": {
45-
"dev-master": "1.0-dev"
45+
"dev-master": "1.1-dev"
4646
},
4747
"hyperf": {
4848
"config": "Hyperf\\Redis\\ConfigProvider"

src/Frequency.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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://doc.hyperf.io
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\Redis;
14+
15+
use Hyperf\Pool\Frequency as DefaultFrequency;
16+
17+
class Frequency extends DefaultFrequency
18+
{
19+
}

src/Pool/RedisPool.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Hyperf\Contract\ConfigInterface;
1616
use Hyperf\Contract\ConnectionInterface;
1717
use Hyperf\Pool\Pool;
18+
use Hyperf\Redis\Frequency;
1819
use Hyperf\Redis\RedisConnection;
1920
use Hyperf\Utils\Arr;
2021
use Psr\Container\ContainerInterface;
@@ -43,6 +44,8 @@ public function __construct(ContainerInterface $container, string $name)
4344
$this->config = $config->get($key);
4445
$options = Arr::get($this->config, 'pool', []);
4546

47+
$this->frequency = make(Frequency::class);
48+
4649
parent::__construct($container, $options);
4750
}
4851

src/RedisConnection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public function reconnect(): bool
106106

107107
public function close(): bool
108108
{
109+
unset($this->connection);
110+
109111
return true;
110112
}
111113

tests/RedisConnectionTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
use Hyperf\Config\Config;
1616
use Hyperf\Contract\ConfigInterface;
1717
use Hyperf\Di\Container;
18+
use Hyperf\Pool\Channel;
19+
use Hyperf\Pool\LowFrequencyInterface;
20+
use Hyperf\Pool\PoolOption;
21+
use Hyperf\Redis\Frequency;
22+
use Hyperf\Utils\ApplicationContext;
1823
use HyperfTest\Redis\Stub\RedisPoolStub;
1924
use Mockery;
2025
use PHPUnit\Framework\TestCase;
@@ -70,10 +75,33 @@ public function testRedisConnectionReconnect()
7075
$this->assertSame(null, $connection->getDatabase());
7176
}
7277

78+
public function testRedisCloseInLowFrequency()
79+
{
80+
$pool = $this->getRedisPool();
81+
82+
$connection1 = $pool->get()->getConnection();
83+
$connection2 = $pool->get()->getConnection();
84+
$connection3 = $pool->get()->getConnection();
85+
86+
$this->assertSame(3, $pool->getCurrentConnections());
87+
88+
$connection1->release();
89+
$connection2->release();
90+
$connection3->release();
91+
92+
$this->assertSame(3, $pool->getCurrentConnections());
93+
94+
$connection = $pool->get()->getConnection();
95+
96+
$this->assertSame(1, $pool->getCurrentConnections());
97+
98+
$connection->release();
99+
}
100+
73101
private function getRedisPool()
74102
{
75103
$container = Mockery::mock(Container::class);
76-
$container->shouldReceive('get')->once()->with(ConfigInterface::class)->andReturn(new Config([
104+
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([
77105
'redis' => [
78106
'default' => [
79107
'host' => 'redis',
@@ -91,6 +119,18 @@ private function getRedisPool()
91119
],
92120
]));
93121

122+
$frequency = Mockery::mock(LowFrequencyInterface::class);
123+
$frequency->shouldReceive('isLowFrequency')->andReturn(true);
124+
$container->shouldReceive('make')->with(Frequency::class, Mockery::any())->andReturn($frequency);
125+
$container->shouldReceive('make')->with(PoolOption::class, Mockery::any())->andReturnUsing(function ($class, $args) {
126+
return new PoolOption(...array_values($args));
127+
});
128+
$container->shouldReceive('make')->with(Channel::class, Mockery::any())->andReturnUsing(function ($class, $args) {
129+
return new Channel($args['size']);
130+
});
131+
132+
ApplicationContext::setContainer($container);
133+
94134
return new RedisPoolStub($container, 'default');
95135
}
96136
}

0 commit comments

Comments
 (0)