hhxsv5 / laravel-s

LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.
MIT License
3.82k stars 470 forks source link

$request->user()在生产过程中获取值为null。 #471

Open furleywolf opened 4 months ago

furleywolf commented 4 months ago
  1. Your software version (Screenshot of your startup)

    Software Version
    PHP 8.2.19
    Swoole 5.1.0
    Laravel/Lumen 9.52.16
  2. Detail description about this issue(error/log)

    在使用了\Laravel\Passport\Http\Middleware\CheckScopes后,初始调用正常,调用10余次会因为$request->user()为空出现异常。不是很清楚为什么swoole启动的时候是正常的,看了issue,都是在第二次获取不到,我是在第十次,很怪。

  3. Some reproducible code blocks and steps

    public function handle($request, $next, ...$scopes)
    {
        //此处会出现$request->user()  = null。
        if (! $request->user() || ! $request->user()->token()) {
            throw new AuthenticationException;
        }
    
        foreach ($scopes as $scope) {
            if (! $request->user()->tokenCan($scope)) {
                throw new MissingScopeException($scope);
            }
        }
    
        return $next($request);
    }

    已加清理器,已配置中间件顺序,在执行到目标中间件时用户已登陆,但仍无法获取$request->user()。

    `

    Hhxsv5\LaravelS\Illuminate\Cleaners\SessionCleaner::class, // If you use the session/authentication in your project, please uncomment this line Hhxsv5\LaravelS\Illuminate\Cleaners\AuthCleaner::class, // If you use the authentication/passport in your project, please uncomment this line Hhxsv5\LaravelS\Illuminate\Cleaners\JWTCleaner::class, // If you use the package "tymon/jwt-auth" in your project, please uncomment this line

    `

已通过不同的终端测试,重启/重载后,所有的终端(用户)都是正常的,共同请求10次左右后所有的都不正常。感觉和其他的几个issue很像,可以通过Auth::user()解决,但是很多包里的中间件要拎出来重写,感觉不太好。

444 #413

hhxsv5 commented 3 weeks ago

将worker_num设置为1来测试看看

cyamz commented 6 days ago

贴子里“请求10次”的描述应该是worker用完,我测试使用30,每次4个,第8次出现2条未找到报错,后续全部报错。改为worker_num=1后,第1次出现3条报错。

max_request=1 可以解决,但性能较差。

最初使用Auth::user()也有同样问题,后发现是cleaners Hhxsv5\LaravelS\Illuminate\Cleaners\AuthCleaner::class 未更新到线上。(折腾很久,脑子懵了)

本着同理的想法,又尝试使用 Hhxsv5\LaravelS\Illuminate\Cleaners\RequestCleaner::class 配合 $request->user()无法解决

Component Version
PHP 8.2.16
Swoole 5.1.0
LaravelS 3.8.2
Laravel Framework [production] 10.48.20

使用的 laravel/sanctum,未加JWT清理器(加了也一样)。

将worker_num设置为1来测试看看