spiral / roadrunner-http
RoadRunner:HTTP 和 PSR-7 工作进程
v3.5.1
2024-04-26 11:16 UTC
Requires
- php: >=8.1
- ext-json: *
- psr/http-factory: ^1.0.1
- psr/http-message: ^1.0.1 || ^2.0
- roadrunner-php/roadrunner-api-dto: ^1.6
- spiral/roadrunner: ^2023.3 || ^2024.1
- spiral/roadrunner-worker: ^3.5
- symfony/polyfill-php83: ^1.29
Requires (Dev)
- jetbrains/phpstorm-attributes: ^1.0
- nyholm/psr7: ^1.3
- phpunit/phpunit: ^10.0
- symfony/process: ^6.2 || ^7.0
- vimeo/psalm: ^5.9
Suggests
- ext-protobuf: Provides Protocol Buffers support. Without it, performance will be lower.
- spiral/roadrunner-cli: Provides RoadRunner installation and management CLI tools
README
RoadRunner 是一个开源的(MIT 许可)高性能 PHP 应用程序服务器、负载均衡器和进程管理器。它支持作为服务运行,并能够根据每个项目的基础扩展其功能。
RoadRunner 包括兼容 PSR-7/PSR-17 的 HTTP 和 HTTP/2 服务器,可以用于替换经典的 Nginx+FPM 设置,以实现更高的性能和灵活性。
仓库
此仓库包含 PSR-7 PHP 工作进程的代码库。要获取应用程序服务器,请查看 spiral/roadrunner。
要求
请确保您的服务器已配置以下 PHP 版本和扩展
- PHP >=8.1
- ext-protobuf:此扩展是可选的,但强烈建议安装。如果没有它,性能可能低达 50%。
- RoadRunner ^2023.3
安装
要安装应用程序服务器和 HTTP 代码库
composer require spiral/roadrunner-http nyholm/psr7
您可以使用方便的安装程序下载最新可用的兼容版本 RoadRunner 组装
composer require spiral/roadrunner-cli --dev
要下载应用程序服务器的最新版本
vendor/bin/rr get
您可以使用任何 PSR-17 兼容的实现。
示例
初始化抽象 RoadRunner 工作进程
<?php require __DIR__ . '/vendor/autoload.php'; use Nyholm\Psr7\Response; use Nyholm\Psr7\Factory\Psr17Factory; use Spiral\RoadRunner\Worker; use Spiral\RoadRunner\Http\PSR7Worker; // Create new RoadRunner worker from global environment $worker = Worker::create(); // Create common PSR-17 HTTP factory $factory = new Psr17Factory(); // // Create PSR-7 worker and pass: // - RoadRunner worker // - PSR-17 ServerRequestFactory // - PSR-17 StreamFactory // - PSR-17 UploadFilesFactory // $psr7 = new PSR7Worker($worker, $factory, $factory, $factory); while (true) { try { $request = $psr7->waitRequest(); } catch (\Throwable $e) { // Although the PSR-17 specification clearly states that there can be // no exceptions when creating a request, however, some implementations // may violate this rule. Therefore, it is recommended to process the // incoming request for errors. // // Send "Bad Request" response. $psr7->respond(new Response(400)); continue; } try { // Here is where the call to your application code will be located. // For example: // // $response = $app->send($request); // // Reply by the 200 OK response $psr7->respond(new Response(200, [], 'Hello RoadRunner!')); } catch (\Throwable $e) { // In case of any exceptions in the application code, you should handle // them and inform the client about the presence of a server error. // // Reply by the 500 Internal Server Error response $psr7->respond(new Response(500, [], 'Something Went Wrong!')); // Additionally, we can inform the RoadRunner that the processing // of the request failed. $worker->error((string)$e); } }
流响应
要在流中发送响应,请将 PSR7Worker
中的 $chunkSize
属性设置为。这将 PSR7Worker 将响应切割成 512KB 的大小并逐个发送到流中。
$psr7 = new PSR7Worker($worker, $factory, $factory, $factory); $psr7->chunkSize = 512 * 1024; // 512KB
现在 PSR7Worker 将响应切割成 512KB 的大小并逐个发送到流中。
早期提示
要发送多个响应,您可以使用具有 endOfStream
参数设置为 false
的 \Spiral\RoadRunner\Http\HttpWorker::respond()
方法。这将发送响应到客户端并允许您发送其他响应。
/** @var \Spiral\RoadRunner\Http\PSR7Worker $psr7 */ $httpWorker = $psr7->getHttpWorker() ->respond(103, header: ['Link' => ['</style.css>; rel=preload; as=style']], endOfStream: false); // End of stream will be sent automatically after PSR7Worker::respond() call $psr7->respond(new Response(200, [], 'Hello RoadRunner!'));
测试
此代码库通过宿主仓库自动测试 - spiral/roadrunner。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 LICENSE
。由 Spiral Scout 维护。