AbstractConnectionPool.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Elasticsearch\ConnectionPool;
  3. use Elasticsearch\Common\Exceptions\InvalidArgumentException;
  4. use Elasticsearch\ConnectionPool\Selectors\SelectorInterface;
  5. use Elasticsearch\Connections\Connection;
  6. use Elasticsearch\Connections\ConnectionFactoryInterface;
  7. use Elasticsearch\Connections\ConnectionInterface;
  8. /**
  9. * Class AbstractConnectionPool
  10. *
  11. * @category Elasticsearch
  12. * @package Elasticsearch\ConnectionPool
  13. * @author Zachary Tong <zach@elastic.co>
  14. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  15. * @link http://elastic.co
  16. */
  17. abstract class AbstractConnectionPool implements ConnectionPoolInterface
  18. {
  19. /**
  20. * Array of connections
  21. *
  22. * @var ConnectionInterface[]
  23. */
  24. protected $connections;
  25. /**
  26. * Array of initial seed connections
  27. *
  28. * @var ConnectionInterface[]
  29. */
  30. protected $seedConnections;
  31. /**
  32. * Selector object, used to select a connection on each request
  33. *
  34. * @var SelectorInterface
  35. */
  36. protected $selector;
  37. /** @var array */
  38. protected $connectionPoolParams;
  39. /** @var \Elasticsearch\Connections\ConnectionFactory */
  40. protected $connectionFactory;
  41. /**
  42. * Constructor
  43. *
  44. * @param ConnectionInterface[] $connections The Connections to choose from
  45. * @param SelectorInterface $selector A Selector instance to perform the selection logic for the available connections
  46. * @param ConnectionFactoryInterface $factory ConnectionFactory instance
  47. * @param array $connectionPoolParams
  48. */
  49. public function __construct($connections, SelectorInterface $selector, ConnectionFactoryInterface $factory, $connectionPoolParams)
  50. {
  51. $paramList = array('connections', 'selector', 'connectionPoolParams');
  52. foreach ($paramList as $param) {
  53. if (isset($$param) === false) {
  54. throw new InvalidArgumentException('`' . $param . '` parameter must not be null');
  55. }
  56. }
  57. if (isset($connectionPoolParams['randomizeHosts']) === true
  58. && $connectionPoolParams['randomizeHosts'] === true) {
  59. shuffle($connections);
  60. }
  61. $this->connections = $connections;
  62. $this->seedConnections = $connections;
  63. $this->selector = $selector;
  64. $this->connectionPoolParams = $connectionPoolParams;
  65. $this->connectionFactory = $factory;
  66. }
  67. /**
  68. * @param bool $force
  69. *
  70. * @return Connection
  71. */
  72. abstract public function nextConnection($force = false);
  73. abstract public function scheduleCheck();
  74. }