Skip to content

Commit ef53911

Browse files
limingxinleohuangzhhui
authored andcommitted
DbConnection::check时,验证闲置时间是否超过最大值 (swoft-cloud/swoft-component#189)
* 验证空闲时间是否超过最大值 * 验证空闲时间是否超过最大值 * 验证空闲时间是否超过最大值 * 完善对应单测 * 抽离验证空闲时间是否超过最大值的逻辑 * 代码格式化 * 抽离验证空闲时间是否超过最大值的逻辑 * Update AbstractDbConnection.php
1 parent 805ab2c commit ef53911

6 files changed

+112
-0
lines changed

src/AbstractDbConnection.php

+10
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,14 @@ protected function pushSqlToStack(string $sql)
9797

9898
RequestContext::setContextDataByKey($contextSqlKey, $stack);
9999
}
100+
101+
/**
102+
* Verify whether the idle time exceeds the maximum value.
103+
*/
104+
protected function isIdleTimeOut(): bool
105+
{
106+
$idleTime = time() - $this->getLastTime();
107+
$maxIdleTime = $this->getPool()->getPoolConfig()->getMaxIdleTime();
108+
return $idleTime > $maxIdleTime;
109+
}
100110
}

src/Driver/Mysql/MysqlConnection.php

+3
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ public function reconnect()
205205
*/
206206
public function check(): bool
207207
{
208+
if ($this->isIdleTimeOut()) {
209+
return false;
210+
}
208211
return $this->connection->connected;
209212
}
210213

src/Driver/Mysql/SyncMysqlConnection.php

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ public function reconnect()
124124
*/
125125
public function check(): bool
126126
{
127+
if ($this->isIdleTimeOut()) {
128+
return false;
129+
}
130+
127131
try {
128132
$this->connection->getAttribute(\PDO::ATTR_SERVER_INFO);
129133
} catch (\Throwable $e) {

test/Cases/PoolTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SwoftTest\Db\Testing\Pool\DbPptPoolConfig;
1515
use SwoftTest\Db\Testing\Pool\DbSlaveEnvPoolConfig;
1616
use SwoftTest\Db\Testing\Pool\DbSlavePptConfig;
17+
use SwoftTest\Db\Testing\Pool\OtherDbPool;
1718

1819
/**
1920
* PoolTest
@@ -89,4 +90,37 @@ public function testDbSlaveEnv()
8990
$this->assertEquals($pConfig->isUseProvider(), false);
9091
$this->assertEquals($pConfig->getMaxWait(), 10);
9192
}
93+
94+
public function testMaxIdleTime()
95+
{
96+
$pool = App::getPool('idle.master');
97+
$connection = $pool->getConnection();
98+
$this->assertTrue($connection->check());
99+
$connection->release(true);
100+
$connection2 = $pool->getConnection();
101+
$this->assertTrue($connection2->check());
102+
$connection2->release(true);
103+
}
104+
105+
public function testMaxIdleTimeByCo()
106+
{
107+
go(function () {
108+
$this->testMaxIdleTime();
109+
110+
$pool = App::getPool('idle.master');
111+
$connection = $pool->getConnection();
112+
$this->assertTrue($connection->check());
113+
$connection->release(true);
114+
115+
\co::sleep(2);
116+
117+
$connection2 = $pool->getConnection();
118+
$this->assertFalse($connection2->check());
119+
$connection2->release(true);
120+
121+
$connection3 = $pool->getConnection();
122+
$this->assertTrue($connection3->check());
123+
$connection2->release(true);
124+
});
125+
}
92126
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* This file is part of Swoft.
4+
*
5+
* @link https://swoft.org
6+
* @document https://doc.swoft.org
7+
* @contact group@swoft.org
8+
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
9+
*/
10+
namespace SwoftTest\Db\Testing\Pool;
11+
12+
use Swoft\Bean\Annotation\Bean;
13+
use Swoft\Bean\Annotation\Value;
14+
use Swoft\Db\Driver\Driver;
15+
use Swoft\Db\Pool\Config\DbPoolProperties;
16+
17+
/**
18+
* OtherDbConfig
19+
*
20+
* @Bean()
21+
*/
22+
class MaxIdleTimeDbConfig extends OtherDbConfig
23+
{
24+
/**
25+
* @var string
26+
*/
27+
protected $name = 'idle.master';
28+
29+
/**
30+
* @var int
31+
*/
32+
protected $maxIdleTime = 1;
33+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* This file is part of Swoft.
4+
*
5+
* @link https://swoft.org
6+
* @document https://doc.swoft.org
7+
* @contact group@swoft.org
8+
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
9+
*/
10+
namespace SwoftTest\Db\Testing\Pool;
11+
12+
use Swoft\Bean\Annotation\Inject;
13+
use Swoft\Bean\Annotation\Pool;
14+
use Swoft\Db\Pool\DbPool;
15+
16+
/**
17+
* OtherDbPool
18+
*
19+
* @Pool("idle.master")
20+
*/
21+
class MaxIdleTimeDbPool extends DbPool
22+
{
23+
/**
24+
* @Inject()
25+
* @var MaxIdleTimeDbConfig
26+
*/
27+
protected $poolConfig;
28+
}

0 commit comments

Comments
 (0)