SocketWorker.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. <?php
  2. namespace Man\Core;
  3. require_once WORKERMAN_ROOT_DIR . 'man/Core/Events/Select.php';
  4. require_once WORKERMAN_ROOT_DIR . 'man/Core/AbstractWorker.php';
  5. require_once WORKERMAN_ROOT_DIR . 'man/Core/Lib/Config.php';
  6. /**
  7. * SocketWorker 监听某个端口,对外提供网络服务的worker
  8. *
  9. * @author walkor <worker-man@qq.com>
  10. *
  11. * <b>使用示例:</b>
  12. * <pre>
  13. * <code>
  14. * $worker = new SocketWorker();
  15. * $worker->start();
  16. * <code>
  17. * </pre>
  18. */
  19. abstract class SocketWorker extends AbstractWorker
  20. {
  21. /**
  22. * udp最大包长 linux:65507 mac:9216
  23. * @var integer
  24. */
  25. const MAX_UDP_PACKEG_SIZE = 65507;
  26. /**
  27. * 停止服务后等待EXIT_WAIT_TIME秒后还没退出则强制退出
  28. * @var integer
  29. */
  30. const EXIT_WAIT_TIME = 3;
  31. /**
  32. * worker的传输层协议
  33. * @var string
  34. */
  35. protected $protocol = "tcp";
  36. /**
  37. * worker监听端口的Socket
  38. * @var resource
  39. */
  40. protected $mainSocket = null;
  41. /**
  42. * worker接受的所有链接
  43. * @var array
  44. */
  45. protected $connections = array();
  46. /**
  47. * worker的所有读buffer
  48. * @var array
  49. */
  50. protected $recvBuffers = array();
  51. /**
  52. * 当前处理的fd
  53. * @var integer
  54. */
  55. protected $currentDealFd = 0;
  56. /**
  57. * UDP当前处理的客户端地址
  58. * @var string
  59. */
  60. protected $currentClientAddress = '';
  61. /**
  62. * worker的服务状态
  63. * @var integer
  64. */
  65. protected $workerStatus = self::STATUS_RUNNING;
  66. /**
  67. * 是否是长链接,(短连接每次请求后服务器主动断开,长连接一般是客户端主动断开)
  68. * @var bool
  69. */
  70. protected $isPersistentConnection = false;
  71. /**
  72. * 事件轮询库的名称
  73. * @var string
  74. */
  75. protected $eventLoopName ="\\Man\\Core\\Events\\Select";
  76. /**
  77. * 时间轮询库实例
  78. * @var object
  79. */
  80. protected $event = null;
  81. /**
  82. * worker名称
  83. * @var string
  84. */
  85. protected $workerName = __CLASS__;
  86. /**
  87. * 该worker进程处理多少请求后退出,0表示不自动退出
  88. * @var integer
  89. */
  90. protected $maxRequests = 0;
  91. /**
  92. * 预读长度
  93. * @var integer
  94. */
  95. protected $prereadLength = 4;
  96. /**
  97. * 该进程使用的php文件
  98. * @var array
  99. */
  100. protected $includeFiles = array();
  101. /**
  102. * 统计信息
  103. * @var array
  104. */
  105. protected $statusInfo = array(
  106. 'start_time' => 0, // 该进程开始时间戳
  107. 'total_request' => 0, // 该进程处理的总请求数
  108. 'recv_timeout' => 0, // 该进程接收数据超时总数
  109. 'proc_timeout' => 0, // 该进程逻辑处理超时总数
  110. 'packet_err' => 0, // 该进程收到错误数据包的总数
  111. 'throw_exception' => 0, // 该进程逻辑处理时收到异常的总数
  112. 'thunder_herd' => 0, // 该进程受惊群效应影响的总数
  113. 'client_close' => 0, // 客户端提前关闭链接总数
  114. 'send_fail' => 0, // 发送数据给客户端失败总数
  115. );
  116. /**
  117. * 用户worker继承此worker类必须实现该方法,根据具体协议和当前收到的数据决定是否继续收包
  118. * @param string $recv_str 收到的数据包
  119. * @return int/false 返回0表示接收完毕/>0表示还有多少字节没有接收到/false出错
  120. */
  121. abstract public function dealInput($recv_str);
  122. /**
  123. * 用户worker继承此worker类必须实现该方法,根据包中的数据处理逻辑
  124. * 逻辑处理
  125. * @param string $recv_str 收到的数据包
  126. * @return void
  127. */
  128. abstract public function dealProcess($recv_str);
  129. /**
  130. * 构造函数
  131. * @param int $port
  132. * @param string $ip
  133. * @param string $protocol
  134. * @return void
  135. */
  136. public function __construct($worker_name = '')
  137. {
  138. // worker name
  139. if(!empty($worker_name))
  140. {
  141. $this->workerName = $worker_name;
  142. }
  143. else
  144. {
  145. $this->workerName = get_class($this);
  146. }
  147. // 是否开启长连接
  148. $this->isPersistentConnection = (bool)Lib\Config::get( $worker_name . '.persistent_connection');
  149. // 最大请求数,如果没有配置则使用PHP_INT_MAX
  150. $this->maxRequests = (int)Lib\Config::get( $worker_name . '.max_requests');
  151. $this->maxRequests = $this->maxRequests <= 0 ? PHP_INT_MAX : $this->maxRequests;
  152. $preread_length = (int)Lib\Config::get( $worker_name . '.preread_length');
  153. if($preread_length > 0)
  154. {
  155. $this->prereadLength = $preread_length;
  156. }
  157. elseif(!$this->isPersistentConnection)
  158. {
  159. $this->prereadLength = 65535;
  160. }
  161. // worker启动时间
  162. $this->statusInfo['start_time'] = time();
  163. //事件轮询库
  164. if(extension_loaded('libevent'))
  165. {
  166. $this->setEventLoopName('Libevent');
  167. }
  168. // 检查退出状态
  169. $this->addShutdownHook();
  170. // 初始化事件轮询库
  171. // $this->event = new Libevent();
  172. // $this->event = new Select();
  173. $this->event = new $this->eventLoopName();
  174. }
  175. /**
  176. * 让该worker实例开始服务
  177. *
  178. * @return void
  179. */
  180. public function start()
  181. {
  182. // 安装信号处理函数
  183. $this->installSignal();
  184. // 触发该worker进程onStart事件,该进程整个生命周期只触发一次
  185. if($this->onStart())
  186. {
  187. return;
  188. }
  189. if($this->protocol == 'udp')
  190. {
  191. // 添加读udp事件
  192. $this->event->add($this->mainSocket, Events\BaseEvent::EV_READ, array($this, 'recvUdp'));
  193. }
  194. else
  195. {
  196. // 添加accept事件
  197. $ret = $this->event->add($this->mainSocket, Events\BaseEvent::EV_READ, array($this, 'accept'));
  198. }
  199. // 主体循环,整个子进程会阻塞在这个函数上
  200. $ret = $this->event->loop();
  201. $this->notice('worker loop exit');
  202. exit(0);
  203. }
  204. /**
  205. * 停止服务
  206. * @param bool $exit 是否退出
  207. * @return void
  208. */
  209. public function stop($exit = true)
  210. {
  211. // 触发该worker进程onStop事件
  212. if($this->onStop())
  213. {
  214. return;
  215. }
  216. // 标记这个worker开始停止服务
  217. if($this->workerStatus != self::STATUS_SHUTDOWN)
  218. {
  219. // 停止接收连接
  220. $this->event->del($this->mainSocket, Events\BaseEvent::EV_READ);
  221. fclose($this->mainSocket);
  222. $this->workerStatus = self::STATUS_SHUTDOWN;
  223. }
  224. // 没有链接要处理了
  225. if($this->allTaskHasDone())
  226. {
  227. if($exit)
  228. {
  229. exit(0);
  230. }
  231. }
  232. }
  233. /**
  234. * 设置worker监听的socket
  235. * @param resource $socket
  236. * @return void
  237. */
  238. public function setListendSocket($socket)
  239. {
  240. // 初始化
  241. $this->mainSocket = $socket;
  242. // 设置监听socket非阻塞
  243. stream_set_blocking($this->mainSocket, 0);
  244. // 获取协议
  245. $mata_data = stream_get_meta_data($socket);
  246. $this->protocol = substr($mata_data['stream_type'], 0, 3);
  247. }
  248. /**
  249. * 设置worker的事件轮询库的名称
  250. * @param string
  251. * @return void
  252. */
  253. public function setEventLoopName($event_loop_name)
  254. {
  255. $this->eventLoopName = "\\Man\\Core\\Events\\".$event_loop_name;
  256. require_once WORKERMAN_ROOT_DIR . 'man/Core/Events/'.ucfirst(str_replace('WORKERMAN', '', $event_loop_name)).'.php';
  257. }
  258. /**
  259. * 接受一个链接
  260. * @param resource $socket
  261. * @param $null_one $flag
  262. * @param $null_two $base
  263. * @return void
  264. */
  265. public function accept($socket, $null_one = null, $null_two = null)
  266. {
  267. // 获得一个连接
  268. $new_connection = @stream_socket_accept($socket, 0);
  269. // 可能是惊群效应
  270. if(false === $new_connection)
  271. {
  272. $this->statusInfo['thunder_herd']++;
  273. return false;
  274. }
  275. // 接受请求数加1
  276. $this->statusInfo['total_request'] ++;
  277. // 连接的fd序号
  278. $fd = (int) $new_connection;
  279. $this->connections[$fd] = $new_connection;
  280. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>$this->prereadLength);
  281. // 非阻塞
  282. stream_set_blocking($this->connections[$fd], 0);
  283. $this->event->add($this->connections[$fd], Events\BaseEvent::EV_READ , array($this, 'dealInputBase'), $fd);
  284. return $new_connection;
  285. }
  286. /**
  287. * 接收Udp数据
  288. * 如果数据超过一个udp包长,需要业务自己解析包体,判断数据是否全部到达
  289. * @param resource $socket
  290. * @param $null_one $flag
  291. * @param $null_two $base
  292. * @return void
  293. */
  294. public function recvUdp($socket, $null_one = null, $null_two = null)
  295. {
  296. $data = stream_socket_recvfrom($socket , self::MAX_UDP_PACKEG_SIZE, 0, $address);
  297. // 可能是惊群效应
  298. if(false === $data || empty($address))
  299. {
  300. $this->statusInfo['thunder_herd']++;
  301. return false;
  302. }
  303. // 接受请求数加1
  304. $this->statusInfo['total_request'] ++;
  305. $this->currentClientAddress = $address;
  306. if(0 === $this->dealInput($data))
  307. {
  308. $this->dealProcess($data);
  309. }
  310. }
  311. /**
  312. * 处理受到的数据
  313. * @param event_buffer $event_buffer
  314. * @param int $fd
  315. * @return void
  316. */
  317. public function dealInputBase($connection, $flag, $fd = null)
  318. {
  319. $this->currentDealFd = $fd;
  320. $buffer = stream_socket_recvfrom($connection, $this->recvBuffers[$fd]['remain_len']);
  321. // 出错了
  322. if('' == $buffer)
  323. {
  324. if(feof($connection))
  325. {
  326. // 客户端提前断开链接
  327. $this->statusInfo['client_close']++;
  328. // 如果该链接对应的buffer有数据,说明放生错误
  329. if(!empty($this->recvBuffers[$fd]['buf']))
  330. {
  331. $this->notice("CLIENT_CLOSE\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  332. }
  333. }
  334. else
  335. {
  336. // 超时了
  337. $this->statusInfo['recv_timeout']++;
  338. // 如果该链接对应的buffer有数据,说明放生错误
  339. if(!empty($this->recvBuffers[$fd]['buf']))
  340. {
  341. $this->notice("RECV_TIMEOUT\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  342. }
  343. }
  344. // 关闭链接
  345. $this->closeClient($fd);
  346. if($this->workerStatus == self::STATUS_SHUTDOWN)
  347. {
  348. $this->stop();
  349. }
  350. return;
  351. }
  352. $this->recvBuffers[$fd]['buf'] .= $buffer;
  353. $remain_len = $this->dealInput($this->recvBuffers[$fd]['buf']);
  354. // 包接收完毕
  355. if(0 === $remain_len)
  356. {
  357. // 执行处理
  358. try{
  359. // 业务处理
  360. $this->dealProcess($this->recvBuffers[$fd]['buf']);
  361. }
  362. catch(\Exception $e)
  363. {
  364. // 关闭闹钟
  365. //pcntl_alarm(0);
  366. $this->notice('CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage()."\n".$e->getTraceAsString()."\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  367. $this->statusInfo['throw_exception'] ++;
  368. $this->sendToClient($e->getMessage());
  369. }
  370. // 是否是长连接
  371. if($this->isPersistentConnection)
  372. {
  373. // 清空缓冲buffer
  374. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>$this->prereadLength);
  375. }
  376. else
  377. {
  378. // 关闭链接
  379. $this->closeClient($fd);
  380. }
  381. }
  382. // 出错
  383. else if(false === $remain_len)
  384. {
  385. // 出错
  386. $this->statusInfo['packet_err']++;
  387. $this->sendToClient('packet_err:'.$this->recvBuffers[$fd]['buf']);
  388. $this->notice("PACKET_ERROR\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  389. $this->closeClient($fd);
  390. }
  391. else
  392. {
  393. $this->recvBuffers[$fd]['remain_len'] = $remain_len;
  394. }
  395. // 检查是否是关闭状态或者是否到达请求上限
  396. if($this->workerStatus == self::STATUS_SHUTDOWN || $this->statusInfo['total_request'] >= $this->maxRequests)
  397. {
  398. // 关闭链接
  399. if($this->isPersistentConnection)
  400. {
  401. $this->closeClient($fd);
  402. }
  403. // 停止服务
  404. $this->stop();
  405. // EXIT_WAIT_TIME秒后退出进程
  406. pcntl_alarm(self::EXIT_WAIT_TIME);
  407. }
  408. }
  409. /**
  410. * 根据fd关闭链接
  411. * @param int $fd
  412. * @return void
  413. */
  414. protected function closeClient($fd)
  415. {
  416. // udp忽略
  417. if($this->protocol != 'udp')
  418. {
  419. $this->event->del($this->connections[$fd], Events\BaseEvent::EV_READ);
  420. fclose($this->connections[$fd]);
  421. unset($this->connections[$fd], $this->recvBuffers[$fd]);
  422. }
  423. }
  424. /**
  425. * 安装信号处理函数
  426. * @return void
  427. */
  428. protected function installSignal()
  429. {
  430. // 如果是由worker脚本启动则不安装信号
  431. if(!defined('WORKERMAN_PID_FILE'))
  432. {
  433. return;
  434. }
  435. // 闹钟信号
  436. $this->event->add(SIGALRM, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'), SIGALRM);
  437. // 终止进程信号
  438. $this->event->add(SIGINT, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'), SIGINT);
  439. // 平滑重启信号
  440. $this->event->add(SIGHUP, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'), SIGHUP);
  441. // 报告进程状态
  442. $this->event->add(SIGUSR1, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'), SIGUSR1);
  443. // 报告该进程使用的文件
  444. $this->event->add(SIGUSR2, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'), SIGUSR2);
  445. // 设置忽略信号
  446. pcntl_signal(SIGTTIN, SIG_IGN);
  447. pcntl_signal(SIGTTOU, SIG_IGN);
  448. pcntl_signal(SIGQUIT, SIG_IGN);
  449. pcntl_signal(SIGPIPE, SIG_IGN);
  450. pcntl_signal(SIGCHLD, SIG_IGN);
  451. }
  452. /**
  453. * 设置server信号处理函数
  454. * @param null $null
  455. * @param int $signal
  456. */
  457. public function signalHandler($signal, $null = null, $null = null)
  458. {
  459. switch($signal)
  460. {
  461. // 时钟处理函数
  462. case SIGALRM:
  463. // 停止服务后EXIT_WAIT_TIME秒还没退出则强制退出
  464. if($this->workerStatus == self::STATUS_SHUTDOWN)
  465. {
  466. exit(0);
  467. }
  468. break;
  469. // 停止该进程
  470. case SIGINT:
  471. // 平滑重启
  472. case SIGHUP:
  473. $this->stop();
  474. // EXIT_WAIT_TIME秒后退出进程
  475. pcntl_alarm(self::EXIT_WAIT_TIME);
  476. break;
  477. // 报告进程状态
  478. case SIGUSR1:
  479. $this->writeStatusToQueue();
  480. break;
  481. // 报告进程使用的php文件
  482. case SIGUSR2:
  483. $this->writeFilesListToQueue();
  484. break;
  485. }
  486. }
  487. /**
  488. * 发送数据到客户端
  489. * @return bool
  490. */
  491. public function sendToClient($str_to_send)
  492. {
  493. // tcp
  494. if($this->protocol != 'udp')
  495. {
  496. // tcp 如果一次没写完(一般是缓冲区满的情况),则阻塞写
  497. if(!$this->blockWrite($this->connections[$this->currentDealFd], $str_to_send, 500))
  498. {
  499. $this->notice('sendToClient fail ,Data length = ' . strlen($str_to_send));
  500. $this->statusInfo['send_fail']++;
  501. return false;
  502. }
  503. return true;
  504. }
  505. // udp 直接发送,要求数据包不能超过65515
  506. $len = stream_socket_sendto($this->mainSocket, $str_to_send, 0, $this->currentClientAddress);
  507. return $len == strlen($str_to_send);
  508. }
  509. /**
  510. * 向fd写数据,如果socket缓冲区满了,则改用阻塞模式写数据
  511. * @param resource $fd
  512. * @param string $str_to_write
  513. * @param int $time_out 单位毫秒
  514. * @return bool
  515. */
  516. protected function blockWrite($fd, $str_to_write, $timeout_ms = 500)
  517. {
  518. $send_len = @fwrite($fd, $str_to_write);
  519. if($send_len == strlen($str_to_write))
  520. {
  521. return true;
  522. }
  523. // 客户端关闭
  524. if(feof($fd))
  525. {
  526. $this->notice("blockWrite client close");
  527. return false;
  528. }
  529. // 设置阻塞
  530. stream_set_blocking($fd, 1);
  531. // 设置超时
  532. $timeout_sec = floor($timeout_ms/1000);
  533. $timeout_ms = $timeout_ms%1000;
  534. stream_set_timeout($fd, $timeout_sec, $timeout_ms*1000);
  535. $send_len += @fwrite($fd, substr($str_to_write, $send_len));
  536. // 改回非阻塞
  537. stream_set_blocking($fd, 0);
  538. return $send_len == strlen($str_to_write);
  539. }
  540. /**
  541. * 获取客户端ip
  542. * @param int $fd 已经链接的socket id
  543. * @return string
  544. */
  545. public function getRemoteIp($fd = null)
  546. {
  547. if(empty($fd))
  548. {
  549. if(!isset($this->connections[$this->currentDealFd]))
  550. {
  551. return '0.0.0.0';
  552. }
  553. $fd = $this->currentDealFd;
  554. }
  555. $ip = '';
  556. if($this->protocol == 'udp')
  557. {
  558. $sock_name = $this->currentClientAddress;
  559. }
  560. else
  561. {
  562. $sock_name = stream_socket_get_name($this->connections[$fd], true);
  563. }
  564. if($sock_name)
  565. {
  566. $tmp = explode(':', $sock_name);
  567. $ip = $tmp[0];
  568. }
  569. return $ip;
  570. }
  571. /**
  572. * 获取本地ip
  573. * @return string
  574. */
  575. public function getLocalIp()
  576. {
  577. $ip = '';
  578. $sock_name = '';
  579. if($this->protocol == 'udp' || !isset($this->connections[$this->currentDealFd]))
  580. {
  581. $sock_name = stream_socket_get_name($this->mainSocket, false);
  582. }
  583. else
  584. {
  585. $sock_name = stream_socket_get_name($this->connections[$this->currentDealFd], false);
  586. }
  587. if($sock_name)
  588. {
  589. $tmp = explode(':', $sock_name);
  590. $ip = $tmp[0];
  591. }
  592. if(empty($ip) || '127.0.0.1' == $ip)
  593. {
  594. $ip = gethostbyname(trim(`hostname`));
  595. }
  596. return $ip;
  597. }
  598. /**
  599. * 将当前worker进程状态写入消息队列
  600. * @return void
  601. */
  602. protected function writeStatusToQueue()
  603. {
  604. if(!Master::getQueueId())
  605. {
  606. return;
  607. }
  608. $error_code = 0;
  609. msg_send(Master::getQueueId(), self::MSG_TYPE_STATUS, array_merge($this->statusInfo, array('memory'=>memory_get_usage(true), 'pid'=>posix_getpid(), 'worker_name' => $this->workerName)), true, false, $error_code);
  610. }
  611. /**
  612. * 开发环境将当前进程使用的文件写入消息队列,用于FileMonitor监控文件更新
  613. * @return void
  614. */
  615. protected function writeFilesListToQueue()
  616. {
  617. if(!Master::getQueueId())
  618. {
  619. return;
  620. }
  621. $error_code = 0;
  622. $flip_file_list = array_flip(get_included_files());
  623. $file_list = array_diff_key($flip_file_list, $this->includeFiles);
  624. $this->includeFiles = $flip_file_list;
  625. if($file_list)
  626. {
  627. msg_send(Master::getQueueId(), self::MSG_TYPE_FILE_MONITOR, array_keys($file_list), true, false, $error_code);
  628. }
  629. }
  630. /**
  631. * 是否所有任务都已经完成
  632. * @return bool
  633. */
  634. protected function allTaskHasDone()
  635. {
  636. // 如果是长链接并且没有要处理的数据则是任务都处理完了
  637. return $this->noConnections() || ($this->isPersistentConnection && $this->allBufferIsEmpty());
  638. }
  639. /**
  640. * 检查是否所有的链接的缓冲区都是空
  641. * @return bool
  642. */
  643. protected function allBufferIsEmpty()
  644. {
  645. foreach($this->recvBuffers as $fd => $buf)
  646. {
  647. if(!empty($buf['buf']))
  648. {
  649. return false;
  650. }
  651. }
  652. return true;
  653. }
  654. /**
  655. * 该进程收到的任务是否都已经完成,重启进程时需要判断
  656. * @return bool
  657. */
  658. protected function noConnections()
  659. {
  660. return empty($this->connections);
  661. }
  662. /**
  663. * 该worker进程开始服务的时候会触发一次,可以在这里做一些全局的事情
  664. * @return bool
  665. */
  666. protected function onStart()
  667. {
  668. return false;
  669. }
  670. /**
  671. * 该worker进程停止服务的时候会触发一次,可以在这里做一些全局的事情
  672. * @return bool
  673. */
  674. protected function onStop()
  675. {
  676. return false;
  677. }
  678. }