http.conf.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. use Workerman\Connection\TcpConnection;
  3. TcpConnection::$defaultMaxPackageSize = 10485760;
  4. return [
  5. 'name' => 'chidian', //服务名
  6. 'ip' => '0.0.0.0', //监听地址
  7. 'port' => 6504, //监听地址
  8. 'init_php' => __DIR__.'/base.php', //初始文件
  9. 'php_run' => function($req, $connection = null) {
  10. // 心跳及空闲示例
  11. $lifetimeTimer = \HttpForPHP\WorkerLifetimeTimer::instance(3, 15);
  12. $lifetimeTimer->onHeartbeat = function () { //间隔数据库连接检测
  13. \Yii::$app->db->createCommand('select 1')->queryOne();
  14. };
  15. $lifetimeTimer->onIdle = function () { //空闲释放
  16. \Yii::$app->db->close();
  17. };
  18. $lifetimeTimer->run();
  19. // 处理插件路由
  20. $getParams = $req->get();
  21. $route = isset($getParams['r']) ? $getParams['r'] : null;
  22. if ($route && strpos($route, 'plugin') !== false) {
  23. if (preg_match('/client\/v(\d+)\/plugin\/(.*)/', $route, $matches)) {
  24. // 客户端插件
  25. $_GET['r'] = sprintf('client/v%d/plugin/callback', $matches[1]);
  26. $_GET['callback'] = $matches[2];
  27. } else {
  28. // 后台插件
  29. $callback = str_replace('admin/plugins/', '', $route);
  30. $_GET['r'] = 'admin/plugins/callback';
  31. $_GET['callback'] = $callback;
  32. }
  33. }
  34. ob_start();
  35. $app = Yii::$app;
  36. // 设置请求头
  37. $headers = \HttpForPHP\SrvBase::$instance->getHeader($req);
  38. foreach ($headers as $name=>$value){
  39. $app->request->headers->set($name, $value);
  40. }
  41. // 设置请求参数
  42. $app->request->setHostInfo($req->header('Chidian-Host', null));
  43. $app->request->setBaseUrl(null);
  44. $app->request->setScriptUrl(null);
  45. $app->request->setPathInfo(null);
  46. $app->request->setUrl(null);
  47. $app->request->setQueryParams($_GET);
  48. $app->request->setBodyParams($_POST);
  49. $app->request->setRawBody(\HttpForPHP\SrvBase::$instance->getRawBody($req));
  50. try{
  51. $checkCache = cache()->exists('wokerman_update');
  52. if ($checkCache) {
  53. posix_kill(posix_getppid(), SIGUSR1);
  54. cache()->delete('wokerman_update');
  55. }
  56. $app->checkAuthByWokerman($req);
  57. $app->run();
  58. } catch (\Exception $e) {
  59. $msg = $e->getMessage();
  60. $failReloadMsg = [
  61. 'MySQL server has gone away',
  62. 'Failed to write to socket', #Failed to write to socket. 0 of 34 bytes written.
  63. 'Failed to read from socket',
  64. 'Error while sending QUERY',
  65. 'read error on connection to', #read error on connection to 192.168.0.186:6379
  66. ];
  67. #因m异常断开重启进程
  68. foreach ($failReloadMsg as $fail) {
  69. if (strpos($msg, $fail) !== false) {
  70. \HttpForPHP\Log::write(sprintf('line:%s, file:%s, err:%s, trace:%s',$e->getLine(), $e->getFile(), $e->getMessage(), $e->getTraceAsString()), 'err');
  71. \HttpForPHP\SrvBase::$instance->stopWorker();
  72. break;
  73. }
  74. }
  75. if ($msg != 'Page not found.' && $msg != '页面未找到。') {
  76. \HttpForPHP\Log::write(sprintf('line:%s, file:%s, err:%s, trace:%s',$e->getLine(), $e->getFile(), $e->getMessage(), $e->getTraceAsString()), 'err');
  77. }
  78. echo \HttpForPHP\Helper::toJson(\HttpForPHP\Helper::fail($e->getCode().':'.$msg));
  79. }
  80. $content = ob_get_clean();
  81. // 常驻服务需要清除信息
  82. $app->request->getHeaders()->removeAll();
  83. $app->response->clear();
  84. if ($connection === null) { //是异步任务
  85. //\HttpForPHP\Log::write($content, 'task');
  86. } else {
  87. $code = \Yii::$app->response->getStatusCode();
  88. $header = ['Access-Control-Allow-Origin' => '*']; //'Content-Type'=>'application/json; charset=utf-8',
  89. if (\Yii::$app->response->headers->count()) { //header头处理
  90. \Yii::$app->response->headers->set('Access-Control-Allow-Origin', '*');
  91. foreach (\Yii::$app->response->headers as $name => $values) {
  92. $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
  93. $header[$name] = end($values);
  94. }
  95. unset($header['Link']);
  96. } else {
  97. $header['Content-Type'] = 'application/json; charset=utf-8';
  98. }
  99. $header['X-Req'] = 'chidian';
  100. // 发送http
  101. \HttpForPHP\SrvBase::$instance->httpSend($connection, $code, $header, $content);
  102. }
  103. unset($content);
  104. return true;
  105. }, //自定义php运行函数
  106. 'setting' => [
  107. 'count' => cpu_count() * 4, // 异步非阻塞CPU核数的1-4倍最合理 同步阻塞按实际情况来填写 如50-100
  108. #'task_worker_num' => 10, //异步任务进程数 配置了异步处理才能生效 异步处理耗时过多的会阻塞后续的异步处理请求
  109. #'max_request' => 500, //最大请求数 默认0 进程内达到此请求重启进程 可能存在不规范的代码造成内存泄露 这里达到一定请求释放下内存
  110. 'stdoutFile' => __DIR__ . '/wokerman/http.log', //终端输出
  111. 'pidFile' => __DIR__ . '/wokerman/http.pid',
  112. 'logFile' => __DIR__ . '/wokerman/http.log', //日志文件
  113. # 'user' => 'www', //设置worker/task子进程的进程用户 提升服务器程序的安全性
  114. ]
  115. ];