diff --git a/connector/perl/lib/MR/IProto.pm b/connector/perl/lib/MR/IProto.pm index 27b0e6359bded860e4e2e0c3e477fbd8c0d3538a..8d3aa4c57b09929a15c89f541bcd61a40789d0c2 100644 --- a/connector/perl/lib/MR/IProto.pm +++ b/connector/perl/lib/MR/IProto.pm @@ -67,6 +67,14 @@ Or asynchronously: $client->send($request, $callback); # callback is called when reply is received or error is occured +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(); + } + =head1 DESCRIPTION This client is used to communicate with cluster of balanced servers using @@ -334,6 +342,18 @@ sub SetTimeout { return; } +=item disconnect_all + +Class method used to disconnect all iproto-connections. Very useful in case of fork(). + +=cut + +sub disconnect_all { + my ($class) = @_; + MR::IProto::Cluster::Server->disconnect_all(); + return; +} + =back =head1 PROTECTED METHODS diff --git a/connector/perl/lib/MR/IProto/Cluster/Server.pm b/connector/perl/lib/MR/IProto/Cluster/Server.pm index ec059ad34cd2dc6efb351dafa0d029e43cf2ab85..e5b75164ee63797452049ac781f70fcf340bd2b5 100644 --- a/connector/perl/lib/MR/IProto/Cluster/Server.pm +++ b/connector/perl/lib/MR/IProto/Cluster/Server.pm @@ -12,6 +12,7 @@ This class is used to implement all communication with one server. use Mouse; use Mouse::Util::TypeConstraints; +use Scalar::Util; use MR::IProto::Connection::Async; use MR::IProto::Connection::Sync; use MR::IProto::Message; @@ -170,14 +171,48 @@ has sync => ( lazy_build => 1, ); +my %servers; + =back +=head1 PUBLIC METHODS + +=over + +=item disconnect_all + +Class method used to disconnect all iproto-connections. Very useful in case of fork(). + +=cut + +sub disconnect_all { + my ($class) = @_; + foreach my $server (values %servers) { + $server->clear_async(); + $server->clear_sync(); + } + return; +} + =head1 PROTECTED METHODS =over =cut +sub BUILD { + my ($self) = @_; + $servers{Scalar::Util::refaddr($self)} = $self; + Scalar::Util::weaken($servers{Scalar::Util::refaddr($self)}); + return; +} + +sub DEMOLISH { + my ($self) = @_; + delete $servers{Scalar::Util::refaddr($self)}; + return; +} + sub _build_async { my ($self) = @_; return MR::IProto::Connection::Async->new( server => $self );