Skip to content

Commit

Permalink
solve the blocking problem
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Jun 17, 2021
1 parent b3eef2f commit 783b56f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/Api/WebSocket/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protected function init(){

$this->add('keysecret',[]);//目前总共key

$this->add('global_local',[]);//临时存储数据

$this->add('debug',[]);
}

Expand Down Expand Up @@ -129,6 +131,7 @@ protected function getData($global,$callback=null,$sub=[]){
$all_sub=$global->get('all_sub');
if(empty($all_sub)) return [];

$global_local=$global->get('global_local');
$temp=[];

//默认返回所有数据
Expand All @@ -142,8 +145,10 @@ protected function getData($global,$callback=null,$sub=[]){
$temp[strtolower($vv)]=$data;
}
}else{
$data=$global->get(strtolower($v));
$temp[strtolower($v)]=$data;
//$data=$global->get(strtolower($v));
//$temp[strtolower($v)]=$data;
$k_strtolower=strtolower($v);
$temp[$k_strtolower]=$global_local['public'][$k_strtolower];
}
}
}else{
Expand All @@ -167,7 +172,8 @@ protected function getData($global,$callback=null,$sub=[]){
$data=$global->getQueue(strtolower($v));
}else{
//public
$data=$global->get(strtolower($v));
//$data=$global->get(strtolower($v));
$data=$global_local['public'][strtolower($v)];
}

if(empty($data)) continue;
Expand Down
15 changes: 13 additions & 2 deletions src/Api/WebSocket/SocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SocketServer

private $public_url=['market','kline'];

private $local_global=['public'=>[],'private'=>[]];

function __construct(array $config=[])
{
$this->config=$config;
Expand Down Expand Up @@ -172,7 +174,9 @@ private function onMessage($global){
$table=$this->userKey($con->tag_keysecret,$table);
$global->saveQueue($table,$data);
}else{
$global->save($table,$data);
//$global->save($table,$data);
$this->local_global['public'][$table]=$data;
//echo date('Y-m-d H:i:s',$data['tick']['ts']/1000).PHP_EOL;

//最后数据更新时间
$con->tag_data_time=time();
Expand Down Expand Up @@ -229,7 +233,8 @@ private function onMessage($global){
$table=$this->userKey($con->tag_keysecret,$table);
$global->saveQueue($table,$data);
}else{
$global->save($table,$data);
//$global->save($table,$data);
$this->local_global['public'][$table]=$data;

//最后数据更新时间
$con->tag_data_time=time();
Expand Down Expand Up @@ -307,6 +312,12 @@ private function other($con,$global){
//private
}
});


//异步保存数据,不然会有阻塞问题。 0.2秒保存一次
Timer::add(0.2, function() use($global) {
$global->save('global_local',$this->local_global);
});
}

/**
Expand Down

0 comments on commit 783b56f

Please sign in to comment.