Skip to content

Commit

Permalink
add channel data time monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Mar 31, 2021
1 parent 0e9b8d9 commit d9b1c3c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Api/WebSocket/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,8 @@ function test_reconnection(){
'public'=>['market'=>'close','kline'=>'close'],
];
}

function test_reconnection2(){
$this->client->debug2=1;
}
}
31 changes: 28 additions & 3 deletions src/Api/WebSocket/SocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public function start(){

$this->worker->onWorkerStart = function() {
$this->addConnection('market');
//$this->addConnection('order');

if(in_array($this->getPlatform(),['future','swap','linear'])) $this->addConnection('kline');

//$this->addConnection('system');
};

Worker::runAll();
Expand Down Expand Up @@ -97,6 +94,7 @@ private function newConnection(){
//自定义属性
$this->connection[$this->connectionIndex]->tag=$tag;//标记公共连接还是私有连接
$this->connection[$this->connectionIndex]->tag_baseurl=$baseurl;
$this->connection[$this->connectionIndex]->tag_reconnection_num=0;//标记当前已重连次数
if(!empty($keysecret)) $this->connection[$this->connectionIndex]->tag_keysecret=$keysecret;//标记私有连接

$this->connection[$this->connectionIndex]->onConnect=$this->onConnect($keysecret);
Expand Down Expand Up @@ -177,6 +175,10 @@ private function onMessage($global){
$global->save($table,$data);
}

//最后数据更新时间
$con->tag_data_time=time();
//成功接收数据重连次数回归0
$con->tag_reconnection_num=0;
return;
}

Expand Down Expand Up @@ -230,6 +232,10 @@ private function onMessage($global){
$global->save($table,$data);
}

//最后数据更新时间
$con->tag_data_time=time();
//成功接收数据重连次数回归0
$con->tag_reconnection_num=0;
return;
}

Expand Down Expand Up @@ -278,6 +284,25 @@ private function other($con,$global){
$this->debug($con,$global);

$this->log('listen '.$con->tag);

/*if(isset($con->tag_data_time)){
echo time()-$con->tag_data_time;
echo PHP_EOL;
}*/

//公共数据如果60秒内无数据更新,则断开连接重新订阅,重试次数不超过5次
if(isset($con->tag_data_time) && time()-$con->tag_data_time>10*($con->tag_reconnection_num+1) && $con->tag_reconnection_num<=5){
if(in_array($con->tag,$this->public_url)) {
//public
$con->close();
}else{
//private
}

$con->tag_reconnection_num++;

$this->log('listen '.$con->tag.' reconnection_num:'.$con->tag_reconnection_num.' tag_data_time:'.$con->tag_data_time);
}
});
}

Expand Down
5 changes: 5 additions & 0 deletions tests/websocket/client_spot.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@
$huobi->client()->test_reconnection();
break;
}

case 10006:{
$huobi->client()->test_reconnection2();
break;
}
}


0 comments on commit d9b1c3c

Please sign in to comment.