hisorange/posix-rpc

PHP 编写的易于使用 RPC 库,通过 Posix 队列进行消息传递。

1.0.0 2020-02-23 21:26 UTC

This package is auto-updated.

Last update: 2024-08-24 07:16:30 UTC


README

Latest Stable Version Build Status Coverage Status Total Downloads License

易于使用的库,用于处理您需要的 IPC(进程间通信)!它提供了一个消息接口,用于发布/订阅模式,还支持安全的 RPC(远程过程调用)。这还不是全部!使用直观的语法,您可以以 同步异步 方式,通过非阻塞传输层调用处理器。

但是,Posix 队列难道不是又小又慢吗?!不!这个库可以将千兆大小的消息推送到队列中,并且可以轻松达到每秒 30k 条消息 ^.^

让我们开始吧!

如何安装

composer require hisorange/posix-rpc

是的,它已经准备好供您使用了!^.^

如何使用

无需大设置,只需使用一个唯一的名称初始化节点

use hisorange\PosixRPC\Node;
use hisorange\PosixRPC\Contract\IMessage;

$main = new Node('main');
$worker = new Node('worker');

// Do the "hard" job here ^.^
$worker->respond->sum(function(IMessage $message) {
  return array_sum($message->getPayload());
});

// Yep, You can scale your app this easily!
$twelve = $main->request->sum([10, 2]);

// Also, You can simply publish messages which does not expect an answear!
$main->publish('log.error', 'Database connection refused!');

// And listen on them in Your lazy process
$worker->subscribe('log.error', function(IMessage $message) {
  take_action($message->getPayload());
});

异步/同步调用

默认情况下,所有消息都是异步发送的,您可以为消息池化传输。但是,如果您想顺序执行逻辑并且您的进程可以在等待消息时阻塞执行,那么您可以使用 await 调用,这将池化传输直到响应到达。

$worker->respond('sum', function($request) {
  return my_array_sum($request);
});


// Will pool the transport until the response is arrived.
$response = $main->request('sum', [10, 2]);

异步语法

$worker->respond('sum', 'array_sum');

$main->request('sum', [10, 2], function($response) {
  assert($response, 12);
});


// Call the pool, when Your process is free to receive messages.
$main->tick();

流畅调用

为了使使用更加符合程序员的需求,该包支持流畅语法。

// Would be funny, right?
$worker->respond->calculator->sum('array_sum');

// Other machine.
$six = $main->request->calculator->sum([1, 2, 3]);