This commit is contained in:
2021-03-29 17:47:13 +08:00
parent ec101543c9
commit 442386866d
7 changed files with 44 additions and 46 deletions
+8 -2
View File
@@ -13,6 +13,7 @@ namespace Database;
use Exception;
use Database\Base\BaseActiveRecord;
use ReflectionException;
use Snowflake\Channel;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
@@ -130,7 +131,10 @@ class ActiveRecord extends BaseActiveRecord
}
$className = get_called_class();
$select = objectPool($className, function () use ($className) {
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
$select = $channel->pop($className, function () use ($className) {
return new $className();
});
@@ -284,7 +288,9 @@ class ActiveRecord extends BaseActiveRecord
*/
public function recover()
{
objectRecover(get_called_class(), $this);
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
return $channel->push($this, get_called_class());
}
+6 -2
View File
@@ -18,6 +18,7 @@ use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Abstracts\Component;
use Database\ActiveRecord;
use Snowflake\Channel;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
@@ -35,7 +36,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
*/
protected array $_item = [];
protected ?ActiveRecord $model;
protected ActiveRecord|string|null $model;
protected ActiveQuery $query;
@@ -119,7 +120,10 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
if (is_object($model)) {
return $model;
}
return objectPool($model, function () use ($model) {
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
return $channel->pop($model, function () use ($model) {
return new $model();
});
}
+4 -2
View File
@@ -29,6 +29,7 @@ use Database\Mysql\Columns;
use Database\Relation;
use Exception;
use Snowflake\Abstracts\TraitApplication;
use Snowflake\Channel;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use validator\Validator;
@@ -1032,8 +1033,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
{
$className = get_called_class();
/** @var static $model */
$model = objectPool($className, function () use ($className) {
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
$model = $channel->pop($className, function () use ($className) {
return new $className();
});
$model->_attributes = $data;
+8 -3
View File
@@ -9,6 +9,7 @@ use Database\ActiveQuery;
use Database\ActiveRecord;
use Exception;
use ReflectionException;
use Snowflake\Channel;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
@@ -56,14 +57,18 @@ class CollectionIterator extends \ArrayIterator
*/
protected function newModel($current): ActiveRecord
{
/** @var ActiveRecord|string $model */
$model = $this->model;
if (is_object($model)) {
$model = get_class($model);
}
return objectPool($model, function () use ($model) {
return new $model();
})->setAttributes($current);
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
$model = $channel->pop($model, function () use ($model) {
return new $model();
});
return $model->setAttributes($current);
}
+5 -1
View File
@@ -6,7 +6,9 @@ namespace Rpc;
use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Channel;
use Snowflake\Core\Json;
use Snowflake\Snowflake;
use Swoole\Coroutine\Client as CClient;
@@ -90,7 +92,9 @@ class Client extends Component
*/
public function getClient(): CClient
{
return objectPool(CClient::class, function () {
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
return $channel->pop(CClient::class, function () {
$client = new CClient($this->config['mode'] ?? SWOOLE_SOCK_TCP);
$client->set([
'timeout' => 0.5,
+5 -3
View File
@@ -76,14 +76,16 @@ class Channel extends Component
* @return mixed
* @throws Exception
*/
public function pop(int|float $timeout, string $name, Closure $closure, int $length = 100): mixed
public function pop(string $name, Closure $closure, int|float $timeout = null, int $length = 999): mixed
{
if (($channel = $this->channelInit($length, $name)) == false) {
return $this->addError('Channel is full.');
}
$data = null;
if (!$channel->isEmpty()) {
return $channel->pop();
}
$data = null;
if ($timeout !== null) {
$data = $channel->pop($timeout);
}
if (empty($data)) {
+8 -33
View File
@@ -16,7 +16,9 @@ use HttpServer\Service\Websocket;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Config;
use Snowflake\Error\Logger;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use HttpServer\Http\Context;
use Snowflake\Core\ArrayAccess;
@@ -303,47 +305,20 @@ if (!function_exists('aop')) {
/**
* @param string $event
* @param mixed $handler
* @param array $params
* @throws Exception
* @throws Exception
* @return mixed
* @throws ReflectionException
* @throws ComponentException
* @throws NotFindClassException
*/
function aop(mixed $handler, array $params = [])
function aop(mixed $handler, array $params = []): mixed
{
return Snowflake::app()->get('aop')->dispatch($handler, ...$params);
}
}
if (!function_exists('objectPool')) {
/**
* @param string $className
* @param callable $construct
* @return mixed
* @throws Exception
*/
function objectPool(mixed $className, callable $construct): mixed
{
return Snowflake::app()->getObject()->load($className, $construct);
}
}
if (!function_exists('objectRecover')) {
/**
* @param string $className
* @param $object
* @return mixed
* @throws Exception
*/
function objectRecover(mixed $className, $object): void
{
Snowflake::app()->getObject()->release($className, $object);
}
}
if (!function_exists('instance_load')) {
function instance_load()