SocketWorker.php 22 KB

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