PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / http / client-common / HttpClientPool / LeastUsedClientPool.php

LeastUsedClientPool.php in DecaLog 4.4.0, at includes/libraries/http/client-common/HttpClientPool/LeastUsedClientPool.php

46 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * LeastUsedClientPool will choose the client with the less current request in the pool.
11 *
12 * This strategy is only useful when doing async request
13 *
14 * @author Joel Wurtz <joel.wurtz@gmail.com>
15 */
16 final class LeastUsedClientPool extends HttpClientPool
17 {
18 /**
19 * {@inheritdoc}
20 */
21 protected function chooseHttpClient(): HttpClientPoolItem
22 {
23 $clientPool = array_filter($this->clientPool, function (HttpClientPoolItem $clientPoolItem) {
24 return !$clientPoolItem->isDisabled();
25 });
26
27 if (0 === count($clientPool)) {
28 throw new HttpClientNotFoundException('Cannot choose a http client as there is no one present in the pool');
29 }
30
31 usort($clientPool, function (HttpClientPoolItem $clientA, HttpClientPoolItem $clientB) {
32 if ($clientA->getSendingRequestCount() === $clientB->getSendingRequestCount()) {
33 return 0;
34 }
35
36 if ($clientA->getSendingRequestCount() < $clientB->getSendingRequestCount()) {
37 return -1;
38 }
39
40 return 1;
41 });
42
43 return reset($clientPool);
44 }
45 }
46