|
11 | 11 | - [Guzzle Middleware](#guzzle-middleware)
|
12 | 12 | - [Guzzle Options](#guzzle-options)
|
13 | 13 | - [Concurrent Requests](#concurrent-requests)
|
| 14 | + - [Request Pooling](#request-pooling) |
| 15 | + - [Request Batching](#request-batching) |
14 | 16 | - [Macros](#macros)
|
15 | 17 | - [Testing](#testing)
|
16 | 18 | - [Faking Responses](#faking-responses)
|
@@ -500,6 +502,9 @@ public function boot(): void
|
500 | 502 |
|
501 | 503 | Sometimes, you may wish to make multiple HTTP requests concurrently. In other words, you want several requests to be dispatched at the same time instead of issuing the requests sequentially. This can lead to substantial performance improvements when interacting with slow HTTP APIs.
|
502 | 504 |
|
| 505 | +<a name="request-pooling"></a> |
| 506 | +### Request Pooling |
| 507 | + |
503 | 508 | Thankfully, you may accomplish this using the `pool` method. The `pool` method accepts a closure which receives an `Illuminate\Http\Client\Pool` instance, allowing you to easily add requests to the request pool for dispatching:
|
504 | 509 |
|
505 | 510 | ```php
|
@@ -552,6 +557,71 @@ $responses = Http::pool(fn (Pool $pool) => [
|
552 | 557 | ]);
|
553 | 558 | ```
|
554 | 559 |
|
| 560 | +<a name="request-batching"></a> |
| 561 | +### Request Batching |
| 562 | + |
| 563 | +Another way of working with concurrent requests in Laravel is to use the `batch` method. Like the `pool` method, it accepts a closure which receives an `Illuminate\Http\Client\Batch` instance, allowing you to easily add requests to the request pool for dispatching, but it also allows you to define completion callbacks: |
| 564 | + |
| 565 | +```php |
| 566 | +use Illuminate\Http\Client\Batch; |
| 567 | +use Illuminate\Http\Client\RequestException; |
| 568 | +use Illuminate\Http\Client\Response; |
| 569 | +use Illuminate\Support\Facades\Http; |
| 570 | + |
| 571 | +$responses = Http::batch(fn (Batch $batch) => [ |
| 572 | + $batch->get('http://localhost/first'), |
| 573 | + $batch->get('http://localhost/second'), |
| 574 | + $batch->get('http://localhost/third'), |
| 575 | +])->before(function (Batch $batch) { |
| 576 | + // The batch has been created but no requests have been initialized... |
| 577 | +})->progress(function (Batch $batch, int|string $key, Response $response) { |
| 578 | + // An individual request has completed successfully... |
| 579 | +})->then(function (Batch $batch, array $results) { |
| 580 | + // All requests completed successfully... |
| 581 | +})->catch(function (Batch $batch, int|string $key, Response|RequestException $response) { |
| 582 | + // First batch request failure detected... |
| 583 | +})->finally(function (Batch $batch, array $results) { |
| 584 | + // The batch has finished executing... |
| 585 | +})->send(); |
| 586 | +``` |
| 587 | + |
| 588 | +Like the `pool` method, you can use the `as` method to name your requests: |
| 589 | + |
| 590 | +```php |
| 591 | +$responses = Http::batch(fn (Batch $batch) => [ |
| 592 | + $batch->as('first')->get('http://localhost/first'), |
| 593 | + $batch->as('second')->get('http://localhost/second'), |
| 594 | + $batch->as('third')->get('http://localhost/third'), |
| 595 | +])->send(); |
| 596 | +``` |
| 597 | + |
| 598 | +After a `batch` is started by calling the `send` method, you can't add new requests to it. Trying to do so will result in a `Illuminate\Http\Client\BatchInProgressException` exception being thrown. |
| 599 | + |
| 600 | +<a name="inspecting-batches"></a> |
| 601 | +#### Inspecting Batches |
| 602 | + |
| 603 | +The `Illuminate\Http\Client\Batch` instance that is provided to batch completion callbacks has a variety of properties and methods to assist you in interacting with and inspecting a given batch of requests: |
| 604 | + |
| 605 | +```php |
| 606 | +// The number of requests assigned to the batch... |
| 607 | +$batch->totalRequests; |
| 608 | + |
| 609 | +// The number of requests that have not been processed yet... |
| 610 | +$batch->pendingRequests; |
| 611 | + |
| 612 | +// The number of requests that have failed... |
| 613 | +$batch->failedRequests; |
| 614 | + |
| 615 | +// The number of requests that have been processed thus far... |
| 616 | +$batch->processedRequests(); |
| 617 | + |
| 618 | +// Indicates if the batch has finished executing... |
| 619 | +$batch->finished(); |
| 620 | + |
| 621 | +// Indicates if the batch has request failures... |
| 622 | +$batch->hasFailures(); |
| 623 | +``` |
| 624 | + |
555 | 625 | <a name="macros"></a>
|
556 | 626 | ## Macros
|
557 | 627 |
|
|
0 commit comments