Swoole
Swoole - 面向生产环境的 PHP 异步网络通信引擎
Swoole is an event-driven asynchronous & coroutine-based concurrency networking communication engine with high performance written in C and C++ for PHP.
https://github.com/grpc/grpc/issues/1304
PHP Extensionで実装されている
ref PHPでgRPCってどこまでいけるの?
アーキテクチャ
https://qiita.com/prograti/items/e29210300232e4a3d8b7#swoole
かなり詳しい記事
PHP-FPMと違い、1プロセスで処理をしている
この処理のため、PHPのリクエストサイクルにおいてSwooleを使うと実行されないところで何が起きているのか抑えておく必要がある(普段意識しないところに罠がある)
Reactor(Reactorパターンがメンタルモデル)
リクエストを監視して処理可能ならdispatcheに投げる(Demultiplexer)
eventの種類ごとにevent handlerに移譲(Dispatch)
worker
manager
プロセス数が少ないのでメモリ消費が小さい
コンテキストスイッチによるオーバーヘッドがない
Swoole以前にできなかったPHPで実現できるようになったこと
非同期処理
HTTP/2サーバー
WebSocketサーバー