SocketWorker.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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->notice("CLIENT_CLOSE\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  315. }
  316. // 关闭链接
  317. $this->closeClient($fd);
  318. if($this->workerStatus == self::STATUS_SHUTDOWN)
  319. {
  320. $this->stop();
  321. }
  322. return;
  323. }
  324. $this->recvBuffers[$fd]['buf'] .= $buffer;
  325. $remain_len = $this->dealInput($this->recvBuffers[$fd]['buf']);
  326. // 包接收完毕
  327. if(0 === $remain_len)
  328. {
  329. // 执行处理
  330. try{
  331. // 业务处理
  332. $this->dealProcess($this->recvBuffers[$fd]['buf']);
  333. }
  334. catch(\Exception $e)
  335. {
  336. $this->notice('CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage()."\n".$e->getTraceAsString()."\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  337. $this->statusInfo['throw_exception'] ++;
  338. $this->sendToClient($e);
  339. }
  340. // 是否是长连接
  341. if($this->isPersistentConnection)
  342. {
  343. // 清空缓冲buffer
  344. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>$this->prereadLength);
  345. }
  346. else
  347. {
  348. // 关闭链接
  349. if(empty($this->sendBuffers[$fd]))
  350. {
  351. $this->closeClient($fd);
  352. }
  353. }
  354. }
  355. // 出错
  356. else if(false === $remain_len)
  357. {
  358. // 出错
  359. $this->statusInfo['packet_err']++;
  360. $this->sendToClient('packet_err:'.$this->recvBuffers[$fd]['buf']);
  361. $this->notice("PACKET_ERROR\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  362. $this->closeClient($fd);
  363. }
  364. else
  365. {
  366. $this->recvBuffers[$fd]['remain_len'] = $remain_len;
  367. }
  368. // 检查是否是关闭状态或者是否到达请求上限
  369. if($this->workerStatus == self::STATUS_SHUTDOWN || $this->statusInfo['total_request'] >= $this->maxRequests)
  370. {
  371. // 停止服务
  372. $this->stop();
  373. // EXIT_WAIT_TIME秒后退出进程
  374. pcntl_alarm(self::EXIT_WAIT_TIME);
  375. }
  376. }
  377. /**
  378. * 根据fd关闭链接
  379. * @param int $fd
  380. * @return void
  381. */
  382. protected function closeClient($fd)
  383. {
  384. // udp忽略
  385. if($this->protocol != 'udp')
  386. {
  387. $this->event->del($this->connections[$fd], Events\BaseEvent::EV_READ);
  388. $this->event->del($this->connections[$fd], Events\BaseEvent::EV_WRITE);
  389. fclose($this->connections[$fd]);
  390. unset($this->connections[$fd], $this->recvBuffers[$fd], $this->sendBuffers[$fd]);
  391. }
  392. }
  393. /**
  394. * 安装信号处理函数
  395. * @return void
  396. */
  397. protected function installSignal()
  398. {
  399. // 闹钟信号
  400. $this->event->add(SIGALRM, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'));
  401. // 终止进程信号
  402. $this->event->add(SIGINT, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'));
  403. // 平滑重启信号
  404. $this->event->add(SIGHUP, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'));
  405. // 报告进程状态
  406. $this->event->add(SIGUSR1, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'));
  407. // 报告该进程使用的文件
  408. $this->event->add(SIGUSR2, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'));
  409. // 设置忽略信号
  410. pcntl_signal(SIGTTIN, SIG_IGN);
  411. pcntl_signal(SIGTTOU, SIG_IGN);
  412. pcntl_signal(SIGQUIT, SIG_IGN);
  413. pcntl_signal(SIGPIPE, SIG_IGN);
  414. pcntl_signal(SIGCHLD, SIG_IGN);
  415. }
  416. /**
  417. * 设置server信号处理函数
  418. * @param null $null
  419. * @param int $signal
  420. */
  421. public function signalHandler($signal, $null = null, $null = null)
  422. {
  423. switch($signal)
  424. {
  425. // 时钟处理函数
  426. case SIGALRM:
  427. // 停止服务后EXIT_WAIT_TIME秒还没退出则强制退出
  428. if($this->workerStatus == self::STATUS_SHUTDOWN)
  429. {
  430. exit(0);
  431. }
  432. break;
  433. // 停止该进程
  434. case SIGINT:
  435. // 平滑重启
  436. case SIGHUP:
  437. $this->stop();
  438. // EXIT_WAIT_TIME秒后退出进程
  439. pcntl_alarm(self::EXIT_WAIT_TIME);
  440. break;
  441. // 报告进程状态
  442. case SIGUSR1:
  443. $this->writeStatusToQueue();
  444. break;
  445. // 报告进程使用的php文件
  446. case SIGUSR2:
  447. $this->writeFilesListToQueue();
  448. break;
  449. }
  450. }
  451. /**
  452. * 发送数据到客户端
  453. * @return bool
  454. */
  455. public function sendToClient($str_to_send)
  456. {
  457. // tcp
  458. if($this->protocol != 'udp')
  459. {
  460. $send_len = @stream_socket_sendto($this->connections[$this->currentDealFd], $str_to_send);
  461. if($send_len === strlen($str_to_send))
  462. {
  463. return true;
  464. }
  465. if($send_len > 0)
  466. {
  467. $this->sendBuffers[$this->currentDealFd] = substr($str_to_send, $send_len);
  468. }
  469. else
  470. {
  471. $this->sendBuffers[$this->currentDealFd] = $str_to_send;
  472. }
  473. if(feof($this->sendBuffers[$this->currentDealFd]))
  474. {
  475. return false;
  476. }
  477. $this->event->add($this->connections[$this->currentDealFd], Events\BaseEvent::EV_WRITE, array($this, 'tcpWriteToClient'), array($this->currentDealFd));
  478. return null;
  479. }
  480. // udp 直接发送,要求数据包不能超过65515
  481. return strlen($str_to_send) == stream_socket_sendto($this->mainSocket, $str_to_send, 0, $this->currentClientAddress);
  482. }
  483. /**
  484. * 向客户端socket写数据
  485. * @param int $fd
  486. * @param string $bin_data
  487. */
  488. public function tcpWriteToClient($fd)
  489. {
  490. if(empty($this->connections[$fd]))
  491. {
  492. $this->notice("tcpWriteToClient \$this->connections[$fd] is null");
  493. return false;
  494. }
  495. $send_len = @stream_socket_sendto($this->connections[$fd], $this->sendBuffers[$fd]);
  496. if($send_len === strlen($this->sendBuffers[$fd]))
  497. {
  498. if(!$this->isPersistentConnection)
  499. {
  500. return $this->closeClient($fd);
  501. }
  502. $this->event->del($this->connections[$fd], BaseEvent::EV_WRITE);
  503. return;
  504. }
  505. if($send_len > 0)
  506. {
  507. $this->sendBuffers[$fd] = substr($this->sendBuffers[$fd], $send_len);
  508. }
  509. }
  510. /**
  511. * 获取客户端地址
  512. * @param int $fd 已经链接的socket id
  513. * @return string ip:port
  514. */
  515. public function getRemoteAddress($fd = null)
  516. {
  517. if(empty($fd) && $this->protocol !== 'udp')
  518. {
  519. if(!isset($this->connections[$this->currentDealFd]))
  520. {
  521. return '';
  522. }
  523. $fd = $this->currentDealFd;
  524. }
  525. if($this->protocol == 'udp')
  526. {
  527. $sock_name = $this->currentClientAddress;
  528. }
  529. else
  530. {
  531. $sock_name = stream_socket_get_name($this->connections[$fd], true);
  532. }
  533. return $sock_name;
  534. }
  535. /**
  536. * 获取客户端ip
  537. * @param integer $fd 已经链接的socket id
  538. * @return string
  539. */
  540. public function getRemoteIp($fd = null)
  541. {
  542. $ip = '';
  543. $address= $this->getRemoteAddress($fd);
  544. if($address)
  545. {
  546. list($ip, $port) = explode(':', $address, 2);
  547. }
  548. return $ip;
  549. }
  550. /**
  551. * 获取客户端ip
  552. * @param integer $fd 已经链接的socket id
  553. * @return integer
  554. */
  555. public function getRemotePort($fd = null)
  556. {
  557. $port = 0;
  558. $address= $this->getRemoteAddress($fd);
  559. if($address)
  560. {
  561. list($ip, $port) = explode(':', $address, 2);
  562. }
  563. return $port;
  564. }
  565. /**
  566. * 获取本地ip
  567. * @return string
  568. */
  569. public function getLocalIp()
  570. {
  571. $ip = '';
  572. $sock_name = '';
  573. if($this->protocol == 'udp' || !isset($this->connections[$this->currentDealFd]))
  574. {
  575. $sock_name = stream_socket_get_name($this->mainSocket, false);
  576. }
  577. else
  578. {
  579. $sock_name = stream_socket_get_name($this->connections[$this->currentDealFd], false);
  580. }
  581. if($sock_name)
  582. {
  583. $tmp = explode(':', $sock_name);
  584. $ip = $tmp[0];
  585. }
  586. if(empty($ip) || '127.0.0.1' == $ip)
  587. {
  588. $ip = gethostbyname(trim(`hostname`));
  589. }
  590. return $ip;
  591. }
  592. /**
  593. * 将当前worker进程状态写入消息队列
  594. * @return void
  595. */
  596. protected function writeStatusToQueue()
  597. {
  598. if(!Master::getQueueId())
  599. {
  600. return;
  601. }
  602. $error_code = 0;
  603. 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);
  604. }
  605. /**
  606. * 开发环境将当前进程使用的文件写入消息队列,用于FileMonitor监控文件更新
  607. * @return void
  608. */
  609. protected function writeFilesListToQueue()
  610. {
  611. if(!Master::getQueueId())
  612. {
  613. return;
  614. }
  615. $error_code = 0;
  616. $flip_file_list = array_flip(get_included_files());
  617. $file_list = array_diff_key($flip_file_list, $this->includeFiles);
  618. $this->includeFiles = $flip_file_list;
  619. if($file_list)
  620. {
  621. foreach(array_chunk($file_list, 10, true) as $list)
  622. {
  623. msg_send(Master::getQueueId(), self::MSG_TYPE_FILE_MONITOR, array_keys($list), true, false, $error_code);
  624. }
  625. }
  626. }
  627. /**
  628. * 是否所有任务都已经完成
  629. * @return bool
  630. */
  631. protected function allTaskHasDone()
  632. {
  633. // 如果是长链接并且没有要处理的数据则是任务都处理完了
  634. return $this->noConnections() || ($this->isPersistentConnection && $this->allBufferIsEmpty());
  635. }
  636. /**
  637. * 检查是否所有的链接的缓冲区都是空
  638. * @return bool
  639. */
  640. protected function allBufferIsEmpty()
  641. {
  642. foreach($this->recvBuffers as $fd => $buf)
  643. {
  644. if(!empty($buf['buf']))
  645. {
  646. return false;
  647. }
  648. }
  649. return true;
  650. }
  651. /**
  652. * 该进程收到的任务是否都已经完成,重启进程时需要判断
  653. * @return bool
  654. */
  655. protected function noConnections()
  656. {
  657. return empty($this->connections);
  658. }
  659. /**
  660. * 该worker进程开始服务的时候会触发一次,可以在这里做一些全局的事情
  661. * @return bool
  662. */
  663. protected function onStart()
  664. {
  665. return false;
  666. }
  667. /**
  668. * 该worker进程停止服务的时候会触发一次,可以在这里做一些全局的事情
  669. * @return bool
  670. */
  671. protected function onStop()
  672. {
  673. return false;
  674. }
  675. }