Skip to content

Commit 01091ba

Browse files
committed
Add documention on http client pool
1 parent 4720a5d commit 01091ba

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

components/client-common.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,67 @@ PluginClient
8181
------------
8282

8383
See :doc:`/plugins/introduction`
84+
85+
HttpClientPool
86+
--------------
87+
88+
This kind of client allows to send a request on a pool of ``HttpClient`` and/or ``HttpAsyncClient`̀`.
89+
90+
You can attach http clients to this kind of client by using the ``addHttpClient`` method::
91+
92+
use Http\Client\Common\HttpClientPool\LeastUsedClientPool;
93+
use Http\Discovery\HttpAsyncClientDiscovery;
94+
use Http\Discovery\HttpClientDiscovery;
95+
use Http\Discovery\MessageFactoryDiscovery;
96+
97+
$messageFactory = MessageFactoryDiscovery::find();
98+
99+
$httpClient = HttpClientDiscovery::find();
100+
$httpAsyncClient = HttpAsyncClientDiscovery::find();
101+
102+
$httpClientPool = new LeastUsedClientPool();
103+
$httpClientPool->addHttpClient($httpClient);
104+
$httpClientPool->addHttpClient($httpAsyncClient);
105+
106+
$httpClientPool->sendRequest($messageFactory->createRequest('GET', 'http://example.com/update'));
107+
108+
Using this kind of client is useful is some use cases :
109+
110+
- When using a cluster (like a elasticsearch service with multiple master nodes)
111+
- When you want to use fallback servers with the combination of the ``RetryPlugin`` (see :doc:`/plugins/retry`)
112+
113+
Each added client will be decorated by the ``HttpClientPoolItem`` class unless it's already an instance of this class.
114+
It allows the pool to be aware of the number processing requests in real time, and also deactivate a client on a error,
115+
it will be reenable automatically after a certain amount of time. However this behavior is not enabled by default (it
116+
reenable after 0 second, so after each request), but you can enable this behavior, client by client by directly using
117+
the ``HttpClientPoolItem`` class::
118+
119+
// Reenable after 30 seconds
120+
$httpClientPool->addHttpClient(new HttpClientPoolItem($httpClient, 30));
121+
// Reenable after 3 minutes (180 seconds)
122+
$httpClientPool->addHttpClient(new HttpClientPoolItem($httpAsyncClient, 180));
123+
124+
This client cannot be used directly as it's only an abstraction. Each implementation as a specific strategy on how to
125+
choose a client into the pool :
126+
127+
LeastUsedClientPool
128+
*******************
129+
130+
``LeastUsedClientPool`` choose the client with less processing request in the pool. As it sounds the best strategy for
131+
sending a request on a pool of clients, the current implementation have some flaws :
132+
133+
- The counter is not shared between php process, so this strategy is not so useful in a web context, however it will make
134+
better sense in a php command line context.
135+
- The counter of a request is decremented after each client call, so using this with the ``sendRequest`` method is
136+
useless, as the first client will always be at 0 after each call. It's better to use the async mechanism, with the
137+
``sendAsyncRequest`` method to correctly distribute requests across your pool.
138+
139+
A disabled client will be remove for the pool until it is reenable, if none are available it will throw a
140+
``NotFoundHttpClientException`̀`
141+
142+
RoundRobinClientPool
143+
********************
144+
145+
``RoundRobinClientPool`` keeps an internal pointer on the pool. At each call the pointer is moved to the next client, if
146+
the current client is disabled it will move to the next client, and if none are available it will throw a
147+
``NotFoundHttpClientException`̀`

0 commit comments

Comments
 (0)