1 Star 0 Fork 0

xiusin/swoole-bundle

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
KernelPool.php 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
xiusin 提交于 2020-01-17 14:25 +08:00 . 优化
<?php
namespace xiusin\SwooleBundle;
use App\Kernel;
class KernelPool
{
private $pools = [];
private $locker;
private $size;
private $env;
private $debug;
/**
* KernelPool constructor.
* @param $env
* @param $debug
* @param $size max object size
*/
public function __construct($env, $debug, $size, $allinit)
{
$this->locker = new \swoole_lock(SWOOLE_MUTEX);
$this->env = $env;
$this->debug = $debug;
$this->size = $size;
$allinit && $this->init();
}
private function init(){
for ($i = 0 ; $i < $this->size; $i++) {
$kernel = $this->get();
$this->put($kernel);
}
}
public function get(): Kernel
{
$kernel = null;
try {
$this->locker->lock();
if (count($this->pools)) {
$kernel = array_pop($this->pools);
} else {
$kernel = new Kernel($this->env, $this->debug);
}
return $kernel;
} catch (\Throwable $exception) {
throw $exception;
} finally {
$this->locker->unlock();
}
}
public function put(Kernel $kernel)
{
try {
$this->locker->lock();
if (count($this->pools) < $this->size) {
$this->pools[] = $kernel;
} else {
unset($kernel);
}
} catch (\Throwable $exception) {
throw $exception;
} finally {
$this->locker->unlock();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/xiusin/swoole-bundle.git
git@gitee.com:xiusin/swoole-bundle.git
xiusin
swoole-bundle
swoole-bundle
master

搜索帮助