|
2 | 2 |
|
3 | 3 | namespace Http\Client\Common\HttpClientPool;
|
4 | 4 |
|
| 5 | +use Http\Client\Common\FlexibleHttpClient; |
5 | 6 | use Http\Client\HttpAsyncClient;
|
6 | 7 | use Http\Client\HttpClient;
|
| 8 | +use Psr\Http\Message\RequestInterface; |
| 9 | +use Http\Client\Exception; |
| 10 | +use Psr\Http\Message\ResponseInterface; |
7 | 11 |
|
8 | 12 | /**
|
9 | 13 | * A HttpClientPoolItem represent a HttpClient inside a Pool.
|
|
12 | 16 | * It also keep tracks of the current number of open requests the client is currently being sending
|
13 | 17 | * (only usable for async method).
|
14 | 18 | *
|
| 19 | + * This class is used internally in the client pools and is not supposed to be used anywhere else. |
| 20 | + * |
| 21 | + * @final |
| 22 | + * |
| 23 | + * @internal |
| 24 | + * |
15 | 25 | * @author Joel Wurtz <joel.wurtz@gmail.com>
|
16 | 26 | */
|
17 |
| -interface HttpClientPoolItem extends HttpClient, HttpAsyncClient |
| 27 | +class HttpClientPoolItem implements HttpClient, HttpAsyncClient |
18 | 28 | {
|
19 | 29 | /**
|
20 |
| - * Whether this client is disabled or not. |
| 30 | + * @var int Number of request this client is currently sending |
| 31 | + */ |
| 32 | + private $sendingRequestCount = 0; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var \DateTime|null Time when this client has been disabled or null if enable |
| 36 | + */ |
| 37 | + private $disabledAt; |
| 38 | + |
| 39 | + /** |
| 40 | + * Number of seconds until this client is enabled again after an error. |
21 | 41 | *
|
22 |
| - * Will also reactivate this client if possible |
| 42 | + * null: never reenable this client. |
23 | 43 | *
|
24 |
| - * @internal |
| 44 | + * @var int|null |
| 45 | + */ |
| 46 | + private $reenableAfter; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var FlexibleHttpClient A http client responding to async and sync request |
| 50 | + */ |
| 51 | + private $client; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param HttpClient|HttpAsyncClient $client |
| 55 | + * @param null|int $reenableAfter Number of seconds until this client is enabled again after an error |
| 56 | + */ |
| 57 | + public function __construct($client, $reenableAfter = null) |
| 58 | + { |
| 59 | + $this->client = new FlexibleHttpClient($client); |
| 60 | + $this->reenableAfter = $reenableAfter; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 67 | + { |
| 68 | + if ($this->isDisabled()) { |
| 69 | + throw new Exception\RequestException('Cannot send the request as this client has been disabled', $request); |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + $this->incrementRequestCount(); |
| 74 | + $response = $this->client->sendRequest($request); |
| 75 | + $this->decrementRequestCount(); |
| 76 | + } catch (Exception $e) { |
| 77 | + $this->disable(); |
| 78 | + $this->decrementRequestCount(); |
| 79 | + |
| 80 | + throw $e; |
| 81 | + } |
| 82 | + |
| 83 | + return $response; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritdoc} |
| 88 | + */ |
| 89 | + public function sendAsyncRequest(RequestInterface $request) |
| 90 | + { |
| 91 | + if ($this->isDisabled()) { |
| 92 | + throw new Exception\RequestException('Cannot send the request as this client has been disabled', $request); |
| 93 | + } |
| 94 | + |
| 95 | + $this->incrementRequestCount(); |
| 96 | + |
| 97 | + return $this->client->sendAsyncRequest($request)->then(function ($response) { |
| 98 | + $this->decrementRequestCount(); |
| 99 | + |
| 100 | + return $response; |
| 101 | + }, function ($exception) { |
| 102 | + $this->disable(); |
| 103 | + $this->decrementRequestCount(); |
| 104 | + |
| 105 | + throw $exception; |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Whether this client is disabled or not. |
25 | 111 | *
|
26 |
| - * @return bool |
| 112 | + * If the client was disabled, calling this method checks if the client can |
| 113 | + * be reenabled and if so enables it. |
27 | 114 | */
|
28 |
| - public function isDisabled(); |
| 115 | + public function isDisabled(): bool |
| 116 | + { |
| 117 | + if (null !== $this->reenableAfter && null !== $this->disabledAt) { |
| 118 | + // Reenable after a certain time |
| 119 | + $now = new \DateTime(); |
| 120 | + |
| 121 | + if (($now->getTimestamp() - $this->disabledAt->getTimestamp()) >= $this->reenableAfter) { |
| 122 | + $this->enable(); |
| 123 | + |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + return null !== $this->disabledAt; |
| 131 | + } |
29 | 132 |
|
30 | 133 | /**
|
31 | 134 | * Get current number of request that are currently being sent by the underlying HTTP client.
|
32 |
| - * |
33 |
| - * @internal |
34 |
| - * |
35 |
| - * @return int |
36 | 135 | */
|
37 |
| - public function getSendingRequestCount(); |
| 136 | + public function getSendingRequestCount(): int |
| 137 | + { |
| 138 | + return $this->sendingRequestCount; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Increment the request count. |
| 143 | + */ |
| 144 | + private function incrementRequestCount() |
| 145 | + { |
| 146 | + ++$this->sendingRequestCount; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Decrement the request count. |
| 151 | + */ |
| 152 | + private function decrementRequestCount() |
| 153 | + { |
| 154 | + --$this->sendingRequestCount; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Enable the current client. |
| 159 | + */ |
| 160 | + private function enable() |
| 161 | + { |
| 162 | + $this->disabledAt = null; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Disable the current client. |
| 167 | + */ |
| 168 | + private function disable() |
| 169 | + { |
| 170 | + $this->disabledAt = new \DateTime('now'); |
| 171 | + } |
38 | 172 | }
|
0 commit comments