@@ -16,7 +16,6 @@ |
||
16 | 16 | |
17 | 17 | use RuntimeException; |
18 | 18 | |
19 | -class InvalidIterableException extends RuntimeException implements TransportException |
|
20 | -{ |
|
19 | +class InvalidIterableException extends RuntimeException implements TransportException { |
|
21 | 20 | |
22 | 21 | } |
23 | 22 | \ No newline at end of file |
@@ -24,59 +24,59 @@ |
||
24 | 24 | |
25 | 25 | class SimpleNodePool implements NodePoolInterface |
26 | 26 | { |
27 | - /** |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $nodes = []; |
|
27 | + /** |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $nodes = []; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @var SelectorInterface |
|
34 | - */ |
|
35 | - protected $selector; |
|
32 | + /** |
|
33 | + * @var SelectorInterface |
|
34 | + */ |
|
35 | + protected $selector; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @var ResurrectInterface |
|
39 | - */ |
|
40 | - protected $resurrect; |
|
37 | + /** |
|
38 | + * @var ResurrectInterface |
|
39 | + */ |
|
40 | + protected $resurrect; |
|
41 | 41 | |
42 | - public function __construct(SelectorInterface $selector, ResurrectInterface $resurrect) |
|
43 | - { |
|
44 | - $this->selector = $selector; |
|
45 | - $this->resurrect = $resurrect; |
|
46 | - } |
|
42 | + public function __construct(SelectorInterface $selector, ResurrectInterface $resurrect) |
|
43 | + { |
|
44 | + $this->selector = $selector; |
|
45 | + $this->resurrect = $resurrect; |
|
46 | + } |
|
47 | 47 | |
48 | - public function setHosts(array $hosts): self |
|
49 | - { |
|
50 | - $this->nodes = []; |
|
51 | - foreach ($hosts as $host) { |
|
52 | - $this->nodes[] = new Node($host); |
|
53 | - } |
|
54 | - shuffle($this->nodes); // randomize for use different hosts on each execution |
|
55 | - $this->selector->setNodes($this->nodes); |
|
48 | + public function setHosts(array $hosts): self |
|
49 | + { |
|
50 | + $this->nodes = []; |
|
51 | + foreach ($hosts as $host) { |
|
52 | + $this->nodes[] = new Node($host); |
|
53 | + } |
|
54 | + shuffle($this->nodes); // randomize for use different hosts on each execution |
|
55 | + $this->selector->setNodes($this->nodes); |
|
56 | 56 | |
57 | - return $this; |
|
58 | - } |
|
57 | + return $this; |
|
58 | + } |
|
59 | 59 | |
60 | - public function nextNode(): Node |
|
61 | - { |
|
62 | - $totNodes = count($this->nodes); |
|
63 | - $dead = 0; |
|
60 | + public function nextNode(): Node |
|
61 | + { |
|
62 | + $totNodes = count($this->nodes); |
|
63 | + $dead = 0; |
|
64 | 64 | |
65 | - while ($dead < $totNodes) { |
|
66 | - $next = $this->selector->nextNode(); |
|
67 | - if ($next->isAlive()) { |
|
68 | - return $next; |
|
69 | - } |
|
70 | - if ($this->resurrect->ping($next)) { |
|
71 | - $next->markAlive(true); |
|
72 | - return $next; |
|
73 | - } |
|
74 | - $dead++; |
|
75 | - } |
|
65 | + while ($dead < $totNodes) { |
|
66 | + $next = $this->selector->nextNode(); |
|
67 | + if ($next->isAlive()) { |
|
68 | + return $next; |
|
69 | + } |
|
70 | + if ($this->resurrect->ping($next)) { |
|
71 | + $next->markAlive(true); |
|
72 | + return $next; |
|
73 | + } |
|
74 | + $dead++; |
|
75 | + } |
|
76 | 76 | |
77 | - throw new NoNodeAvailableException(sprintf( |
|
78 | - 'No alive nodes. All the %d nodes seem to be down.', |
|
79 | - $totNodes |
|
80 | - )); |
|
81 | - } |
|
77 | + throw new NoNodeAvailableException(sprintf( |
|
78 | + 'No alive nodes. All the %d nodes seem to be down.', |
|
79 | + $totNodes |
|
80 | + )); |
|
81 | + } |
|
82 | 82 | } |
83 | 83 | \ No newline at end of file |
@@ -22,8 +22,7 @@ |
||
22 | 22 | use function shuffle; |
23 | 23 | use function sprintf; |
24 | 24 | |
25 | -class SimpleNodePool implements NodePoolInterface |
|
26 | -{ |
|
25 | +class SimpleNodePool implements NodePoolInterface { |
|
27 | 26 | /** |
28 | 27 | * @var array |
29 | 28 | */ |
@@ -16,10 +16,10 @@ |
||
16 | 16 | |
17 | 17 | interface NodePoolInterface |
18 | 18 | { |
19 | - public function nextNode(): Node; |
|
19 | + public function nextNode(): Node; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @param string[] $hosts |
|
23 | - */ |
|
24 | - public function setHosts(array $hosts): self; |
|
21 | + /** |
|
22 | + * @param string[] $hosts |
|
23 | + */ |
|
24 | + public function setHosts(array $hosts): self; |
|
25 | 25 | } |
26 | 26 | \ No newline at end of file |
@@ -14,8 +14,7 @@ |
||
14 | 14 | |
15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Transport\NodePool; |
16 | 16 | |
17 | -interface NodePoolInterface |
|
18 | -{ |
|
17 | +interface NodePoolInterface { |
|
19 | 18 | public function nextNode(): Node; |
20 | 19 | |
21 | 20 | /** |
@@ -19,23 +19,23 @@ |
||
19 | 19 | |
20 | 20 | trait SelectorTrait |
21 | 21 | { |
22 | - protected array $nodes = []; |
|
22 | + protected array $nodes = []; |
|
23 | 23 | |
24 | - public function setNodes(array $nodes): void |
|
25 | - { |
|
26 | - foreach ($nodes as $node) { |
|
27 | - if (!$node instanceof Node) { |
|
28 | - throw new InvalidArrayException(sprintf( |
|
29 | - "The nodes array must contain only %s objects", |
|
30 | - Node::class |
|
31 | - )); |
|
32 | - } |
|
33 | - } |
|
34 | - $this->nodes = $nodes; |
|
35 | - } |
|
24 | + public function setNodes(array $nodes): void |
|
25 | + { |
|
26 | + foreach ($nodes as $node) { |
|
27 | + if (!$node instanceof Node) { |
|
28 | + throw new InvalidArrayException(sprintf( |
|
29 | + "The nodes array must contain only %s objects", |
|
30 | + Node::class |
|
31 | + )); |
|
32 | + } |
|
33 | + } |
|
34 | + $this->nodes = $nodes; |
|
35 | + } |
|
36 | 36 | |
37 | - public function getNodes(): array |
|
38 | - { |
|
39 | - return $this->nodes; |
|
40 | - } |
|
37 | + public function getNodes(): array |
|
38 | + { |
|
39 | + return $this->nodes; |
|
40 | + } |
|
41 | 41 | } |
42 | 42 | \ No newline at end of file |
@@ -17,8 +17,7 @@ |
||
17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Transport\NodePool\Node; |
18 | 18 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Transport\Exception\InvalidArrayException; |
19 | 19 | |
20 | -trait SelectorTrait |
|
21 | -{ |
|
20 | +trait SelectorTrait { |
|
22 | 21 | protected array $nodes = []; |
23 | 22 | |
24 | 23 | public function setNodes(array $nodes): void |
@@ -19,22 +19,22 @@ |
||
19 | 19 | |
20 | 20 | class RoundRobin implements SelectorInterface |
21 | 21 | { |
22 | - use SelectorTrait; |
|
22 | + use SelectorTrait; |
|
23 | 23 | |
24 | - public function nextNode(): Node |
|
25 | - { |
|
26 | - if (empty($this->getNodes())) { |
|
27 | - $className = substr(__CLASS__, strrpos(__CLASS__, '\\') + 1); |
|
28 | - throw new NoNodeAvailableException(sprintf( |
|
29 | - "No node available. Please use %s::setNodes() before calling %s::nextNode().", |
|
30 | - $className, |
|
31 | - $className |
|
32 | - )); |
|
33 | - } |
|
34 | - $node = current($this->nodes); |
|
35 | - if (false === next($this->nodes)) { |
|
36 | - reset($this->nodes); |
|
37 | - } |
|
38 | - return $node; |
|
39 | - } |
|
24 | + public function nextNode(): Node |
|
25 | + { |
|
26 | + if (empty($this->getNodes())) { |
|
27 | + $className = substr(__CLASS__, strrpos(__CLASS__, '\\') + 1); |
|
28 | + throw new NoNodeAvailableException(sprintf( |
|
29 | + "No node available. Please use %s::setNodes() before calling %s::nextNode().", |
|
30 | + $className, |
|
31 | + $className |
|
32 | + )); |
|
33 | + } |
|
34 | + $node = current($this->nodes); |
|
35 | + if (false === next($this->nodes)) { |
|
36 | + reset($this->nodes); |
|
37 | + } |
|
38 | + return $node; |
|
39 | + } |
|
40 | 40 | } |
41 | 41 | \ No newline at end of file |
@@ -17,8 +17,7 @@ |
||
17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Transport\NodePool\Node; |
18 | 18 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Transport\Exception\NoNodeAvailableException; |
19 | 19 | |
20 | -class RoundRobin implements SelectorInterface |
|
21 | -{ |
|
20 | +class RoundRobin implements SelectorInterface { |
|
22 | 21 | use SelectorTrait; |
23 | 22 | |
24 | 23 | public function nextNode(): Node |
@@ -18,15 +18,15 @@ |
||
18 | 18 | |
19 | 19 | interface SelectorInterface |
20 | 20 | { |
21 | - public function nextNode(): Node; |
|
21 | + public function nextNode(): Node; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @param Node[] $nodes |
|
25 | - */ |
|
26 | - public function setNodes(array $nodes): void; |
|
23 | + /** |
|
24 | + * @param Node[] $nodes |
|
25 | + */ |
|
26 | + public function setNodes(array $nodes): void; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return Node[] |
|
30 | - */ |
|
31 | - public function getNodes(): array; |
|
28 | + /** |
|
29 | + * @return Node[] |
|
30 | + */ |
|
31 | + public function getNodes(): array; |
|
32 | 32 | } |
33 | 33 | \ No newline at end of file |
@@ -16,8 +16,7 @@ |
||
16 | 16 | |
17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Transport\NodePool\Node; |
18 | 18 | |
19 | -interface SelectorInterface |
|
20 | -{ |
|
19 | +interface SelectorInterface { |
|
21 | 20 | public function nextNode(): Node; |
22 | 21 | |
23 | 22 | /** |
@@ -22,29 +22,29 @@ |
||
22 | 22 | |
23 | 23 | class Node |
24 | 24 | { |
25 | - protected UriInterface $uri; |
|
26 | - protected bool $alive = true; |
|
27 | - |
|
28 | - public function __construct(string $host) |
|
29 | - { |
|
30 | - if (substr($host, 0, 5) !== 'http:' && substr($host, 0, 6) !== 'https:') { |
|
31 | - $host = sprintf("http://%s", $host); |
|
32 | - } |
|
33 | - $this->uri = Psr17FactoryDiscovery::findUriFactory()->createUri($host); |
|
34 | - } |
|
35 | - |
|
36 | - public function markAlive(bool $alive): void |
|
37 | - { |
|
38 | - $this->alive = $alive; |
|
39 | - } |
|
40 | - |
|
41 | - public function isAlive(): bool |
|
42 | - { |
|
43 | - return $this->alive; |
|
44 | - } |
|
45 | - |
|
46 | - public function getUri(): UriInterface |
|
47 | - { |
|
48 | - return $this->uri; |
|
49 | - } |
|
25 | + protected UriInterface $uri; |
|
26 | + protected bool $alive = true; |
|
27 | + |
|
28 | + public function __construct(string $host) |
|
29 | + { |
|
30 | + if (substr($host, 0, 5) !== 'http:' && substr($host, 0, 6) !== 'https:') { |
|
31 | + $host = sprintf("http://%s", $host); |
|
32 | + } |
|
33 | + $this->uri = Psr17FactoryDiscovery::findUriFactory()->createUri($host); |
|
34 | + } |
|
35 | + |
|
36 | + public function markAlive(bool $alive): void |
|
37 | + { |
|
38 | + $this->alive = $alive; |
|
39 | + } |
|
40 | + |
|
41 | + public function isAlive(): bool |
|
42 | + { |
|
43 | + return $this->alive; |
|
44 | + } |
|
45 | + |
|
46 | + public function getUri(): UriInterface |
|
47 | + { |
|
48 | + return $this->uri; |
|
49 | + } |
|
50 | 50 | } |
51 | 51 | \ No newline at end of file |
@@ -20,8 +20,7 @@ |
||
20 | 20 | use function sprintf; |
21 | 21 | use function substr; |
22 | 22 | |
23 | -class Node |
|
24 | -{ |
|
23 | +class Node { |
|
25 | 24 | protected UriInterface $uri; |
26 | 25 | protected bool $alive = true; |
27 | 26 |
@@ -19,10 +19,10 @@ |
||
19 | 19 | |
20 | 20 | class NoResurrect implements ResurrectInterface |
21 | 21 | { |
22 | - protected ClientInterface $client; |
|
22 | + protected ClientInterface $client; |
|
23 | 23 | |
24 | - public function ping(Node $node): bool |
|
25 | - { |
|
26 | - return false; |
|
27 | - } |
|
24 | + public function ping(Node $node): bool |
|
25 | + { |
|
26 | + return false; |
|
27 | + } |
|
28 | 28 | } |
29 | 29 | \ No newline at end of file |
@@ -17,8 +17,7 @@ |
||
17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Transport\NodePool\Node; |
18 | 18 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Client\ClientInterface; |
19 | 19 | |
20 | -class NoResurrect implements ResurrectInterface |
|
21 | -{ |
|
20 | +class NoResurrect implements ResurrectInterface { |
|
22 | 21 | protected ClientInterface $client; |
23 | 22 | |
24 | 23 | public function ping(Node $node): bool |
@@ -19,5 +19,5 @@ |
||
19 | 19 | |
20 | 20 | interface ResurrectInterface |
21 | 21 | { |
22 | - public function ping(Node $node): bool; |
|
22 | + public function ping(Node $node): bool; |
|
23 | 23 | } |
24 | 24 | \ No newline at end of file |
@@ -17,7 +17,6 @@ |
||
17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Transport\NodePool\Node; |
18 | 18 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Client\ClientInterface; |
19 | 19 | |
20 | -interface ResurrectInterface |
|
21 | -{ |
|
20 | +interface ResurrectInterface { |
|
22 | 21 | public function ping(Node $node): bool; |
23 | 22 | } |
24 | 23 | \ No newline at end of file |