| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Http\Client\Common\HttpClientPool; |
| 6 |
|
| 7 |
use Http\Client\Common\Exception\HttpClientNotFoundException; |
| 8 |
|
| 9 |
/** |
| 10 |
* RoundRobinClientPool will choose the next client in the pool. |
| 11 |
* |
| 12 |
* @author Joel Wurtz <joel.wurtz@gmail.com> |
| 13 |
*/ |
| 14 |
final class RoundRobinClientPool extends HttpClientPool |
| 15 |
{ |
| 16 |
/** |
| 17 |
* {@inheritdoc} |
| 18 |
*/ |
| 19 |
protected function chooseHttpClient(): HttpClientPoolItem |
| 20 |
{ |
| 21 |
$last = current($this->clientPool); |
| 22 |
|
| 23 |
do { |
| 24 |
$client = next($this->clientPool); |
| 25 |
|
| 26 |
if (false === $client) { |
| 27 |
$client = reset($this->clientPool); |
| 28 |
|
| 29 |
if (false === $client) { |
| 30 |
throw new HttpClientNotFoundException('Cannot choose a http client as there is no one present in the pool'); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// Case when there is only one and the last one has been disabled |
| 35 |
if ($last === $client && $client->isDisabled()) { |
| 36 |
throw new HttpClientNotFoundException('Cannot choose a http client as there is no one enabled in the pool'); |
| 37 |
} |
| 38 |
} while ($client->isDisabled()); |
| 39 |
|
| 40 |
return $client; |
| 41 |
} |
| 42 |
} |
| 43 |
|