MR::IProto(3pm) | User Contributed Perl Documentation | MR::IProto(3pm) |
MR::IProto - iproto network protocol client
IProto client can be created with full control of its behaviour:
my $client = MR::IProto->new( cluster => MR::IProto::Cluster->new( servers => [ MR::IProto::Cluster::Server->new( host => 'xxx.xxx.xxx.xxx', port => xxxx, ), ... ], ), );
Or without it:
my $client = MR::IProto->new( servers => 'xxx.xxx.xxx.xxx:xxxx,xxx.xxx.xxx.xxx:xxxx', );
Messages can be prepared and processed using objects (requires some more CPU):
my $request = MyProject::Message::MyOperation::Request->new( arg1 => 1, arg2 => 2, ); my $response = $client->send($request); # $response isa My::Project::Message::MyOperation::Response. # Of course, both message classes (request and reply) must # be implemented by user.
Or without them:
my $response = $client->send({ msg => x, data => [...], pack => 'xxx', unpack => sub { my ($data) = @_; return (...); }, });
Messages can be sent synchronously:
my $response = $client->send($response); # exception is raised if error is occurred # besides $@ you can check $! to identify reason of error
Or asynchronously:
use AnyEvent; my $callback = sub { my ($reply, $error) = @_; # on error $error is defined and $! can be set return; }; $client->send($request, $callback); # callback is called when reply is received or error is occurred
It is recommended to disconnect all connections in child after fork() to prevent possible conflicts:
my $pid = fork(); if ($pid == 0) { MR::IProto->disconnect_all(); }
This client is used to communicate with cluster of balanced servers using iproto network protocol.
To use it nicely you should to implement two subclasses of MR::IProto::Message for each message type, one for request message and another for reply. This classes must be named as "prefix::*::suffix", where prefix must be passed to constructor of MR::IProto as value of "prefix" attribute and suffix is either "Request" or "Response". This classes must be loaded before first message through client object will be sent.
To send messages asynchronously you should to implement event loop by self. AnyEvent is recommended.
If $callback is passed then request is done asynchronously and reply is passed to callback as first argument. Method must be called in void context to prevent possible errors. Only client errors can be raised in async mode. All communication errors are passed to callback as second argument. Additional information can be extracted from $! variable.
In sync mode (when $callback argument is skipped) all errors are raised and $! is also set. Response is returned from method, so method must be called in scalar context.
Request $message can be instance of MR::IProto::Message subclass. In this case reply will be also subclass of MR::IProto::Message. Or it can be passed as "\%args" hash reference with keys described in "_send".
See "_send" for more information about message data. Either $message or "\%args" allowed as content of "\@messages".
See "BUILDARGS" in Mouse::Manual::Construction for more information.
$message is an instance of MR::IProto::Message. If "\%args" hash reference is passed instead of $message then it can contain following keys:
MR::IProto::Cluster, MR::IProto::Cluster::Server, MR::IProto::Message.
2020-07-21 | perl v5.30.3 |