laravel / octane

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

Gates do not work in `Octane::concurrently()` #431

Closed smortexa closed 2 years ago

smortexa commented 2 years ago

Description:

The Laravel authorization services do not work in Octane::concurrently()

Steps To Reproduce:

Just try:

// FooServiceProvider.php
Gate::define('edit', fn(User $user) => true);

// BarController.php
$permission1 = Gate::allows('edit'));

[$permission2] = Octane::concurrently([
     fn () => Gate::allows('edit'),
]);

dd($permission1, $permission2);  // true, false
driesvints commented 2 years ago

Octane's concurrent requests are separate processes. As such, there's no auth state or any "http" state at all. Please see the docs here: https://laravel.com/docs/8.x/octane#concurrent-tasks

Screenshot 2021-12-06 at 11 32 20

marol1210 commented 2 years ago

@driesvints What workerState->work is when register request callback , workerState->worker will not assigned in this closure . use ($server, $workerState, $serverState) , $workerState is just inherit variables from the parent scope.

  1. include WorkerState
    
    require_once __DIR__.'/WorkerState.php';

$workerState = new WorkerState;

$workerState->cacheTable = require DIR.'/createSwooleCacheTable.php'; $workerState->timerTable = $timerTable; $workerState->tables = require DIR.'/createSwooleTables.php';


2. $workerState is instance of WorkerState

$server->on('request', function ($request, $response) use ($server, $workerState, $serverState) { $workerState->lastRequestTime = microtime(true);

if ($workerState->timerTable) {
    $workerState->timerTable->set($workerState->workerId, [
        'worker_pid' => $workerState->workerPid,
        'time' => time(),
    ]);
}

$workerState->worker->handle(...$workerState->client->marshalRequest(new RequestContext([
    'swooleRequest' => $request,
    'swooleResponse' => $response,
    'publicPath' => $serverState['publicPath'],
    'octaneConfig' => $serverState['octaneConfig'],
])));

if ($workerState->timerTable) {
    $workerState->timerTable->del($workerState->workerId);
}

});