I’m working in two projects, a Laravel 8 project and a Laravel Lumen 8 project, both of which use Guzzle to make HTTP requests, they’re running on the following domains:
- Laravel 8 http://localhost:8000
- Laravel Lumen 8 http://localhost:8001
And I’m using MAMP Pro (Apache and MySQL).
My Laravel 8 project makes a HTTP request to my Lumen project, which makes a HTTP request back to my Laravel 8 project, the problem I’m facing is that the first request from my Laravel project always times out and then the request starts from the Lumen project.
This isn’t what I want, I need the first request to instantly start the request in my Lumen project and have it return a response back to the very first request.
What am I missing from my HTTP request structure to allow this to happen? I’ve tried utilising sessions in the db as I thought it might be session blocking or queuing the requests:
Laravel 8 project’s request (first)
/**
* Route the microservice
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function microservice(Request $request, $service)
{
Log::debug("HUB Microservice");
// TODO: this is always timing out regardless of what timeout I set
// seems to only start the Lumen request when this finishes but I need
// the response from the Lumen's request to be here
$response = Http::timeout(20)->get('http://localhost:8001/api/reports');
// the response from the microservice on the Lumen project
return response()->json($response->json(), $response->status());
}
Laravel Lumen 8 project’s request (second, needs to return the response back to the first)
<?php
namespace AppHttpMiddleware;
use Closure;
use GuzzleHttpClient;
class BeforeMiddleware
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $ability)
{
$client = new Client([
'base_uri' => 'http://localhost:8000',
'timeout' => 60
]);
// TODO: this request only ever starts when my Laravel (first) request
// times out, is it domain related?
$res = $client->request('POST', '/api/hub/login', [
'form_params' => [
'key' => 'value'
]
]);
// Post-Middleware Action
return $next($request);
}
}
What am I missing
2
Answers
It’s because of your server configuration. On windows php-cgi cant handle several connections simultaneously. So before start second script, php-cgi.exe waits until first is end.
You can use build-in php server (
php -S
) which allows you to handle connections to8000
and8001
separatelyI’m using Laragon (nginx) for local development.
I fixed it by raising Upstream from 2 to 5.
It can be increased in this file:
C:laragonusrlaragon.ini
laragon.ini