Pool.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace think\swoole;
  3. use Smf\ConnectionPool\ConnectionPool;
  4. use think\helper\Arr;
  5. class Pool
  6. {
  7. protected $pools = [];
  8. /**
  9. * @param string $name
  10. * @param ConnectionPool $pool
  11. *
  12. * @return Pool
  13. */
  14. public function add(string $name, ConnectionPool $pool)
  15. {
  16. $pool->init();
  17. $this->pools[$name] = $pool;
  18. return $this;
  19. }
  20. /**
  21. * @param string $name
  22. *
  23. * @return ConnectionPool
  24. */
  25. public function get(string $name)
  26. {
  27. return $this->pools[$name] ?? null;
  28. }
  29. public function close(string $key)
  30. {
  31. return $this->pools[$key]->close();
  32. }
  33. /**
  34. * @return array
  35. */
  36. public function getAll()
  37. {
  38. return $this->pools;
  39. }
  40. public function closeAll()
  41. {
  42. foreach ($this->pools as $pool) {
  43. $pool->close();
  44. }
  45. }
  46. /**
  47. * @param string $key
  48. *
  49. * @return ConnectionPool
  50. */
  51. public function __get($key)
  52. {
  53. return $this->get($key);
  54. }
  55. public static function pullPoolConfig(&$config)
  56. {
  57. return [
  58. 'minActive' => Arr::pull($config, 'min_active', 0),
  59. 'maxActive' => Arr::pull($config, 'max_active', 10),
  60. 'maxWaitTime' => Arr::pull($config, 'max_wait_time', 5),
  61. 'maxIdleTime' => Arr::pull($config, 'max_idle_time', 20),
  62. 'idleCheckInterval' => Arr::pull($config, 'idle_check_interval', 10),
  63. ];
  64. }
  65. }