Files
kiri-core/System/Abstracts/Pool.php
T

147 lines
2.5 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
namespace Snowflake\Abstracts;
use Exception;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
/**
* Class Pool
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Pool
2020-08-31 01:27:08 +08:00
*/
abstract class Pool extends Component
{
/** @var Channel[] */
private $_items = [];
2020-09-04 19:10:08 +08:00
protected $max = 60;
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @param false $isMaster
* @param int $max
*/
public function initConnections($name, $isMaster = false, $max = 60)
{
$name = $this->name($name, $isMaster);
2020-09-05 03:51:21 +08:00
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) {
2020-08-31 01:27:08 +08:00
return;
}
$this->_items[$name] = new Channel($max);
2020-09-04 19:10:08 +08:00
$this->max = $max;
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
* @return mixed
2020-08-31 12:38:32 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2020-09-05 01:29:46 +08:00
protected function get($name)
2020-08-31 01:27:08 +08:00
{
2020-09-05 03:08:39 +08:00
$this->debug('get connect.' . $this->size($name));
2020-09-05 02:31:08 +08:00
[$timeout, $connection] = $this->_items[$name]->pop();
2020-09-04 19:16:09 +08:00
if (!$this->checkCanUse($name, $timeout, $connection)) {
2020-08-31 01:27:08 +08:00
unset($client);
return [0, null];
} else {
2020-09-05 01:29:46 +08:00
return [$timeout, $connection];
2020-08-31 01:27:08 +08:00
}
}
/**
* @param $cds
* @param false $isMaster
* @return string
*/
public function name($cds, $isMaster = false)
{
return hash('sha1', $cds . ($isMaster ? 'master' : 'slave'));
}
/**
2020-09-04 19:10:08 +08:00
* @param $name
2020-08-31 01:27:08 +08:00
* @param $time
* @param $client
* @return mixed
* 检查连接可靠性
* @throws Exception
*/
2020-09-04 19:10:08 +08:00
public function checkCanUse($name, $time, $client)
2020-08-31 01:27:08 +08:00
{
throw new Exception('Undefined system processing function.');
}
/**
* @param $name
* @throws Exception
*/
public function desc($name)
{
throw new Exception('Undefined system processing function.');
}
/**
* @param array $config
* @param $isMaster
* @throws Exception
*/
public function getConnection(array $config, $isMaster)
{
throw new Exception('Undefined system processing function.');
}
/**
* @param $name
* @return mixed
*/
public function hasItem($name)
{
2020-09-04 19:00:27 +08:00
return $this->size($name) > 0;
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
* @return mixed
*/
2020-09-04 19:00:27 +08:00
public function size($name)
2020-08-31 01:27:08 +08:00
{
if (!isset($this->_items[$name])) {
return 0;
}
return $this->_items[$name]->length();
}
/**
* @param $name
* @param $client
*/
public function push($name, $client)
{
2020-09-05 01:49:36 +08:00
$this->_items[$name]->push([time(), $client]);
2020-09-05 03:08:39 +08:00
unset($client);
2020-09-05 03:51:21 +08:00
$this->debug('release connect.' . $this->max . ':' . $this->size($name));
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
*/
public function clean($name)
{
if (!isset($this->_items[$name])) {
return;
}
while ([$time, $client] = $this->_items[$name]->pop(0.001)) {
unset($client);
}
}
}