2021-02-22 17:44:24 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Snowflake\Pool;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use ReflectionException;
|
2021-02-23 17:30:10 +08:00
|
|
|
use Snowflake\Event;
|
2021-02-23 19:00:22 +08:00
|
|
|
use Snowflake\Exception\ComponentException;
|
2021-02-22 17:44:24 +08:00
|
|
|
use Snowflake\Exception\NotFindClassException;
|
|
|
|
|
use Snowflake\Snowflake;
|
2021-02-24 14:39:26 +08:00
|
|
|
use Swoole\Coroutine;
|
2021-02-22 17:44:24 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class ObjectPool
|
|
|
|
|
* @package Snowflake\Pool
|
|
|
|
|
*/
|
|
|
|
|
class ObjectPool extends \Snowflake\Abstracts\Pool
|
|
|
|
|
{
|
|
|
|
|
|
2021-02-23 19:27:28 +08:00
|
|
|
private array $_waitRecover = [];
|
|
|
|
|
|
2021-02-22 17:44:24 +08:00
|
|
|
|
2021-02-23 16:56:34 +08:00
|
|
|
/**
|
2021-02-24 14:25:04 +08:00
|
|
|
* ObjectPool constructor.
|
|
|
|
|
* @param array $config
|
2021-02-23 19:27:28 +08:00
|
|
|
* @throws ComponentException
|
2021-02-24 14:25:24 +08:00
|
|
|
* @throws Exception
|
2021-02-23 16:56:34 +08:00
|
|
|
*/
|
2021-02-24 14:25:04 +08:00
|
|
|
public function __construct($config = [])
|
2021-02-23 16:56:34 +08:00
|
|
|
{
|
2021-02-23 19:31:00 +08:00
|
|
|
$this->max = 5000;
|
2021-02-23 19:27:28 +08:00
|
|
|
|
2021-02-24 14:27:16 +08:00
|
|
|
listen(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'destruct']);
|
2021-02-24 14:25:04 +08:00
|
|
|
|
|
|
|
|
parent::__construct($config);
|
2021-02-23 16:56:34 +08:00
|
|
|
}
|
2021-02-23 00:40:51 +08:00
|
|
|
|
|
|
|
|
|
2021-02-23 16:56:34 +08:00
|
|
|
/**
|
|
|
|
|
* @param array $config
|
2021-02-24 14:22:56 +08:00
|
|
|
* @param callable $construct
|
2021-02-23 16:56:34 +08:00
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-02-24 14:22:56 +08:00
|
|
|
public function load(mixed $config, callable $construct): mixed
|
2021-02-23 16:56:34 +08:00
|
|
|
{
|
|
|
|
|
if (is_object($config)) {
|
|
|
|
|
return $config;
|
|
|
|
|
}
|
2021-02-23 19:00:22 +08:00
|
|
|
return $this->getFromChannel($name = md5($config), [$config, $construct]);
|
2021-02-23 16:56:34 +08:00
|
|
|
}
|
2021-02-23 00:38:33 +08:00
|
|
|
|
|
|
|
|
|
2021-02-23 16:56:34 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param mixed $config
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function createClient(string $name, mixed $config): mixed
|
|
|
|
|
{
|
2021-02-24 14:22:56 +08:00
|
|
|
return call_user_func($config[1]);
|
2021-02-23 16:56:34 +08:00
|
|
|
}
|
2021-02-23 00:38:33 +08:00
|
|
|
|
|
|
|
|
|
2021-02-23 16:56:34 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param $object
|
|
|
|
|
*/
|
|
|
|
|
public function release(string $name, mixed $object)
|
|
|
|
|
{
|
2021-02-23 19:27:49 +08:00
|
|
|
if (!isset($this->_waitRecover[$name])) {
|
|
|
|
|
$this->_waitRecover[$name] = [];
|
|
|
|
|
}
|
2021-02-23 19:27:28 +08:00
|
|
|
$this->_waitRecover[$name][] = $object;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-23 19:30:50 +08:00
|
|
|
/**
|
|
|
|
|
* 清理等待回收的对象
|
|
|
|
|
*/
|
2021-02-23 19:27:28 +08:00
|
|
|
public function destruct()
|
|
|
|
|
{
|
2021-02-24 14:39:26 +08:00
|
|
|
$this->async_create(function ($scope) {
|
|
|
|
|
if (empty($scope->_waitRecover)) {
|
|
|
|
|
return;
|
2021-02-23 19:27:28 +08:00
|
|
|
}
|
2021-02-24 14:39:26 +08:00
|
|
|
foreach ($scope->_waitRecover as $name => $value) {
|
|
|
|
|
if (empty($value)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
foreach ($value as $object) {
|
|
|
|
|
if (method_exists($object, 'clean')) {
|
|
|
|
|
$object->clean();
|
|
|
|
|
}
|
|
|
|
|
$scope->push($name, $object);
|
2021-02-23 19:27:28 +08:00
|
|
|
}
|
2021-02-24 14:39:26 +08:00
|
|
|
unset($scope->_waitRecover[$name]);
|
2021-02-23 19:27:28 +08:00
|
|
|
}
|
2021-02-24 14:39:26 +08:00
|
|
|
}, $this);
|
2021-02-23 16:56:34 +08:00
|
|
|
}
|
2021-02-22 17:44:24 +08:00
|
|
|
|
|
|
|
|
}
|