I have raised an issue #528.
We are going to use microservices to request in the project. After the client initiates the request, the server requests the interface of the same project through the local area network http, but it is incredible to find that the request has timed out.
I used the tool to test the service interface and it did not time out, but the code request timed out.
First write a mock request in the controller.
app/Http/Controllers/Controller.php
<?php
namespace App\Http\Controllers;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Response;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/**
* Simulate login interface.
* Request the login service interface on the login interface.
*
* @return JsonResponse
* @throws GuzzleException
*/
public function login(): JsonResponse
{
$client = new Client();
$response = $client->post(
'http://127.0.0.1:8000/api/loginService',
[RequestOptions::TIMEOUT => 5]
);
$content = (string)$response->getBody();
return Response::json(json_decode($content, true));
}
/**
* Simulate remote login service interface.
*
* @return JsonResponse
*/
public function loginService(): JsonResponse
{
return Response::json(['status' => 200, 'message' => 'Login successful!']);
}
}
Register route.
routes/api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/login', [\App\Http\Controllers\Controller::class, 'login']);
Route::post('/loginService', [\App\Http\Controllers\Controller::class, 'loginService']);
Run command.
php artisan octane:start
Using octane default port 8000
, the client requests http://127.0.0.1:8000/api/login
.
curl -X POST http://127.0.0.1:8000/api/login
Output:
GuzzleHttp\Exception\ConnectException: cURL error 28: Operation timed out after 5002 milliseconds with 0 out of -1 bytes received (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://127.0.0.1:8000/api/loginService in file /mnt/hgfs/www/laravel/laravel9-issue/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 210
A separate request to http://127.0.0.1:8000/api/loginService
can return data normally.
curl -X POST http://127.0.0.1:8000/api/loginService
Output:
{"status":200,"message":"Login successful!"}
I suspect that initiating another request in one request will cause blocking, causing another request to block, hence the timeout.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community