Skip to content

Commit

Permalink
DbConnection::check时,验证闲置时间是否超过最大值 (swoft-cloud/swoft-component#189)
Browse files Browse the repository at this point in the history
* 验证空闲时间是否超过最大值

* 验证空闲时间是否超过最大值

* 验证空闲时间是否超过最大值

* 完善对应单测

* 抽离验证空闲时间是否超过最大值的逻辑

* 代码格式化

* 抽离验证空闲时间是否超过最大值的逻辑

* Update AbstractDbConnection.php
  • Loading branch information
limingxinleo authored and huangzhhui committed Aug 30, 2018
1 parent 805ab2c commit ef53911
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/AbstractDbConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ protected function pushSqlToStack(string $sql)

RequestContext::setContextDataByKey($contextSqlKey, $stack);
}

/**
* Verify whether the idle time exceeds the maximum value.
*/
protected function isIdleTimeOut(): bool
{
$idleTime = time() - $this->getLastTime();
$maxIdleTime = $this->getPool()->getPoolConfig()->getMaxIdleTime();
return $idleTime > $maxIdleTime;
}
}
3 changes: 3 additions & 0 deletions src/Driver/Mysql/MysqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ public function reconnect()
*/
public function check(): bool
{
if ($this->isIdleTimeOut()) {
return false;
}
return $this->connection->connected;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Driver/Mysql/SyncMysqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public function reconnect()
*/
public function check(): bool
{
if ($this->isIdleTimeOut()) {
return false;
}

try {
$this->connection->getAttribute(\PDO::ATTR_SERVER_INFO);
} catch (\Throwable $e) {
Expand Down
34 changes: 34 additions & 0 deletions test/Cases/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SwoftTest\Db\Testing\Pool\DbPptPoolConfig;
use SwoftTest\Db\Testing\Pool\DbSlaveEnvPoolConfig;
use SwoftTest\Db\Testing\Pool\DbSlavePptConfig;
use SwoftTest\Db\Testing\Pool\OtherDbPool;

/**
* PoolTest
Expand Down Expand Up @@ -89,4 +90,37 @@ public function testDbSlaveEnv()
$this->assertEquals($pConfig->isUseProvider(), false);
$this->assertEquals($pConfig->getMaxWait(), 10);
}

public function testMaxIdleTime()
{
$pool = App::getPool('idle.master');
$connection = $pool->getConnection();
$this->assertTrue($connection->check());
$connection->release(true);
$connection2 = $pool->getConnection();
$this->assertTrue($connection2->check());
$connection2->release(true);
}

public function testMaxIdleTimeByCo()
{
go(function () {
$this->testMaxIdleTime();

$pool = App::getPool('idle.master');
$connection = $pool->getConnection();
$this->assertTrue($connection->check());
$connection->release(true);

\co::sleep(2);

$connection2 = $pool->getConnection();
$this->assertFalse($connection2->check());
$connection2->release(true);

$connection3 = $pool->getConnection();
$this->assertTrue($connection3->check());
$connection2->release(true);
});
}
}
33 changes: 33 additions & 0 deletions test/Testing/Pool/MaxIdleTimeDbConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://doc.swoft.org
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/
namespace SwoftTest\Db\Testing\Pool;

use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Value;
use Swoft\Db\Driver\Driver;
use Swoft\Db\Pool\Config\DbPoolProperties;

/**
* OtherDbConfig
*
* @Bean()
*/
class MaxIdleTimeDbConfig extends OtherDbConfig
{
/**
* @var string
*/
protected $name = 'idle.master';

/**
* @var int
*/
protected $maxIdleTime = 1;
}
28 changes: 28 additions & 0 deletions test/Testing/Pool/MaxIdleTimeDbPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://doc.swoft.org
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/
namespace SwoftTest\Db\Testing\Pool;

use Swoft\Bean\Annotation\Inject;
use Swoft\Bean\Annotation\Pool;
use Swoft\Db\Pool\DbPool;

/**
* OtherDbPool
*
* @Pool("idle.master")
*/
class MaxIdleTimeDbPool extends DbPool
{
/**
* @Inject()
* @var MaxIdleTimeDbConfig
*/
protected $poolConfig;
}

0 comments on commit ef53911

Please sign in to comment.