Я пробую запустить следующий код:
https://gist.github.com/fgilio/7bc0d5dce3f1b3670f2...
Мои действия:
PendingConcurrentPool:
<?php
namespace App\Http\Controllers;
use Closure;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Collection;
class PendingConcurrentPool extends Controller
{
private Closure $requestsBuilder;
private int $concurrency = 10;
public function __construct(Closure $requestsBuilder)
{
$this->requestsBuilder = $requestsBuilder;
}
public function concurrency(int $amount): self
{
$this->concurrency = $amount;
return $this;
}
public function wait(): Collection
{
$responses = collect();
$pool = new Pool(new Client(), call_user_func($this->requestsBuilder), [
'concurrency' => $this->concurrency,
'fulfilled' => function (Response $response, $index) use ($responses) {
$responses[$index] = new \Illuminate\Http\Client\Response($response);
},
'rejected' => function (RequestException $reason, $index) use ($responses) {
$responses[$index] = new \Illuminate\Http\Client\Response($reason->getResponse());
},
]);
$pool->promise()->wait();
return $responses;
}
}
testSendsConcurrentRequests:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
class HttpClientController extends Controller
{
public function testSendsConcurrentRequests()
{
$responses = Http::pool(function () {
return yield from [
'req-1' => new \GuzzleHttp\Psr7\Request('GET', 'http://localhost:8000/test/req-1'),
'req-2' => new \GuzzleHttp\Psr7\Request('GET', 'http://localhost:8001/test/req-2'),
'req-3' => new \GuzzleHttp\Psr7\Request('GET', 'http://localhost:8002/test/req-3'),
'req-4' => new \GuzzleHttp\Psr7\Request('GET', 'http://localhost:8003/test/req-4'),
];
})->concurrency(4)->wait();
dump($responses->map->json());
}
}
PendingRequest:
<?php
namespace App\Providers;
use Closure;
use App\Http\Controllers\PendingConcurrentPool;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\ServiceProvider;
class MacroServiceProvider extends ServiceProvider
{
public function register()
{
PendingRequest::macro('pool', function (Closure $requestsBuilder) {
return new PendingConcurrentPool($requestsBuilder);
});
}
}
Провайдера зарегистрировал в:
config app -> 'providers' => [
App\Providers\MacroServiceProvider::class,
При переходе на страницу получаю ошибку:
Call to a member function concurrency() on array
})->concurrency(4)->wait();
В чем ошибка?