Added Hyperf\LoadBalancer\LoadBalancerInterface::getNodes(). (#1331)

This commit is contained in:
李铭昕 2020-02-06 13:38:16 +08:00 committed by GitHub
parent 11b60b7419
commit 322a3477a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 25 additions and 8 deletions

View File

@ -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
@ -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

View File

@ -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).

View File

@ -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",
@ -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",

View File

@ -60,7 +60,7 @@ class BaseClientTest extends TestCase
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.'
);

View File

@ -39,6 +39,11 @@ abstract class AbstractLoadBalancer implements LoadBalancerInterface
return $this;
}
public function getNodes(): array
{
return $this->nodes;
}
/**
* Remove a node from the node list.
*/

View File

@ -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.
*/

View File

@ -50,7 +50,7 @@ class WeightedRoundRobin extends AbstractLoadBalancer
$this->currentWeight = $this->maxWeight;
if ($this->currentWeight == 0) {
// Degrade to random algorithm.
return array_rand($this->nodes);
return $this->nodes[array_rand($this->nodes)];
}
}
}

View File

@ -32,5 +32,6 @@ class RandomTest extends TestCase
$random = new Random($nodes);
$node = $random->select();
$this->assertTrue(in_array($node, $nodes));
$this->assertSame($nodes, $random->getNodes());
}
}