version 1.5
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client\Common\HttpClientPool;
|
||||
|
||||
use Http\Client\Common\Exception\HttpClientNotFoundException;
|
||||
use Http\Client\Common\HttpClientPool as HttpClientPoolInterface;
|
||||
use Http\Client\HttpAsyncClient;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* A http client pool allows to send requests on a pool of different http client using a specific strategy (least used,
|
||||
* round robin, ...).
|
||||
*/
|
||||
abstract class HttpClientPool implements HttpClientPoolInterface
|
||||
{
|
||||
/**
|
||||
* @var HttpClientPoolItem[]
|
||||
*/
|
||||
protected $clientPool = [];
|
||||
|
||||
/**
|
||||
* Add a client to the pool.
|
||||
*
|
||||
* @param ClientInterface|HttpAsyncClient $client
|
||||
*/
|
||||
public function addHttpClient($client): void
|
||||
{
|
||||
// no need to check for HttpClientPoolItem here, since it extends the other interfaces
|
||||
if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) {
|
||||
throw new \TypeError(
|
||||
sprintf('%s::addHttpClient(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client))
|
||||
);
|
||||
}
|
||||
|
||||
if (!$client instanceof HttpClientPoolItem) {
|
||||
$client = new HttpClientPoolItem($client);
|
||||
}
|
||||
|
||||
$this->clientPool[] = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an http client given a specific strategy.
|
||||
*
|
||||
* @return HttpClientPoolItem Return a http client that can do both sync or async
|
||||
*
|
||||
* @throws HttpClientNotFoundException When no http client has been found into the pool
|
||||
*/
|
||||
abstract protected function chooseHttpClient(): HttpClientPoolItem;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sendAsyncRequest(RequestInterface $request)
|
||||
{
|
||||
return $this->chooseHttpClient()->sendAsyncRequest($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sendRequest(RequestInterface $request): ResponseInterface
|
||||
{
|
||||
return $this->chooseHttpClient()->sendRequest($request);
|
||||
}
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client\Common\HttpClientPool;
|
||||
|
||||
use Http\Client\Common\FlexibleHttpClient;
|
||||
use Http\Client\Exception;
|
||||
use Http\Client\HttpAsyncClient;
|
||||
use Http\Client\HttpClient;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* A HttpClientPoolItem represent a HttpClient inside a Pool.
|
||||
*
|
||||
* It is disabled when a request failed and can be reenabled after a certain number of seconds.
|
||||
* It also keep tracks of the current number of open requests the client is currently being sending
|
||||
* (only usable for async method).
|
||||
*
|
||||
* This class is used internally in the client pools and is not supposed to be used anywhere else.
|
||||
*
|
||||
* @final
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @author Joel Wurtz <joel.wurtz@gmail.com>
|
||||
*/
|
||||
class HttpClientPoolItem implements HttpClient, HttpAsyncClient
|
||||
{
|
||||
/**
|
||||
* @var int Number of request this client is currently sending
|
||||
*/
|
||||
private $sendingRequestCount = 0;
|
||||
|
||||
/**
|
||||
* @var \DateTime|null Time when this client has been disabled or null if enable
|
||||
*/
|
||||
private $disabledAt;
|
||||
|
||||
/**
|
||||
* Number of seconds until this client is enabled again after an error.
|
||||
*
|
||||
* null: never reenable this client.
|
||||
*
|
||||
* @var int|null
|
||||
*/
|
||||
private $reenableAfter;
|
||||
|
||||
/**
|
||||
* @var FlexibleHttpClient A http client responding to async and sync request
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* @param ClientInterface|HttpAsyncClient $client
|
||||
* @param int|null $reenableAfter Number of seconds until this client is enabled again after an error
|
||||
*/
|
||||
public function __construct($client, int $reenableAfter = null)
|
||||
{
|
||||
if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) {
|
||||
throw new \TypeError(
|
||||
sprintf('%s::__construct(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client))
|
||||
);
|
||||
}
|
||||
|
||||
$this->client = new FlexibleHttpClient($client);
|
||||
$this->reenableAfter = $reenableAfter;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sendRequest(RequestInterface $request): ResponseInterface
|
||||
{
|
||||
if ($this->isDisabled()) {
|
||||
throw new Exception\RequestException('Cannot send the request as this client has been disabled', $request);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->incrementRequestCount();
|
||||
$response = $this->client->sendRequest($request);
|
||||
$this->decrementRequestCount();
|
||||
} catch (Exception $e) {
|
||||
$this->disable();
|
||||
$this->decrementRequestCount();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sendAsyncRequest(RequestInterface $request)
|
||||
{
|
||||
if ($this->isDisabled()) {
|
||||
throw new Exception\RequestException('Cannot send the request as this client has been disabled', $request);
|
||||
}
|
||||
|
||||
$this->incrementRequestCount();
|
||||
|
||||
return $this->client->sendAsyncRequest($request)->then(function ($response) {
|
||||
$this->decrementRequestCount();
|
||||
|
||||
return $response;
|
||||
}, function ($exception) {
|
||||
$this->disable();
|
||||
$this->decrementRequestCount();
|
||||
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this client is disabled or not.
|
||||
*
|
||||
* If the client was disabled, calling this method checks if the client can
|
||||
* be reenabled and if so enables it.
|
||||
*/
|
||||
public function isDisabled(): bool
|
||||
{
|
||||
if (null !== $this->reenableAfter && null !== $this->disabledAt) {
|
||||
// Reenable after a certain time
|
||||
$now = new \DateTime();
|
||||
|
||||
if (($now->getTimestamp() - $this->disabledAt->getTimestamp()) >= $this->reenableAfter) {
|
||||
$this->enable();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return null !== $this->disabledAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current number of request that are currently being sent by the underlying HTTP client.
|
||||
*/
|
||||
public function getSendingRequestCount(): int
|
||||
{
|
||||
return $this->sendingRequestCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the request count.
|
||||
*/
|
||||
private function incrementRequestCount(): void
|
||||
{
|
||||
++$this->sendingRequestCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement the request count.
|
||||
*/
|
||||
private function decrementRequestCount(): void
|
||||
{
|
||||
--$this->sendingRequestCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the current client.
|
||||
*/
|
||||
private function enable(): void
|
||||
{
|
||||
$this->disabledAt = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the current client.
|
||||
*/
|
||||
private function disable(): void
|
||||
{
|
||||
$this->disabledAt = new \DateTime('now');
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client\Common\HttpClientPool;
|
||||
|
||||
use Http\Client\Common\Exception\HttpClientNotFoundException;
|
||||
|
||||
/**
|
||||
* LeastUsedClientPool will choose the client with the less current request in the pool.
|
||||
*
|
||||
* This strategy is only useful when doing async request
|
||||
*
|
||||
* @author Joel Wurtz <joel.wurtz@gmail.com>
|
||||
*/
|
||||
final class LeastUsedClientPool extends HttpClientPool
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function chooseHttpClient(): HttpClientPoolItem
|
||||
{
|
||||
$clientPool = array_filter($this->clientPool, function (HttpClientPoolItem $clientPoolItem) {
|
||||
return !$clientPoolItem->isDisabled();
|
||||
});
|
||||
|
||||
if (0 === count($clientPool)) {
|
||||
throw new HttpClientNotFoundException('Cannot choose a http client as there is no one present in the pool');
|
||||
}
|
||||
|
||||
usort($clientPool, function (HttpClientPoolItem $clientA, HttpClientPoolItem $clientB) {
|
||||
if ($clientA->getSendingRequestCount() === $clientB->getSendingRequestCount()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ($clientA->getSendingRequestCount() < $clientB->getSendingRequestCount()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
});
|
||||
|
||||
return reset($clientPool);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client\Common\HttpClientPool;
|
||||
|
||||
use Http\Client\Common\Exception\HttpClientNotFoundException;
|
||||
|
||||
/**
|
||||
* RoundRobinClientPool will choose the next client in the pool.
|
||||
*
|
||||
* @author Joel Wurtz <joel.wurtz@gmail.com>
|
||||
*/
|
||||
final class RandomClientPool extends HttpClientPool
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function chooseHttpClient(): HttpClientPoolItem
|
||||
{
|
||||
$clientPool = array_filter($this->clientPool, function (HttpClientPoolItem $clientPoolItem) {
|
||||
return !$clientPoolItem->isDisabled();
|
||||
});
|
||||
|
||||
if (0 === count($clientPool)) {
|
||||
throw new HttpClientNotFoundException('Cannot choose a http client as there is no one present in the pool');
|
||||
}
|
||||
|
||||
return $clientPool[array_rand($clientPool)];
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client\Common\HttpClientPool;
|
||||
|
||||
use Http\Client\Common\Exception\HttpClientNotFoundException;
|
||||
|
||||
/**
|
||||
* RoundRobinClientPool will choose the next client in the pool.
|
||||
*
|
||||
* @author Joel Wurtz <joel.wurtz@gmail.com>
|
||||
*/
|
||||
final class RoundRobinClientPool extends HttpClientPool
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function chooseHttpClient(): HttpClientPoolItem
|
||||
{
|
||||
$last = current($this->clientPool);
|
||||
|
||||
do {
|
||||
$client = next($this->clientPool);
|
||||
|
||||
if (false === $client) {
|
||||
$client = reset($this->clientPool);
|
||||
|
||||
if (false === $client) {
|
||||
throw new HttpClientNotFoundException('Cannot choose a http client as there is no one present in the pool');
|
||||
}
|
||||
}
|
||||
|
||||
// Case when there is only one and the last one has been disabled
|
||||
if ($last === $client && $client->isDisabled()) {
|
||||
throw new HttpClientNotFoundException('Cannot choose a http client as there is no one enabled in the pool');
|
||||
}
|
||||
} while ($client->isDisabled());
|
||||
|
||||
return $client;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user