Skip to content

Commit

Permalink
适配动态创建pool
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Jan 28, 2019
1 parent 475d7d3 commit 483dc82
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/Pool/PoolManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,26 @@ function getDefaultConfig()

function register(string $className, $maxNum = 20):?PoolConf
{
$ref = new \ReflectionClass($className);
if($ref->isSubclassOf(AbstractPool::class)){
$conf = clone $this->defaultConfig;
$conf->setClass($className);
$conf->setMaxObjectNum($maxNum);
$this->pool[$this->generateKey($className)] = $conf;
return $conf;
}else{
try{
$ref = new \ReflectionClass($className);
if($ref->isSubclassOf(AbstractPool::class)){
$conf = clone $this->defaultConfig;
$conf->setClass($className);
$conf->setMaxObjectNum($maxNum);
$this->pool[$this->generateKey($className)] = $conf;
return $conf;
}else{
return null;
}
}catch (\Throwable $throwable){
return null;
}
}

/*
* 请在进程克隆后,也就是worker start后,每个进程中独立使用
*/
function getPool(string $className):?AbstractPool
function getPool(string $className,?callable $createCall = null):?AbstractPool
{
$key = $this->generateKey($className);
if(isset($this->pool[$key])){
Expand All @@ -58,21 +62,34 @@ function getPool(string $className):?AbstractPool
$this->pool[$key] = $obj;
return $obj;
}
}else if(class_exists($className)){
if(!$this->register($className)){
}else{
//尝试注册。
if(class_exists($this->register($className)) && $this->register($className)){
return $this->getPool($className);
}else{
$config = clone $this->defaultConfig;
$config->setClass($className);
$pool = new class($config) extends AbstractPool{
$pool = new class($config,$createCall) extends AbstractPool{
protected $createCall;
public function __construct(PoolConf $conf,$createCall)
{
$this->createCall = $createCall;
parent::__construct($conf);
}
protected function createObject()
{
// TODO: Implement createObject() method.
$className = $this->getPoolConfig()->getClass();
return new $className;
if(is_callable($this->createCall)){
return call_user_func($this->createCall);
}else{
$className = $this->getPoolConfig()->getClass();
return new $className;
}
}
};
$this->pool[$key] = $pool;
return $pool;
}
return $this->getPool($className);
}
return null;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,25 @@ function testNormalClass()
$obj = $pool->getObj();
$this->assertEquals(PoolObject::class,$obj->fuck());
}

function testNormalClass2()
{
$pool = PoolManager::getInstance()->getPool('test',function (){
// var_dump('dy');
return new PoolObject();
});
/**
* @var $obj PoolObject
*/
$obj = $pool->getObj();
$hash1 = $obj->__objectHash;
$this->assertEquals(PoolObject::class,$obj->fuck());
$pool->recycleObj($obj);

$obj = $pool->getObj();
$hash2 = $obj->__objectHash;
$pool->recycleObj($obj);
$this->assertEquals($pool->status()['created'],1);
$this->assertEquals($hash1,$hash2);
}
}

0 comments on commit 483dc82

Please sign in to comment.