From 50ec5017b85b7d707adc0ac5635edceb66b682b2 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 20 Jul 2022 17:12:39 +0800 Subject: [PATCH] AddsThrow NoNodesAvailableException when cannot select any node --- .gitignore | 1 + src/Exception/NoNodesAvailableException.php | 18 ++++++++++++++++++ src/LoadBalancer/Random.php | 4 +++- src/LoadBalancer/RoundRobin.php | 4 ++-- src/Transporter/StreamSocketTransporter.php | 2 +- 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/Exception/NoNodesAvailableException.php diff --git a/.gitignore b/.gitignore index 5aaa618..b85e3f7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ composer.lock *.bak /phpunit.xml /.phpunit.result.cache +.idea \ No newline at end of file diff --git a/src/Exception/NoNodesAvailableException.php b/src/Exception/NoNodesAvailableException.php new file mode 100644 index 0000000..b3febb8 --- /dev/null +++ b/src/Exception/NoNodesAvailableException.php @@ -0,0 +1,18 @@ +nodes)) { - throw new \RuntimeException('Cannot select any node from load balancer.'); + throw new NoNodesAvailableException('Cannot select any node from load balancer.'); } $key = array_rand($this->nodes); diff --git a/src/LoadBalancer/RoundRobin.php b/src/LoadBalancer/RoundRobin.php index beb1d35..df6c6b9 100644 --- a/src/LoadBalancer/RoundRobin.php +++ b/src/LoadBalancer/RoundRobin.php @@ -11,7 +11,7 @@ */ namespace FriendsOfHyperf\Jet\LoadBalancer; -use RuntimeException; +use FriendsOfHyperf\Jet\Exception\NoNodesAvailableException; class RoundRobin extends AbstractLoadBalancer { @@ -28,7 +28,7 @@ public function select(): Node $count = count($this->nodes); if ($count <= 0) { - throw new RuntimeException('Nodes missing.'); + throw new NoNodesAvailableException('Nodes missing.'); } $item = $this->nodes[self::$current % $count]; diff --git a/src/Transporter/StreamSocketTransporter.php b/src/Transporter/StreamSocketTransporter.php index bd3c68e..b519f4d 100644 --- a/src/Transporter/StreamSocketTransporter.php +++ b/src/Transporter/StreamSocketTransporter.php @@ -27,7 +27,7 @@ class StreamSocketTransporter extends AbstractTransporter protected $client; /** - * @var float + * @var int */ protected $timeout;