[/Controller/ImServer.php] CLI啟動(dòng),并且onWorkerStart 會(huì)啟動(dòng)3次,因?yàn)槲以O(shè)置了worker->count=3;
public function onWorkerStart($worker){
FaceLog::info('[server]TCP/管道Channel服務(wù)啟動(dòng):|當(dāng)前進(jìn)程池編號(hào):'.$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
* 每個(gè)進(jìn)程啟動(dòng),$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
* 初始化進(jìn)程 渲染workers,與 UserToken
*/
public function Init($worker){
FaceLog::warning('[GlobalData]添加進(jìn)程->'.$worker->name.$worker->id);
$c_worker=[
$worker->name.$worker->id
];
if(!isset($this->G->workers)){
FaceLog::info('[GlobalData]未設(shè)置->workers,現(xiàn)在開(kāi)始設(shè)置');
$this->G->workers=$c_worker;
$this->G->TokenUser=[];
var_dump($this->G->workers);
return true;
}
FaceLog::info('[GlobalData]已設(shè)置->workers,現(xiàn)在開(kāi)始新增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[]');
// }
}
?>
順便問(wèn)下 這個(gè)論壇的 walkor是誰(shuí)啊
已經(jīng)解決:
原因:onWorkerStart 會(huì)啟動(dòng)三次,就是因?yàn)槿尾l(fā)導(dǎo)致add無(wú)法進(jìn)行原子寫入。()
解決辦法:
<?php
declare(strict_types=1);
namespace app\tcp\logic;
use GlobalData\Client;
use think\Exception;
/**
* Class ImGlobalDataBase
* @package app\tcp\logic
* 每個(gè)進(jìn)程啟動(dòng),$this->worker->count次
*/
class ImGlobalDataBase
{
public static $port=2116;
private $global;
public function __construct($worker)
{
$this->global=new Client('0.0.0.0:'.self::$port);
$this->Init($worker);
}
/**
* @param $worker
* @throws \Exception
* 初始化進(jìn)程 渲染workers,與 UserToken
*/
public function Init($worker){
do{
$_old=$_new=$this->global->workers;
$_new[]=[
'worker_id'=>$worker->id,
'worker_name'=>$worker->name,
'worker_ip'=>gethostbyname(gethostname())
];
$apand=$this->global->cas('workers',$_old,$_new);
}while($apand==false);
FaceLog::info('添加workers[]');
}
//TokenUser
public function AddTokenUser($conn){
do{
$old=$new=$this->global->TokenUser;
$_new=[
'uid'=>$conn->_uid,
'channel_subscript'=>$conn->id,
'worker_id'=>$conn->worker->name.$conn->worker->id,
'this_server_ip'=>gethostbyname(gethostname())
];
$new[]=$_new;
FaceLog::info('[GlobalData] '.__FUNCTION__.'添加(如果發(fā)現(xiàn)內(nèi)容重復(fù)請(qǐng)注意BUG)'.jsen($_new));
}while(!$this->global->cas('TokenUser',$old,$new));
unset($old);
unset($_new);
unset($new);
}
/**
* @param $conn
* @throws \Exception
* 移除所有的 TokenUser->[]['uid']= $conn->_uid
*/
public function RemoveTokenUser($conn){
do{
$old=$new=$this->global->TokenUser;
foreach ($new as $k=>$v){
if($v['uid']==$conn->_uid){
FaceLog::info('[GlobalData] '.__FUNCTION__.'移除(如果發(fā)現(xiàn)內(nèi)容重復(fù)請(qǐng)注意BUG)'.jsen($new[$k]));
unset($new[$k]);
}
}
}while(!$this->global->cas('TokenUser',$old,$new));
}
}