Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Jan 22, 2019
1 parent 8210c5f commit 484a88c
Showing 1 changed file with 67 additions and 66 deletions.
133 changes: 67 additions & 66 deletions src/Pool/AbstractPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,124 +22,125 @@ abstract class AbstractPool
private $poolChannel;
private $objHash = [];
private $conf;

/*
* 如果成功创建了,请返回对应的obj
*/
abstract protected function createObject() ;
abstract protected function createObject();

public function __construct(PoolConf $conf)
{
if($conf->getMinObjectNum() >= $conf->getMaxObjectNum()){
if ($conf->getMinObjectNum() >= $conf->getMaxObjectNum()) {
$class = static::class;
throw new PoolObjectNumError("pool max num is small than min num for {$class} error");
}
$this->conf = $conf;
$this->poolChannel = new Channel($conf->getMaxObjectNum() + 1);
if($conf->getIntervalCheckTime() > 0){
swoole_timer_tick($conf->getIntervalCheckTime(),[$this,'intervalCheck']);
if ($conf->getIntervalCheckTime() > 0) {
swoole_timer_tick($conf->getIntervalCheckTime(), [$this, 'intervalCheck']);
}
}

/*
* 回收一个对象
*/
public function recycleObj($obj):bool
public function recycleObj($obj): bool
{
if(is_object($obj)){
if($obj instanceof PoolObjectInterface){
if (is_object($obj)) {
if ($obj instanceof PoolObjectInterface) {
$obj->objectRestore();
}
$ret = $this->putObject($obj);
if($ret){
$ret = $this->putObject($obj);
if ($ret) {
$this->inuse--;
}
return true;
}
return false;
}

public function getObj(float $timeout = null,int $beforeUseTryTimes = 3)
public function getObj(float $timeout = null, int $beforeUseTryTimes = 3)
{
if($timeout === null){
if ($timeout === null) {
$timeout = $this->conf->getGetObjectTimeout();
}
if($beforeUseTryTimes <= 0){
if ($beforeUseTryTimes <= 0) {
return null;
}
//懒惰创建模式
$obj = null;
if($this->poolChannel->isEmpty()){
if ($this->poolChannel->isEmpty()) {
//如果还没有达到最大连接数,则尝试进行创建
if($this->createdNum < $this->conf->getMaxObjectNum()){
if ($this->createdNum < $this->conf->getMaxObjectNum()) {
$this->createdNum++;
/*
* 创建对象的时候,请加try,尽量不要抛出异常
*/
$obj = $this->createObject();
$hash = Random::character(16);
if(is_object($obj)){
if (is_object($obj)) {
//标记手动标记一个id spl_hash 存在坑
$obj->__objectHash = $hash;
//标记为false,才可以允许put回去队列
$this->objHash[$hash] = false;
if(!$this->putObject($obj)){
if (!$this->putObject($obj)) {
$this->createdNum--;
unset($this->objHash[$hash]);
}
}
//同样进入调度等待,理论上此处可以马上pop出来
$obj = $this->poolChannel->pop($timeout);
}else{
} else {
$obj = $this->poolChannel->pop($timeout);
}
}else{
} else {
$obj = $this->poolChannel->pop($timeout);
}
//对对象进行标记处理
if(is_object($obj)){
if (is_object($obj)) {
//上一步已经put object了,put object中设置了__objectHash
$key = $obj->__objectHash;
//标记这个对象已经出队列了
$this->objHash[$key] = false;
if($obj instanceof PoolObjectInterface){
if ($obj instanceof PoolObjectInterface) {
//请加try,尽量不要抛出异常
$status = $obj->beforeUse();
if($status === false){
if ($status === false) {
$this->unsetObj($obj);
//重新进入对象获取
return $this->getObj($timeout,$beforeUseTryTimes - 1);
return $this->getObj($timeout, $beforeUseTryTimes - 1);
}
}
$this->inuse++;
return $obj;
}else{
} else {
return null;
}
}

/*
* 彻底释放一个对象
*/
public function unsetObj($obj):bool
public function unsetObj($obj): bool
{
if(is_object($obj)){
if(!isset($obj->__objectHash)){
if (is_object($obj)) {
if (!isset($obj->__objectHash)) {
return false;
}
$key = $obj->__objectHash;
if(isset($this->objHash[$key])){
if (isset($this->objHash[$key])) {
unset($this->objHash[$key]);
$this->createdNum--;
if($obj instanceof PoolObjectInterface){
if ($obj instanceof PoolObjectInterface) {
$obj->objectRestore();
$obj->gc();
}
unset($obj);
return true;
}else{
} else {
return false;
}
}else{
} else {
return false;
}
}
Expand All @@ -150,21 +151,21 @@ public function unsetObj($obj):bool
public function gcObject(int $idleTime)
{
$list = [];
while (true){
if(!$this->poolChannel->isEmpty()){
while (true) {
if (!$this->poolChannel->isEmpty()) {
$obj = $this->poolChannel->pop(0.001);
if(is_object($obj)){
if(time() - $obj->last_recycle_time > $idleTime){
if (is_object($obj)) {
if (time() - $obj->last_recycle_time > $idleTime) {
$this->unsetObj($obj);
}else{
array_push($list,$obj);
} else {
array_push($list, $obj);
}
}
}else{
} else {
break;
}
}
foreach ($list as $item){
foreach ($list as $item) {
$this->poolChannel->push($item);
}
}
Expand All @@ -175,54 +176,54 @@ protected function intervalCheck()
$this->keepMin();
}

protected function getPoolConfig():PoolConf
protected function getPoolConfig(): PoolConf
{
return $this->conf;
}

public static function invoke(callable $call,float $timeout = null)
public static function invoke(callable $call, float $timeout = null)
{
$pool = PoolManager::getInstance()->getPool(static::class);
if($pool instanceof AbstractPool){
if ($pool instanceof AbstractPool) {
$obj = $pool->getObj($timeout);
if($obj){
try{
$ret = call_user_func($call,$obj);
if ($obj) {
try {
$ret = call_user_func($call, $obj);
return $ret;
}catch (\Throwable $throwable){
} catch (\Throwable $throwable) {
throw $throwable;
}finally{
} finally {
$pool->recycleObj($obj);
}
}else{
throw new PoolEmpty(static::class." pool is empty");
} else {
throw new PoolEmpty(static::class . " pool is empty");
}
}else{
throw new PoolException(static::class." convert to pool error");
} else {
throw new PoolException(static::class . " convert to pool error");
}
}

public function keepMin(?int $num = null):int
public function keepMin(?int $num = null): int
{
if($num == null){
if ($num == null) {
$num = $this->conf->getMinObjectNum();
}
if($this->createdNum >= $num){
if ($this->createdNum >= $num) {
return $this->createdNum;
}else{
} else {
$num = $num - $this->createdNum;
}

for ($i= 0;$i < $num;$i++){
for ($i = 0; $i < $num; $i++) {
$this->createdNum++;
$obj = $this->createObject();
$hash = Random::character(16);
if(is_object($obj)){
if (is_object($obj)) {
//标记手动标记一个id spl_hash 存在坑
$obj->__objectHash = $hash;
//标记为false,才可以允许put回去队列
$this->objHash[$hash] = false;
if(!$this->putObject($obj)){
if (!$this->putObject($obj)) {
$this->createdNum--;
}
}
Expand All @@ -234,23 +235,23 @@ public function keepMin(?int $num = null):int
/*
* 用以解决冷启动问题,其实是是keepMin别名
*/
public function preLoad(?int $num = null):int
public function preLoad(?int $num = null): int
{
return $this->keepMin($num);
}

/*
* 把一个对象归还到队列中
*/
protected function putObject($object):bool
protected function putObject($object): bool
{
if(is_object($object)){
if(!isset($object->__objectHash)){
if (is_object($object)) {
if (!isset($object->__objectHash)) {
return false;
}
$hash = $object->__objectHash;
//不在的时候说明为其他pool对象,不允许归还,若为true,说明已经归还,禁止重复
if(isset($this->objHash[$hash]) && ($this->objHash[$hash] == false)){
if (isset($this->objHash[$hash]) && ($this->objHash[$hash] == false)) {
$object->last_recycle_time = time();
$this->objHash[$hash] = true;
$this->poolChannel->push($object);
Expand All @@ -263,10 +264,10 @@ protected function putObject($object):bool
public function status()
{
return [
'created'=>$this->createdNum,
'inuse'=>$this->inuse,
'max'=>$this->getPoolConfig()->getMaxObjectNum(),
'min'=>$this->getPoolConfig()->getMinObjectNum()
'created' => $this->createdNum,
'inuse' => $this->inuse,
'max' => $this->getPoolConfig()->getMaxObjectNum(),
'min' => $this->getPoolConfig()->getMinObjectNum()
];
}
}

0 comments on commit 484a88c

Please sign in to comment.