Skip to content

Commit

Permalink
pools的channel的pop添加timeout,防止死循环
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Jan 6, 2019
1 parent cde9d23 commit 99eff79
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 3 additions & 3 deletions score/Core/BService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function send($fd, $data, $header = []) {
$data = \Swoolefy\Tcp\TcpServer::pack($args);
Swfy::getServer()->send($fd, $data);
}else {
throw new \Exception("this method only can be called by tcp or rpc server!");
throw new \Exception("BService::send() this method only can be called by tcp or rpc server!");
}

}
Expand All @@ -91,7 +91,7 @@ public function sendto($ip, $port, $data, $server_socket = -1) {
}
Swfy::getServer()->sendto($ip, $port, $data, $server_socket);
}else {
throw new \Exception("this method only can be called by udp server!");
throw new \Exception("BService::sendto() this method only can be called by udp server!");
}
}

Expand All @@ -112,7 +112,7 @@ public function push($fd, $data, $opcode = 1, $finish = true) {
$result = Swfy::getServer()->push($fd, $data, $opcode, $finish);
return $result;
}else {
throw new \Exception("this method only can be called by websocket server!");
throw new \Exception("BService::push() this method only can be called by websocket server!");
}

}
Expand Down
6 changes: 5 additions & 1 deletion score/Core/Pools.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ public function registerPools(string $poolsName, int $size = 5 * 1024 * 1024) {
* @param string $poolsName
* @return mixed
*/
public function getObj(string $poolsName) {
public function getObj(string $poolsName, int $timeout = 10) {
if(Swfy::isWorkerProcess()) {
$worker_id = Swfy::getCurrentWorkerId();
if(isset($this->pools[$poolsName][$worker_id])) {
$chan = $this->pools[$poolsName][$worker_id];
$obj = '';
$start_time = time();
while(1) {
$obj = $chan->pop();
if(is_object($obj)) {
$this->sendMessage($poolsName);
break;
}else {
if((time() - $start_time) > $timeout) {
break;
}
usleep(1000);
}
}
Expand Down
2 changes: 1 addition & 1 deletion score/Core/SwoolefyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static function appError($errno, $errstr, $errfile, $errline) {
public static function shutHalt($errorMsg, $errorType = 'error') {
$logFilePath = rtrim(LOG_PATH,'/').'/runtime.log';
if(is_file($logFilePath)) {
$logFilesSize = filesize($logFilePath);
$logFilesSize = filesize($logFilePath);
}
// 定时清除这个log文件
if($logFilesSize > 1024 * 20) {
Expand Down

0 comments on commit 99eff79

Please sign in to comment.