Search code examples
phplaraveldockerlaravel-9laravel-sail

How to make Laravel (Sail) handle requests asynchronously


I have been having this issue on multiple devices, with both Sail and artisan serve:

I have a basic controller like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    function test($id) {
        if ($id == 1) {
            sleep(45);
        }

        return 'Hello World';
    }
}

with this in my web.php route file:

Route::controller(Controller::class)->group(function () {
    Route::get('/test/{id}', 'test');
});

Then, in Chrome I will go to localhost/test/1 and immediately go to localhost/test/2 in Firefox. In chrome, the tab will sit and wait for 45 seconds before loading as expected. However, Firefox will also wait until the Chrome script finishes before it loads. If you visit localhost/test/2 without visiting the other first, it loads instantly as expected.

What is going on? Why is one script blocking all others, and how do I fix it?


Solution

  • Add this to the .env file:

    PHP_CLI_SERVER_WORKERS=3
    

    Confirmed that it works. Don't forget to restart Sail.

    Source: https://laracasts.com/discuss/channels/servers/can-laravel-sail-only-handle-one-request-at-a-time