Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method getNodes()for LoadBalancerInterface. #1331

Merged
merged 4 commits into from Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions .travis.yml
Expand Up @@ -5,13 +5,13 @@ sudo: required
matrix:
include:
- php: 7.2
env: SW_VERSION="4.4.14"
env: SW_VERSION="4.4.15"
- php: 7.3
env: SW_VERSION="4.4.14"
env: SW_VERSION="4.4.15"
- php: 7.4
env: SW_VERSION="4.4.14"
env: SW_VERSION="4.4.15"
- php: master
env: SW_VERSION="4.4.14"
env: SW_VERSION="4.4.15"

allow_failures:
- php: master
Expand Down Expand Up @@ -41,5 +41,6 @@ before_script:

script:
- composer analyse src/di src/json-rpc src/tracer src/metric src/redis src/nats src/db src/retry src/grpc-client src/nsq
- composer analyse src/load-balancer
- composer test -- --exclude-group NonCoroutine
- vendor/bin/phpunit --group NonCoroutine
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# v1.1.18 - TBD

## Added

- [#1331](https://github.com/hyperf/hyperf/pull/1331) Added `Hyperf\LoadBalancer\LoadBalancerInterface::getNodes()`.

## Changed

- [#1324](https://github.com/hyperf/hyperf/pull/1324) `Hyperf\AsyncQueue\Listener\QueueLengthListener` is no longer as the default listener of [hyperf/async-queue](https://github.com/hyperf/async-queue).
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -10,9 +10,9 @@
"support": {},
"require": {
"php": ">=7.2",
"ext-bcmath": "*",
"ext-json": "*",
"ext-swoole": ">=4.4",
"ext-bcmath": "*",
"bandwidth-throttle/token-bucket": "^2.0",
"doctrine/annotations": "^1.6",
"doctrine/inflector": "^1.3",
Expand All @@ -28,6 +28,7 @@
"jcchavezs/zipkin-opentracing": "^0.1.4",
"jean85/pretty-package-versions": "^1.2",
"laminas/laminas-mime": "^2.7",
"markrogoyski/math-php": "^0.49.0",
"monolog/monolog": "^1.24",
"nesbot/carbon": "^2.0",
"nikic/fast-route": "^1.3",
Expand Down
2 changes: 1 addition & 1 deletion src/grpc-client/tests/BaseClientTest.php
Expand Up @@ -60,7 +60,7 @@ public static function tearDownAfterClass()

public function setUp()
{
if (swoole_version() === '4.4.14') {
if (in_array(swoole_version(), ['4.4.14', '4.4.15'])) {
$this->markTestSkipped(
'Swoole v4.4.14 has a bug on their side.'
);
Expand Down
5 changes: 5 additions & 0 deletions src/load-balancer/src/AbstractLoadBalancer.php
Expand Up @@ -39,6 +39,11 @@ public function setNodes(array $nodes)
return $this;
}

public function getNodes(): array
{
return $this->nodes;
}

/**
* Remove a node from the node list.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/load-balancer/src/LoadBalancerInterface.php
Expand Up @@ -20,11 +20,16 @@ interface LoadBalancerInterface
public function select(array ...$parameters): Node;

/**
* @param \Hyperf\LoadBalancer\Node[] $nodes
* @param Node[] $nodes
* @return $this
*/
public function setNodes(array $nodes);

/**
* @return Node[] $nodes
*/
public function getNodes(): array;

/**
* Remove a node from the node list.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/load-balancer/src/WeightedRoundRobin.php
Expand Up @@ -50,7 +50,7 @@ public function select(array ...$parameters): Node
$this->currentWeight = $this->maxWeight;
if ($this->currentWeight == 0) {
// Degrade to random algorithm.
return array_rand($this->nodes);
return $this->nodes[array_rand($this->nodes)];
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/load-balancer/tests/RandomTest.php
Expand Up @@ -32,5 +32,6 @@ public function testRandom()
$random = new Random($nodes);
$node = $random->select();
$this->assertTrue(in_array($node, $nodes));
$this->assertSame($nodes, $random->getNodes());
}
}