[/Controller/ImServer.php] CLI启动,并且onWorkerStart 会启动3次,因为我设置了worker->count=3;
public function onWorkerStart($worker){
FaceLog::info('[server]TCP/管道Channel服务启动:|当前进程池编号:'.$worker->id);
try{
new ImGlobalDataBase($worker);
}catch (\Exception $e){
FaceLog::error('[error]'.$e->getMessage().'[line]'.$e->getLine().'[file]'.$e->getFile());
}
}
<?php
declare(strict_types=1);
namespace app\tcp\logic;
use GlobalData\Client;
use think\Exception;
/**
* Class ImGlobalDataBase
* @package app\tcp\logic
* 每个进程启动,$this->worker->count次
*/
class ImGlobalDataBase
{
public static $port=2116;
private $G;
public function __construct($worker)
{
$this->G=new Client('0.0.0.0:'.self::$port);
$this->Init($worker);
}
/**
* @param $worker
* @throws \Exception
* 初始化进程 渲染workers,与 UserToken
*/
public function Init($worker){
FaceLog::warning('[GlobalData]添加进程->'.$worker->name.$worker->id);
$c_worker=[
$worker->name.$worker->id
];
if(!isset($this->G->workers)){
FaceLog::info('[GlobalData]未设置->workers,现在开始设置');
$this->G->workers=$c_worker;
$this->G->TokenUser=[];
var_dump($this->G->workers);
return true;
}
FaceLog::info('[GlobalData]已设置->workers,现在开始新增worker');
$_workers=$this->G->workers;
$_workers[]=$c_worker;
$this->G->workers=$_workers;
return true;
// if(!isset($this->G->workers)){
// //init
//
// if(!$this->G->add('workers',$c_worker)){
// throw new Exception(workers__FUNCTION__.'失败workers[init first]');
// }
// FaceLog::info('添加workers[init first]');
// if(!$this->G->add('TokenUser',[])){
// throw new Exception(__FUNCTION__.'失败TokenUser');
// }
//
// FaceLog::info('添加TokenUser[init first]');
// }else{
// $_workers=$this->G->workers;
// $_workers[]= $worker->name.$worker->id;
// if(!$this->G->cas('workers',$this->G->workers,$_workers)){
// throw new Exception(__FUNCTION__.'失败workers');
// }
// FaceLog::info('添加workers[]');
// }
}
?>
顺便问下 这个论坛的 walkor是谁啊
已经解决:
原因:onWorkerStart 会启动三次,就是因为三次并发导致add无法进行原子写入。()
解决办法:
walkor大大是workerman的作者