Cakephp AuthenticationMiddleware.php中的process()函数有时花费的时间太长

Cakephp AuthenticationMiddleware.php中的process()函数有时花费的时间太长,cakephp,Cakephp,有些页面需要简单的重新加载,有时需要很长的时间(4到45秒)。不幸的是,延迟是不可预测的。首先我认为这当然是我做错了,但经过进一步的调查,我发现了延迟函数 它位于vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php函数process()运行此代码$service->authenticate($request) 以下是我的执行时间度量中的一些示例转储: 执行时间长的/api/shop转储;向右滚动查看

有些页面需要简单的重新加载,有时需要很长的时间(4到45秒)。不幸的是,延迟是不可预测的。首先我认为这当然是我做错了,但经过进一步的调查,我发现了延迟函数


它位于
vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php
函数
process()
运行此代码
$service->authenticate($request)


以下是我的执行时间度量中的一些示例转储:

执行时间长的
/api/shop
转储;向右滚动查看每次快照之间的增量时间:

[2020-11-01T16:58:02+00:00] /api/shop/ - /vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php :0103 - Delta time:0.04342
[2020-11-01T16:58:14+00:00] /api/shop/ - /vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php :0107 - Delta time:11.81272
[2020-11-01T16:58:14+00:00] /api/shop/ - /vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php :0121 - Delta time:11.81295  
然后,17秒后[没有更改,只是重新加载页面]没有延迟:

[2020-11-01T16:58:31+00:00] /api/shop/ - /vendor/cakephp/cakephp/src/Http/Runner.php                                :0071 - Delta time:0.03756
[2020-11-01T16:58:31+00:00] /api/shop/ - /vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php :0103 - Delta time:0.04211
[2020-11-01T16:58:31+00:00] /api/shop/ - /vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php :0107 - Delta time:0.04506

你知道原因是什么吗?这非常令人沮丧,因为它完全不可预测。正如上面所示的真实示例,在几秒钟内出现了巨大的延迟,然后它工作了3-4次,然后突然再次延迟。如前所述,我没有做任何事情;只是重新加载页面


CakePHP4.1.4

我也在下面发布了这个案例。CakePHP成员建议我检查
Authentication\Authenticator\SessionAuthenticator
,我在这里找到了原因:

vendor/cakephp/cakephp/src/Http/Session.php

-->在函数中
start()

-->如果(!session_start()){

完整代码:

    if (!session_start()) {
        throw new RuntimeException('Could not start the session');
    }

然后解决方案非常简单。我正在使用SSE,这就是这些不可预测延迟的原因。调用
session\u write\u close()后
在SSE函数中,页面加载需要深入挖掘,该方法背后可能有大量处理。我会连接一个类似Xdebug的调试器来生成适当的分析信息,显示时间的确切位置。也就是说,在黑暗中拍摄,偶尔的延迟通常是由模式缓存(re)造成的填充(当信息模式非常混乱时)。锁定会话也有些常见。
    if (!session_start()) {
        throw new RuntimeException('Could not start the session');
    }