processTimeout

说明:

  1. int BusinessWorker::$processTimeout

(需要GatewayWorker版本>=2.0.2)

设置监控服务端业务超时时间(单位秒)。不设置默认是30秒,设置为0表示不监控。

此属性用于监控Events::onConnect Events::onMessage Events::onClose的执行时间,单次执行时间如果超过BusinessWorker::$processTimeout设定的值,则记录一条日志到workerman.log,日志中包含超时的调用栈,这对于排查业务长时间阻塞以及死循环等问题非常有帮助。

注意:

1、需要GatewayWorker版本>=2.0.2,如何查看版本号参考《常见问题》一章

2、不支持windows系统

3、此属性需要在Events.php文件头部设置一条语句declare(ticks=1);才能生效,见下面示例。

范例

start_businessworker.php

  1. use \Workerman\Worker;
  2. use \GatewayWorker\BusinessWorker;
  3. $worker = new BusinessWorker();
  4. $worker->name = 'ChatBusinessWorker';
  5. $worker->count = 4;
  6. $worker->registerAddress = '127.0.0.1:1236';
  7. // 设置业务超时时间10秒,需要配合业务
  8. $worker->processTimeout = 10;
  9. if(!defined('GLOBAL_START'))
  10. {
  11. Worker::runAll();
  12. }

Events.php

需要在文件头部增加declare(ticks=1);语句,添加后运行php start.php reload即可生效。

  1. <?php
  2. /**
  3. * BusinessWorker::$processTimeout 属性需要配合declare(ticks=1)才能生效
  4. */
  5. declare(ticks=1);
  6. use \GatewayWorker\Lib\Gateway;
  7. class Events
  8. {
  9. public static function onMessage($client_id, $message)
  10. {
  11. self::testTimeout();
  12. }
  13. protected static function testTimeout()
  14. {
  15. // 故意放一个死循环
  16. while(1)
  17. {
  18. sleep(1);
  19. }
  20. }
  21. }

以上应用运行后,会在GatewayWorker/workerman.log(设置Worker::$logFile可以更改日志路径)里面产生业务超时日志,日志内容类似如下。

  1. process_timeout:
  2. #1 /home/GatewayWorker/Applications/YourApp/Event.php(14): Events::testTimeout()
  3. #2 [internal function]: Events::onMessage('7f0000010b54000...', 'test msg')
  4. #3 /home/GatewayWorker/GatewayWorker/BusinessWorker.php(264): call_user_func('Event::onMessag...', '7f0000010b54000...', 'test msg')
  5. #4 [internal function]: GatewayWorker\BusinessWorker->onGatewayMessage(Object(Workerman\Connection\AsyncTcpConnection), Array)
  6. #5 /home/GatewayWorker/Workerman/Connection/TcpConnection.php(453): call_user_func(Array, Object(Workerman\Connection\AsyncTcpConnection), Array)
  7. #6 [internal function]: Workerman\Connection\TcpConnection->baseRead(Resource id #49, 2, NULL)
  8. #7 /home/GatewayWorker/Workerman/Events/Libevent.php(221): event_base_loop(Resource id #32)
  9. #8 /home/GatewayWorker/Workerman/Worker.php(1507): Workerman\Events\Libevent->loop()
  10. #9 /home/GatewayWorker/GatewayWorker/BusinessWorker.php(121): Workerman\Worker->run()
  11. #10 /home/GatewayWorker/Workerman/Worker.php(900): GatewayWorker\BusinessWorker->run()
  12. #11 /home/GatewayWorker/Workerman/Worker.php(860): Workerman\Worker::forkOneWorker(Object(GatewayWorker\BusinessWorker))
  13. #12 /home/GatewayWorker/Workerman/Worker.php(387): Workerman\Worker::forkWorkers()
  14. #13 /home/GatewayWorker/start.php(32): Workerman\Worker::runAll()
  15. #14 {main}

从上面日志中可以看到业务代码在/home/GatewayWorker/Applications/YourApp/Events.php的第14行运行Event::testTimeout()时超时