You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http-server.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. include '../vendor/autoload.php';
  3. use Smf\ConnectionPool\ConnectionPool;
  4. use Smf\ConnectionPool\ConnectionPoolTrait;
  5. use Smf\ConnectionPool\Connectors\CoroutineMySQLConnector;
  6. use Smf\ConnectionPool\Connectors\PhpRedisConnector;
  7. use Swoole\Coroutine\MySQL;
  8. use Swoole\Http\Request;
  9. use Swoole\Http\Response;
  10. use Swoole\Http\Server;
  11. class HttpServer
  12. {
  13. use ConnectionPoolTrait;
  14. protected $swoole;
  15. public function __construct(string $host, int $port)
  16. {
  17. $this->swoole = new Server($host, $port);
  18. $this->setDefault();
  19. $this->bindWorkerEvents();
  20. $this->bindHttpEvent();
  21. }
  22. protected function setDefault()
  23. {
  24. $this->swoole->set([
  25. 'daemonize' => false,
  26. 'dispatch_mode' => 1,
  27. 'max_request' => 8000,
  28. 'open_tcp_nodelay' => true,
  29. 'reload_async' => true,
  30. 'max_wait_time' => 60,
  31. 'enable_reuse_port' => true,
  32. 'enable_coroutine' => true,
  33. 'http_compression' => false,
  34. 'enable_static_handler' => false,
  35. 'buffer_output_size' => 4 * 1024 * 1024,
  36. 'worker_num' => 4, // Each worker holds a connection pool
  37. ]);
  38. }
  39. protected function bindHttpEvent()
  40. {
  41. $this->swoole->on('Request', function (Request $request, Response $response) {
  42. $pool1 = $this->getConnectionPool('mysql');
  43. /**@var MySQL $mysql */
  44. $mysql = $pool1->borrow();
  45. $status = $mysql->query('SHOW STATUS LIKE "Threads_connected"');
  46. // Return the connection to pool as soon as possible
  47. $pool1->return($mysql);
  48. $pool2 = $this->getConnectionPool('redis');
  49. /**@var \Redis $redis */
  50. $redis = $pool2->borrow();
  51. $clients = $redis->info('Clients');
  52. // Return the connection to pool as soon as possible
  53. $pool2->return($redis);
  54. $json = [
  55. 'status' => $status,
  56. 'clients' => $clients,
  57. ];
  58. // Other logic
  59. // ...
  60. $response->header('Content-Type', 'application/json');
  61. $response->end(json_encode($json));
  62. });
  63. }
  64. protected function bindWorkerEvents()
  65. {
  66. $createPools = function () {
  67. // All MySQL connections: [4 workers * 2 = 8, 4 workers * 10 = 40]
  68. $pool1 = new ConnectionPool(
  69. [
  70. 'minActive' => 2,
  71. 'maxActive' => 10,
  72. ],
  73. new CoroutineMySQLConnector,
  74. [
  75. 'host' => '127.0.0.1',
  76. 'port' => '3306',
  77. 'user' => 'root',
  78. 'password' => 'xy123456',
  79. 'database' => 'mysql',
  80. 'timeout' => 10,
  81. 'charset' => 'utf8mb4',
  82. 'strict_type' => true,
  83. 'fetch_mode' => true,
  84. ]);
  85. $pool1->init();
  86. $this->addConnectionPool('mysql', $pool1);
  87. // All Redis connections: [4 workers * 5 = 20, 4 workers * 20 = 80]
  88. $pool2 = new ConnectionPool(
  89. [
  90. 'minActive' => 5,
  91. 'maxActive' => 20,
  92. ],
  93. new PhpRedisConnector,
  94. [
  95. 'host' => '127.0.0.1',
  96. 'port' => '6379',
  97. 'database' => 0,
  98. 'password' => null,
  99. ]);
  100. $pool2->init();
  101. $this->addConnectionPool('redis', $pool2);
  102. };
  103. $closePools = function () {
  104. $this->closeConnectionPools();
  105. };
  106. $this->swoole->on('WorkerStart', $createPools);
  107. $this->swoole->on('WorkerStop', $closePools);
  108. $this->swoole->on('WorkerError', $closePools);
  109. }
  110. public function start()
  111. {
  112. $this->swoole->start();
  113. }
  114. }
  115. // Enable coroutine for PhpRedis
  116. Swoole\Runtime::enableCoroutine();
  117. $server = new HttpServer('0.0.0.0', 5200);
  118. $server->start();