config.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. use Imi\App;
  3. use function Imi\env;
  4. return (function() {
  5. $mode = App::isInited() ? App::getApp()->getType() : '';
  6. $isMysql = ('mysql' === strtolower(env('TFB_TEST_DATABASE', 'mysql')));
  7. return [
  8. // 项目根命名空间
  9. 'namespace' => 'ImiApp',
  10. // 配置文件
  11. 'configs' => [
  12. 'beans' => __DIR__ . '/beans.php',
  13. ],
  14. // 组件命名空间
  15. 'components' => [],
  16. // 主服务器配置
  17. 'mainServer' => 'swoole' === $mode ? [
  18. 'namespace' => 'ImiApp\ApiServer',
  19. 'type' => Imi\Swoole\Server\Type::HTTP,
  20. 'host' => '0.0.0.0',
  21. 'port' => 8080,
  22. 'mode' => SWOOLE_BASE,
  23. 'configs' => [
  24. 'worker_num' => swoole_cpu_num(),
  25. 'open_tcp_nodelay' => true,
  26. 'tcp_fastopen' => true,
  27. 'http_parse_post' => false,
  28. 'http_parse_cookie' => false,
  29. 'http_parse_files' => false,
  30. 'http_compression' => false,
  31. ],
  32. ] : [],
  33. // Workerman 服务器配置
  34. 'workermanServer' => 'workerman' === $mode ? [
  35. // 服务器名,http 也可以改成 abc 等等,完全自定义
  36. 'http' => [
  37. // 指定服务器命名空间
  38. 'namespace' => 'ImiApp\ApiServer',
  39. // 服务器类型
  40. 'type' => Imi\Workerman\Server\Type::HTTP, // HTTP、WEBSOCKET、TCP、UDP
  41. 'host' => '0.0.0.0',
  42. 'port' => 8080,
  43. // socket的上下文选项,参考:http://doc3.workerman.net/315128
  44. 'context' => [],
  45. 'configs' => [
  46. // 支持设置 Workerman 参数
  47. 'count' => (int) shell_exec('nproc') * 4,
  48. ],
  49. ],
  50. ] : [],
  51. 'db' => [
  52. 'defaultPool' => $isMysql ? 'mysql' : 'pgsql', // 默认连接池
  53. 'connections' => [
  54. 'mysql' => [
  55. 'host' => env('DB_HOST', 'tfb-database'),
  56. 'username' => env('DB_USERNAME', 'benchmarkdbuser'),
  57. 'password' => env('DB_PASSWORD', 'benchmarkdbpass'),
  58. 'database' => 'hello_world',
  59. 'dbClass' => \Imi\Db\Mysql\Drivers\Mysqli\Driver::class,
  60. 'checkStateWhenGetResource' => false,
  61. ],
  62. 'pgsql' => [
  63. 'host' => env('DB_HOST', 'tfb-database'),
  64. 'username' => env('DB_USERNAME', 'benchmarkdbuser'),
  65. 'password' => env('DB_PASSWORD', 'benchmarkdbpass'),
  66. 'database' => 'hello_world',
  67. 'dbClass' => \Imi\Pgsql\Db\Drivers\PdoPgsql\Driver::class,
  68. 'checkStateWhenGetResource' => false,
  69. ],
  70. ],
  71. ],
  72. 'pools' => 'swoole' === $mode ? [
  73. // 连接池名称
  74. 'mysql' => [
  75. 'pool' => [
  76. 'class' => \Imi\Swoole\Db\Pool\CoroutineDbPool::class,
  77. 'config' => [
  78. // 池子中最多资源数
  79. 'maxResources' => intval(1024 / swoole_cpu_num()),
  80. // 池子中最少资源数
  81. 'minResources' => $isMysql ? 16 : 0,
  82. 'gcInterval' => 0,
  83. 'checkStateWhenGetResource' => false,
  84. ],
  85. ],
  86. // resource也可以定义多个连接
  87. 'resource' => [
  88. 'host' => env('DB_HOST', 'tfb-database'),
  89. 'username' => env('DB_USERNAME', 'benchmarkdbuser'),
  90. 'password' => env('DB_PASSWORD', 'benchmarkdbpass'),
  91. 'database' => 'hello_world',
  92. 'dbClass' => \Imi\Db\Mysql\Drivers\Mysqli\Driver::class,
  93. ],
  94. ],
  95. 'pgsql' => [
  96. 'pool' => [
  97. 'class' => \Imi\Swoole\Db\Pool\CoroutineDbPool::class,
  98. 'config' => [
  99. // 池子中最多资源数
  100. 'maxResources' => intval(1024 / swoole_cpu_num()),
  101. // 池子中最少资源数
  102. 'minResources' => $isMysql ? 0 : 16,
  103. 'checkStateWhenGetResource' => false,
  104. ],
  105. ],
  106. // resource也可以定义多个连接
  107. 'resource' => [
  108. 'host' => env('DB_HOST', 'tfb-database'),
  109. 'username' => env('DB_USERNAME', 'benchmarkdbuser'),
  110. 'password' => env('DB_PASSWORD', 'benchmarkdbpass'),
  111. 'database' => 'hello_world',
  112. 'dbClass' => \Imi\Pgsql\Db\Drivers\PdoPgsql\Driver::class,
  113. ],
  114. ],
  115. ] : [],
  116. ];
  117. })();