laravel / octane

Supercharge your Laravel application's performance.
https://laravel.com/docs/octane
MIT License
3.74k stars 291 forks source link

Request own interface timed out. #528

Closed peibinzhu closed 2 years ago

peibinzhu commented 2 years ago

Description:

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.

Steps To Reproduce:

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!"}
driesvints commented 2 years ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.