-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[12.x] Add concurrency control to Http::pool and Http::batch #57555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[12.x] Add concurrency control to Http::pool and Http::batch #57555
Conversation
|
Nice - thanks! |
|
@WendellAdriel Should the ->retry method work in this example: $responses = Http::batch(fn (Batch $batch) => [
$batch->as('first')->retry(3)->get('http://localhost/first'),
$batch->as('second')->get('http://localhost/second'),
$batch->as('third')->get('http://localhost/third'),
])->concurrency(2)->send();I might be doing something wrong but I can't get it to retry. |
|
@jonnywilliamson this weekend I'm busy, but next week I'll check it ASAP to give you some feedback. |
|
Actually I think I found a way to test it properly! Thank you for replying. |
|
@jonnywilliamson did you check if the retry is working as expected? |
|
Yes this proves it works! <?php
use Illuminate\Http\Client\Batch;
use Illuminate\Support\Facades\Http;
it('retries failing requests within a batch while concurrent', function () {
// 1. Strict Mode: Ensure only faked requests are allowed
Http::preventStrayRequests();
// 2. Setup Fakes
Http::fake([
// The 'first' URL will fail twice (500), then succeed (200)
'http://localhost/first' => Http::sequence()
->push('Server Error 1', 500)
->push('Server Error 2', 500)
->push('First Success', 200),
// All other URLs return a standard 200 OK
'http://localhost/*' => Http::response('OK', 200),
]);
// 3. Execute Batch
$responses = Http::batch(fn (Batch $batch) => [
$batch->as('first')->retry(3)->get('http://localhost/first'),
$batch->as('second')->get('http://localhost/second'),
$batch->as('third')->get('http://localhost/third'),
$batch->as('fourth')->get('http://localhost/forth'),
$batch->as('fifth')->get('http://localhost/fifth'),
])->concurrency(2)->send();
// 4. Assertions
// Verify 'first' eventually succeeded and got the correct final response
expect($responses['first']->successful())->toBeTrue();
expect($responses['first']->body())->toBe('First Success');
// Verify all other requests succeeded
expect($responses['second']->successful())->toBeTrue();
expect($responses['third']->successful())->toBeTrue();
expect($responses['fourth']->successful())->toBeTrue();
expect($responses['fifth']->successful())->toBeTrue();
// CRITICAL ASSERTION:
// We expect 7 total requests sent to the "network":
// - 3 attempts for 'first' (Fail, Fail, Success)
// - 1 attempt for 'second'
// - 1 attempt for 'third'
// - 1 attempt for 'fourth'
// - 1 attempt for 'fifth'
// = 7 total
Http::assertSentCount(7);
});Thanks! |
This PR adds the ability to control how many concurrent HTTP requests the
Http::poolandHttp::batchcan make.Http::poolExampleA new optional param should be passed in the
Http::poolcall:Http::batchExampleA new
->concurrency()method can be called before calling the->send()or->defer()methods: