This commit is contained in:
2020-09-04 19:10:08 +08:00
parent 4f8db41ca1
commit 075861fdad
3 changed files with 48 additions and 20 deletions
+5 -3
View File
@@ -18,7 +18,7 @@ abstract class Pool extends Component
/** @var Channel[] */
private $_items = [];
public $max = 60;
protected $max = 60;
private $is_ticker = false;
@@ -35,6 +35,7 @@ abstract class Pool extends Component
return;
}
$this->_items[$name] = new Channel($max);
$this->max = $max;
}
/**
@@ -53,7 +54,7 @@ abstract class Pool extends Component
} else {
$client = $this->_items[$name]->pop();
}
if (!$this->checkCanUse(...$client)) {
if (!$this->checkCanUse($name, ...$client)) {
unset($client);
return [0, null];
} else {
@@ -73,13 +74,14 @@ abstract class Pool extends Component
/**
* @param $name
* @param $time
* @param $client
* @return mixed
* 检查连接可靠性
* @throws Exception
*/
public function checkCanUse($time, $client)
public function checkCanUse($name, $time, $client)
{
throw new Exception('Undefined system processing function.');
}
+23 -6
View File
@@ -22,6 +22,9 @@ class Connection extends Pool
/** @var PDO[] */
protected $connections = [];
private $creates = 0;
/**
* @param $timeout
*/
@@ -170,6 +173,7 @@ class Connection extends Pool
}
for ($i = 0; $i < 10 - $this->size($name); $i++) {
$this->push($name, $this->createConnect($config['cds'], $config['username'], $config['password']));
$this->incr($name);
}
return $this;
}
@@ -187,7 +191,13 @@ class Connection extends Pool
if ($client instanceof PDO) {
return $this->saveClient($coroutineName, $client);
}
unset($client);
if (($this->hasCreate[$coroutineName] ?? 0) >= $this->max) {
[$time, $client] = $this->get($coroutineName, -1);
return $client;
}
if ($this->hasItem($coroutineName) < 1) {
return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config));
}
@@ -243,6 +253,7 @@ class Connection extends Pool
}
$this->push($coroutineName, $client);
$this->remove($coroutineName);
$this->desc($coroutineName);
}
@@ -274,6 +285,7 @@ class Connection extends Pool
$this->push($name, $connection);
$this->remove($name);
}
$this->hasCreate = [];
}
@@ -286,25 +298,30 @@ class Connection extends Pool
}
/**
* @param $name
* @param $time
* @param $connect
* @return bool
*/
public function checkCanUse($time, $connect)
public function checkCanUse($name, $time, $connect)
{
try {
if ($time + 60 * 10 < time()) {
return false;
return $result = false;
}
if (empty($connect) || !($connect instanceof PDO)) {
return false;
return $result = false;
}
if (!$connect->getAttribute(PDO::ATTR_SERVER_INFO)) {
return false;
return $result = false;
}
return true;
return $result = true;
} catch (\Swoole\Error | \Throwable $exception) {
return false;
return $result = false;
} finally {
if (!$result) {
$this->desc($name);
}
}
}
+20 -11
View File
@@ -145,23 +145,32 @@ class Redis extends Pool
}
/**
* @param $name
* @param $time
* @param $client
* @return bool|mixed
* @throws RedisException
* @throws Exception
*/
public function checkCanUse($time, $client)
public function checkCanUse($name, $time, $client)
{
if ($time + 60 * 10 < time()) {
return false;
try {
if ($time + 60 * 10 < time()) {
return $result = false;
}
if (!($client instanceof SRedis)) {
return $result = false;
}
if (!$client->isConnected() || !$client->ping('connect.')) {
return $result = false;
}
return $result = true;
} catch (Exception $exception) {
return $result = false;
} finally {
if (!$result) {
$this->desc($name);
}
}
if (!($client instanceof SRedis)) {
return false;
}
if (!$client->isConnected() || !$client->ping('connect.')) {
return false;
}
return true;
}
public function desc($name)