Concurrent requests
Independent requests do not have to wait for each other. sendAll() sends them at the same
time, so the total time is that of the slowest one, not the sum.
sendAll
$responses = $client->sendAll([
'user' => $client->get('users/42'),
'orders' => $client->get('orders', ['user' => 42]),
'stats' => $client->get('stats/42'),
]);
$user = $responses['user']->throw()->json();
$orders = $responses['orders']->json();- Responses come back with the same keys and in the same order as the requests. Keys can be anything: strings, integers.
- Every request goes through the whole chain: middleware, retries, redirects,
sinkTo(),onProgress(). - A 4xx/5xx is a response and is in the result, just as with
send().
How many at once
By default no more than 16 requests run at the same time. The rest wait for a free slot.
$client->withConcurrency(32); // for the client
$client->sendAll($requests, concurrency: 4); // for one callThe limit protects both the server you are calling and your own process.
A generator instead of an array
Requests can be supplied by a generator. It is read as sending goes on, so a list of 10,000 requests never has to be held in memory whole:
$requests = (function () use ($client, $ids) {
foreach ($ids as $id) {
yield $id => $client->get("users/{$id}");
}
})();
foreach ($client->sendAll($requests, concurrency: 8) as $id => $response) {
// ...
}The responses are still collected into an array and returned at the end.
Failures
If some requests got no response (network, timeout), sendAll() waits for all the others and
only then throws a BatchException:
use Flytachi\Winter\Cast\Exception\BatchException;
try {
$responses = $client->sendAll($requests);
} catch (BatchException $e) {
$responses = $e->responses(); // the responses received — under the requests' keys
$failures = $e->failures(); // key => exception
}One failed request does not cancel the others: the responses received are not lost.
How it works
The calling code is the same everywhere. The mechanism is chosen by itself:
| Where it is called | How it runs |
|---|---|
| inside a Swoole coroutine | each request is a coroutine of its own; cURL does not block the worker thanks to Swoole’s hooks |
| PHP-FPM, CLI, Swoole outside a coroutine | each request is a Fiber of its own; when a request waits for the network, its handle joins a shared curl_multi and the Fiber is suspended |
Fibers are there so that middleware and retries stay ordinary synchronous code:
$next($request) returns a response, while the waiting on the network is shared. Pauses
between retries do not block the other requests either.
More in Transport and connection pool.
Inside coroutines without sendAll
In Swoole you can also just start coroutines, each with its own send(). One client is safely
shared by all of them: it is immutable, and every request takes a separate connection.
use Swoole\Coroutine;
use Swoole\Coroutine\WaitGroup;
$group = new WaitGroup();
foreach ($ids as $id) {
$group->add();
Coroutine::create(function () use ($client, $id, $group) {
$client->get("users/{$id}")->send();
$group->done();
});
}
$group->wait();