Skip to content

Commit 69930d3

Browse files
committed
Add documention on http client pool
1 parent 9970840 commit 69930d3

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

components/client-common.rst

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

8383
See :doc:`/plugins/introduction`
84+
85+
HttpClientPool
86+
--------------
87+
88+
The ``HttpClientPool`` allows to balance requests between a pool of ``HttpClient`` and/or ``HttpAsyncClient``.
89+
90+
The use cases are:
91+
92+
- Using a cluster (like an Elasticsearch service with multiple master nodes)
93+
- Using fallback servers with the combination of the ``RetryPlugin`` (see :doc:`/plugins/retry`)
94+
95+
You can attach HTTP clients to this kind of client by using the ``addHttpClient`` method::
96+
97+
use Http\Client\Common\HttpClientPool\LeastUsedClientPool;
98+
use Http\Discovery\HttpAsyncClientDiscovery;
99+
use Http\Discovery\HttpClientDiscovery;
100+
use Http\Discovery\MessageFactoryDiscovery;
101+
102+
$messageFactory = MessageFactoryDiscovery::find();
103+
104+
$httpClient = HttpClientDiscovery::find();
105+
$httpAsyncClient = HttpAsyncClientDiscovery::find();
106+
107+
$httpClientPool = new LeastUsedClientPool();
108+
$httpClientPool->addHttpClient($httpClient);
109+
$httpClientPool->addHttpClient($httpAsyncClient);
110+
111+
$httpClientPool->sendRequest($messageFactory->createRequest('GET', 'http://example.com/update'));
112+
113+
Clients added to the pool are decorated with the ``HttpClientPoolItem`` class unless they already are an instance of this class.
114+
115+
The pool item class lets the pool be aware of the number of requests currently being processed by that client.
116+
It is also used to deactivate clients when they receive errors.
117+
118+
Deactivated clients can be reactivated after a certain amount of time, however, by default, they stay deactivated forever.
119+
To enable the behavior, wrap the clients with the ``HttpClientPoolItem`` class yourself and specify the re-enable timeout::
120+
121+
// Reactivate after 30 seconds
122+
$httpClientPool->addHttpClient(new HttpClientPoolItem($httpClient, 30));
123+
// Reactivate after each call
124+
$httpClientPool->addHttpClient(new HttpClientPoolItem($httpClient, 0));
125+
// Never reactivate the client
126+
$httpClientPool->addHttpClient(new HttpClientPoolItem($httpClient, null));
127+
128+
``HttpClientPool`` is abstract. There are three concrete implementations with specific strategies how to choose clients:
129+
130+
LeastUsedClientPool
131+
*******************
132+
133+
``LeastUsedClientPool`` choose the client with the fewest requests in progress. As it sounds the best strategy for
134+
sending a request on a pool of clients, this strategy has some limitations: :
135+
136+
- The counter is not shared between PHP process, so this strategy is not so useful in a web context, however it will make
137+
better sense in a PHP command line context.
138+
- This pool only makes sense with asynchronous clients. If you use ``sendRequest``, the call is blocking, and the pool
139+
will only ever use the first client as its request count will be 0 once ``sendRequest`` finished.
140+
141+
A deactivated client will be removed for the pool until it is reactivated, if none are available it will throw a
142+
``NotFoundHttpClientException``
143+
144+
RoundRobinClientPool
145+
********************
146+
147+
``RoundRobinClientPool`` keeps an internal pointer on the pool. At each call the pointer is moved to the next client, if
148+
the current client is disabled it will move to the next client, and if none are available it will throw a
149+
``NotFoundHttpClientException``
150+
151+
The pointer is not shared across PHP processes, so for each new one it will always start on the first client.
152+
153+
RandomClientPool
154+
****************
155+
156+
``RandomClientPool`` randomly choose an available client, throw a ``NotFoundHttpClientException`` if none are available.

spelling_word_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ multipart
2121
param
2222
params
2323
profiler
24+
PHP
2425
Puli
2526
rebase
2627
Semver

0 commit comments

Comments
 (0)