laravelconf 2018《优雅与效能,laravel 与 swoole 整合之路》笔记 | laravel china 社区-大发黄金版app下载
在线视频
- 地址1:
- 地址2:
laravel为什么慢
- 一次请求差不多要加载200多个php,当然也可以用opcache
- 每次请求结束,所有资源都被销毁,不能重复利用。下面有一个laravel的请求生命周期图:
和swoole整合的方案
方案1
把swoole当php-fpm一样使用,每次处理请求都加载一下laravel核心,其实还是不能避免每次都加载200多个php,代码如下:
public function onrequest($swoolerequest, $swooleresponse) {
$app = require_once __dir__.'/bootstrap/app.php';
$kernel = $app->make(illuminate\contracts\http\kernel::class);
$illuminaterequest = request::make($swoolerequest)->toilluminate();
$illuminateresponse = $kernel->handle($illuminaterequest);
$response->end($illuminateresponse->getcontent());
$kernel->terminate($illuminaterequest, $illuminateresponse);
}
方案2
公用一个laravel容器,代码如下:
public function onrequest($swoolerequest, $swooleresponse) {
$app = $this->getapplication();
$kernel = $app->make(illuminate\contracts\http\kernel::class);
$illuminaterequest = request::make($swoolerequest)->toilluminate();
$illuminateresponse = $kernel->handle($illuminaterequest);
$response->end($illuminateresponse->getcontent());
$kernel->terminate($illuminaterequest, $illuminateresponse);
}
这个方案对比方案1,可以不用重复加载php了,laravel的请求生命周期图如下:
这个方案的问题是如果a用户登录了,b用户访问swoole同一个worker的时候也是登录状态,因为authmanager是单例,同一个进程是数据共享的。实际上,单例、全局变量、静态属性也都是数据共享的。这样就造成数据安全问题了。
方案3
基于方案2,每次获取容器实例后,清理掉单例对象,理论上可行,但是太繁琐了
方案4
基于方案2,第一次laravel容器初始化并注册服务提供者和实例后,就克隆一个干净的对象sandbox。每次请求都基于sandbox做后续的处理,sandbox每次都会reset一下。
这个方案的laravel的请求生命周期图如下:
此方案的github地址是:
本作品采用《cc 协议》,转载必须注明作者和本文链接
好文章,赞一下。