Skip to content

Commit

Permalink
增加连接池释放机制
Browse files Browse the repository at this point in the history
  • Loading branch information
anoxia committed Feb 16, 2019
1 parent 55f488d commit b1718a6
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/LayerInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<?php
namespace Bee\Db;

/**
* 数据连接对象接口
*
* @package Bee\Db
*/
interface LayerInterface
{
/**
* 释放数据连接
*
* @return mixed
*/
public function clean();
}
9 changes: 9 additions & 0 deletions src/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,13 @@ public function delete(string $sql, array $params = [], float $timeout = 0)
{
return $this->master($sql, $params, $timeout);
}

/**
* 释放数据连接
*/
public function clean()
{
$this->masterPool->clean();
$this->slavePool->clean();
}
}
12 changes: 11 additions & 1 deletion src/MySQL/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function get() : Item
$item = $this->pool->pop($this->timeout);

if ($item === false) {
throw new Exception('Get Item instance timeout, all connection is used!');
throw new Exception('Get mysql connection instance timeout, pool: ' . $this->getLength());
}

return $item;
Expand All @@ -64,4 +64,14 @@ public function getLength() : int
{
return $this->pool->length();
}

/**
* 清空数据库连接池(释放数据连接)
*/
public function clean()
{
while ($item = $this->pool->pop($this->timeout)) {
$item->close();
}
}
}
12 changes: 11 additions & 1 deletion src/PoolInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Bee\Db;


/**
* Interface PoolInterface
*
Expand All @@ -10,13 +9,24 @@
interface PoolInterface
{
/**
* 放回数据连接实例
*
* @param \Bee\Db\MySQL\Item|\Bee\Db\Redis\Item $item
* @return bool
*/
public function put($item);

/**
* 获取数据连接实例
*
* @return \Bee\Db\MySQL\Item|\Swoole\Coroutine\Redis
*/
public function get();

/**
* 清空数据库连接池(释放数据连接)
*
* @return mixed
*/
public function clean();
}
9 changes: 9 additions & 0 deletions src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,13 @@ public function putSlaveConnect($item)
{
$this->slavePool->put($item);
}

/**
* 释放数据连接
*/
public function clean()
{
$this->masterPool->clean();
$this->slavePool->clean();
}
}
12 changes: 11 additions & 1 deletion src/Redis/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function get()
$item = $this->pool->pop($this->timeout);

if ($item === false) {
throw new Exception('Get Item instance timeout, all connection is used!');
throw new Exception('Get redis connection instance timeout, pool: ' . $this->getLength());
}

return $item;
Expand All @@ -64,4 +64,14 @@ public function getLength() : int
{
return $this->pool->length();
}

/**
* 清空数据库连接池(释放数据连接)
*/
public function clean()
{
while ($item = $this->pool->pop($this->timeout)) {
$item->close();
}
}
}

0 comments on commit b1718a6

Please sign in to comment.