Skip to content

Commit 4c6adad

Browse files
authored
Merge pull request #134 from php-http/feature/http-client-pool
Add documention on http client pool
2 parents 8d1316c + c48aa85 commit 4c6adad

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

components/client-common.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,74 @@ 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+
The pool item class lets the pool be aware of the number of requests currently being processed by that client.
115+
It is also used to deactivate clients when they receive errors.
116+
Deactivated clients can be reactivated after a certain amount of time, however, by default, they stay deactivated forever.
117+
To enable the behavior, wrap the clients with the ``HttpClientPoolItem`` class yourself and specify the re-enable timeout::
118+
119+
// Reactivate after 30 seconds
120+
$httpClientPool->addHttpClient(new HttpClientPoolItem($httpClient, 30));
121+
// Reactivate after each call
122+
$httpClientPool->addHttpClient(new HttpClientPoolItem($httpClient, 0));
123+
// Never reactivate the client (default)
124+
$httpClientPool->addHttpClient(new HttpClientPoolItem($httpClient, null));
125+
126+
``HttpClientPool`` is abstract. There are three concrete implementations with specific strategies on how to choose clients:
127+
128+
LeastUsedClientPool
129+
*******************
130+
131+
``LeastUsedClientPool`` choose the client with the fewest requests in progress. As it sounds the best strategy for
132+
sending a request on a pool of clients, this strategy has some limitations: :
133+
134+
- The counter is not shared between PHP process, so this strategy is not so useful in a web context, however it will make
135+
better sense in a PHP command line context.
136+
- This pool only makes sense with asynchronous clients. If you use ``sendRequest``, the call is blocking, and the pool
137+
will only ever use the first client as its request count will be 0 once ``sendRequest`` finished.
138+
139+
A deactivated client will be removed for the pool until it is reactivated, 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``
148+
149+
The pointer is not shared across PHP processes, so for each new one it will always start on the first client.
150+
151+
RandomClientPool
152+
****************
153+
154+
``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)