连接池

3.1 版本开始,加入 Swoole Server 连接池,在 Swoole Server中,回调 workerStart 自动启动连接池。

连接池实现

在 Server onWokerStart 回调中,程序会调用 app() 函数递归所有服务,若服务实现自 FastD\Pool\PoolInterface 接口,那么在 Server 启动的时候,就会自动调用 initPool 方法,在该方法下执行连接,连接后除非 Worker 中断,否则会一直连接。

  1. <?php
  2. namespace FastD\Pool;
  3. use Medoo\Medoo;
  4. use FastD\Model\Database;
  5. class DatabasePool implements PoolInterface
  6. {
  7. /**
  8. * @var Medoo[]
  9. */
  10. protected $connections = [];
  11. /**
  12. * @var array
  13. */
  14. protected $config;
  15. /**
  16. * Database constructor.
  17. *
  18. * @param array $config
  19. */
  20. public function __construct(array $config)
  21. {
  22. $this->config = $config;
  23. }
  24. /**
  25. * @param $key
  26. *
  27. * @return Database
  28. */
  29. public function getConnection($key)
  30. {
  31. if (!isset($this->connections[$key])) {
  32. $config = $this->config[$key];
  33. $this->connections[$key] = new Database([
  34. 'database_type' => isset($config['adapter']) ? $config['adapter'] : 'mysql',
  35. 'database_name' => $config['name'],
  36. 'server' => $config['host'],
  37. 'username' => $config['user'],
  38. 'password' => $config['pass'],
  39. 'charset' => isset($config['charset']) ? $config['charset'] : 'utf8',
  40. 'port' => isset($config['port']) ? $config['port'] : 3306,
  41. 'prefix' => isset($config['prefix']) ? $config['prefix'] : '',
  42. ]);
  43. }
  44. return $this->connections[$key];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function initPool()
  50. {
  51. foreach ($this->config as $name => $config) {
  52. $this->getConnection($name);
  53. }
  54. }
  55. }

然后通过 ServiceProvider 注入到容器中, 可参考ServiceProvider

启动服务器,就会连接到每个 worker 当中,要注意的是,所有每个 worker 都是独立的,也就是如果开 20 个 worker,就会产生 20 个连接。

  1. Server: fast-d
  2. App version 2.0.0 (dev)
  3. Swoole version 1.9.5
  4. PID file: /Users/janhuang/Documents/htdocs/me/fastd/fastd/tests/config/../runtime/pid/fast-d.pid, PID: 23683
  5. Server udp://0.0.0.0:9527
  6. Server Master[23683] is started
  7. Server Worker[23695] is started [15]
  8. Server Worker[23690] is started [10]
  9. Server Worker[23691] is started [11]
  10. Server Worker[23692] is started [12]
  11. Server Worker[23693] is started [13]
  12. Server Worker[23694] is started [14]
  13. Server Worker[23696] is started [16]
  14. Server Worker[23697] is started [17]
  15. Server Worker[23698] is started [18]
  16. Server Worker[23699] is started [19]
  17. Server Worker[23700] is started [20]
  18. Server Worker[23701] is started [21]
  19. Server Worker[23702] is started [22]
  20. Server Worker[23703] is started [23]
  21. Server Worker[23704] is started [24]
  22. Server Worker[23705] is started [25]
  23. Server Worker[23706] is started [26]
  24. Server Worker[23707] is started [27]
  25. Server Worker[23708] is started [28]
  26. Server Worker[23709] is started [29]
  27. Server Worker[23710] is started [0]
  28. Server Worker[23711] is started [1]
  29. Server Worker[23712] is started [2]
  30. Server Worker[23713] is started [3]
  31. Server Worker[23714] is started [4]
  32. Server Worker[23715] is started [5]
  33. Server Worker[23716] is started [6]
  34. Server Worker[23717] is started [7]
  35. Server Worker[23718] is started [8]
  36. Server Manager[23689] is started
  37. Server Worker[23719] is started [9]