什麼是Actor?
Actor對於PHPer來說,可能會比較陌生,寫過Java的同學會比較熟悉,Java一直都有執行緒的概念(雖然PHP有Pthread,但不普及),它是一種非共享記憶體的併發模型,每個Actor內的資料獨立存在,Actor之間通過訊息傳遞的形式進行互動排程,且Actor是一種高度抽象化的程式設計模型,非常適合於遊戲、硬體行業。
Swoole協程與信箱
得益於Swoole4.x,我們可以基於Swoole的協程與Channel快速實現一個信箱模式排程。模擬程式碼如下:
use Swoole\Coroutine\Channel;
go(function (){
//建立十個信箱通道
$mailBoxes = [];
for ($i = 1;$i <= 10;$i++){
$mailBoxes[$i] = new Channel(16);
}
//模擬master 郵局排程,隨機像一個信箱投遞訊息
go(function ()use($mailBoxes){
while (1){
\co::sleep(2);
$key = rand(1,10);
($mailBoxes[$key])->push(time());
}
});
//模擬actor 實體消費
for ($i = 1;$i <= 10;$i++){
go(function ()use($mailBoxes,$i){
while (1){
$msg = ($mailBoxes[$i])->pop();
echo "Actor {$i} recv msg : {$msg} \n";
}
});
}
});
以上程式碼執行輸出:
php test.php
Actor 8 recv msg : 1559622691
Actor 10 recv msg : 1559622693
Actor 1 recv msg : 1559622695
Actor 5 recv msg : 1559622697
協程通道每次在POP遇到無資料的時候,都會自動讓出執行權(具體可以去看Swoole協程排程)
Actor庫
基於上面的原理,我們實行了一個多程式分佈的協程Actor庫
composer require easyswoole/actor=2.x-dev
我們依賴dev庫進行測試,生產可以自己依賴stable版本
程式關係
Easyswoole的Actor模型中,存在兩組程式,一組是proxy程式,用來實現Actor對外服務,一組是worker程式,proxy程式與worker程式之間通過unixsock進行通訊,而Actor例項就均勻的分佈worker之中。
樣例程式碼
比如在一個聊天室中,我們可以定義一個房間模型。
namespace EasySwoole\Actor\Test;
use EasySwoole\Actor\AbstractActor;
use EasySwoole\Actor\ActorConfig;
class RoomActor extends AbstractActor
{
public static function configure(ActorConfig $actorConfig)
{
$actorConfig->setActorName('Room');
}
public function onStart()
{
//每當一個RoomActor實體被建立的時候,都會執行該回撥
var_dump('room actor '.$this->actorId().' start');
}
public function onMessage($msg)
{
//每當一個RoomActor實體收到外部訊息的時候,都會執行該回撥當
var_dump('room actor '.$this->actorId().' onmessage: '.$msg);
return 'reply at '.time();
}
public function onExit($arg)
{
//每當一個RoomActor實體退出的時候,都會執行該回撥
var_dump('room actor '.$this->actorId().' exit at arg: '.$arg);
return 'exit at '.time();
}
protected function onException(\Throwable $throwable)
{
//每當一個RoomActor出現異常的時候,都會執行該回撥
var_dump($throwable->getMessage());
}
}
在cli模式下建立一個Actor服務
use EasySwoole\Actor\Actor;
use EasySwoole\Actor\Test\RoomActor;
use EasySwoole\Actor\ProxyProcess;
Actor::getInstance()->register(RoomActor::class);
$list = Actor::getInstance()->generateProcess();
foreach ($list['proxy'] as $proxy){
/** @var ProxyProcess $proxy */
$proxy->getProcess()->start();
}
foreach ($list['worker'] as $actors){
foreach ($actors as $actorProcess){
/** @var ProxyProcess $actorProcess */
$actorProcess->getProcess()->start();
}
}
while($ret = \Swoole\Process::wait()) {
echo "PID={$ret['pid']}\n";
}
建立一個cli測試指令碼
use EasySwoole\Actor\Actor;
use EasySwoole\Actor\Test\RoomActor;
Actor::getInstance()->register(RoomActor::class);
go(function (){
$actorId = RoomActor::client()->create('create arg1');
var_dump($actorId);
\co::sleep(3);
var_dump(RoomActor::client()->send($actorId,'this is msg'));
\co::sleep(3);
var_dump(RoomActor::client()->exit($actorId,'this is exit arg'));
\co::sleep(3);
RoomActor::client()->create('create arg2');
\co::sleep(3);
RoomActor::client()->create('create arg3');
\co::sleep(3);
var_dump(RoomActor::client()->sendAll('sendAll msg'));
\co::sleep(3);
var_dump(RoomActor::client()->status());
\co::sleep(3);
var_dump(RoomActor::client()->exitAll('sendAll exit'));
});
以上程式碼執行結果如下:
服務端
php test.php
string(40) "room actor 00101000000000000000001 start"
string(57) "room actor 00101000000000000000001 onmessage: this is msg"
string(64) "room actor 00101000000000000000001 exit at arg: this is exit arg"
string(40) "room actor 00101000000000000000002 start"
string(40) "room actor 00103000000000000000001 start"
string(57) "room actor 00101000000000000000002 onmessage: sendAll msg"
string(57) "room actor 00103000000000000000001 onmessage: sendAll msg"
string(60) "room actor 00101000000000000000002 exit at arg: sendAll exit"
string(60) "room actor 00103000000000000000001 exit at arg: sendAll exit"
客戶端
php test2.php
string(23) "00101000000000000000001"
string(19) "reply at 1559623925"
string(18) "exit at 1559623928"
bool(true)
array(3) {
[1]=>
int(1)
[2]=>
int(0)
[3]=>
int(1)
}
bool(true)
更多細節可以在EasySwoole專案官網得到文件支援 http://easyswoole.com/
喜歡EasySwoole專案的,可以給個star https://github.com/easy-swool...