Giter VIP home page Giter VIP logo

actor's Introduction

Actor

提供Actor模式支持,助力游戏行业开发。EasySwoole的Actor采用自定义process作为存储载体,以协程作为最小调度单位,利用协程Channel做mail box,而客户端与process之间的通讯,采用UnixSocket实现。

测试代码

服务端-SwooleServer模式


服务端-Process模式

use EasySwoole\Actor\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'));

});

注意请基于协程实现,不要在actor中写阻塞代码,否则效率会非常差。其次使用虚拟机,docker等方式开发,不能在共享文件夹使用,因为unixsock 无法在共享目录中正确读写,请修改tempDir临时目录,把unxisock文件挂载在非共享目录即可.

内存问题

Actor数据分散在进程内,一个进程可能需要占用很大的内存,因此请根据实际业务量配置内存大小。

actor's People

Contributors

evalor avatar kiss291323003 avatar tioncico avatar xiaocaigua avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.