[libmessage-passing-zeromq-perl] 18/78: Split out a HasASocket role

Jonas Smedegaard js at alioth.debian.org
Mon Sep 30 09:28:19 UTC 2013


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libmessage-passing-zeromq-perl.

commit 5e75abe87b01483c9c0b95a6e20ad938fc17a2ad
Author: Tomas Doran <bobtfish at bobtfish.net>
Date:   Mon Mar 5 23:17:20 2012 +0000

    Split out a HasASocket role
---
 Makefile.PL                             |    1 +
 lib/Log/Stash/Input/ZeroMQ.pm           |   37 ++++++++++++++++---------------
 lib/Log/Stash/Output/ZeroMQ.pm          |   34 +++++++++++++---------------
 lib/Log/Stash/ZeroMQ.pm                 |   26 ++++------------------
 lib/Log/Stash/ZeroMQ/Role/HasASocket.pm |   37 +++++++++++++++++++++++++++++++
 5 files changed, 77 insertions(+), 58 deletions(-)

diff --git a/Makefile.PL b/Makefile.PL
index 45d4148..c538630 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -15,6 +15,7 @@ requires 'JSON::XS';
 requires 'Try::Tiny';
 requires 'Task::Weaken';
 requires 'Log::Stash' => '0.001';
+requires 'POSIX::AtFork' => '0.02';
 
 test_requires 'Test::More' => '0.88';
 
diff --git a/lib/Log/Stash/Input/ZeroMQ.pm b/lib/Log/Stash/Input/ZeroMQ.pm
index 345939f..960f4d3 100644
--- a/lib/Log/Stash/Input/ZeroMQ.pm
+++ b/lib/Log/Stash/Input/ZeroMQ.pm
@@ -6,7 +6,16 @@ use Scalar::Util qw/ weaken /;
 use Try::Tiny qw/ try catch /;
 use namespace::autoclean;
 
-with 'Log::Stash::Role::Input';
+with qw/
+    Log::Stash::ZeroMQ::Role::HasASocket
+    Log::Stash::Role::Input
+/;
+
+has '+_socket' => (
+    handles => {
+        _zmq_recv => 'recv',
+    },
+);
 
 has socket_bind => (
     is => 'ro',
@@ -14,24 +23,16 @@ has socket_bind => (
     default => 'tcp://*:5558',
 );
 
-with 'Log::Stash::ZeroMQ::Role::HasAContext';
+sub _socket_type { ZMQ_SUB }
 
-has _socket => (
-    is => 'ro',
-    isa => 'ZeroMQ::Socket',
-    lazy => 1,
-    default => sub {
-        my $self = shift;
-        my $socket = $self->_ctx->socket(ZMQ_SUB);
-        $socket->setsockopt(ZMQ_SUBSCRIBE, '');
-        $socket->setsockopt(ZMQ_HWM, 100000); # Buffer up to 100k messages.
-        $socket->bind($self->socket_bind);
-        return $socket;
-    },
-    handles => {
-        _zmq_recv => 'recv',
-    },
-);
+around _build_socket => sub {
+    my ($orig, $self, @args) = @_;
+    my $socket = $self->$orig(@args);
+    $socket->setsockopt(ZMQ_SUBSCRIBE, '');
+    $socket->setsockopt(ZMQ_HWM, 100000); # Buffer up to 100k messages.
+    $socket->bind($self->socket_bind);
+    return $socket;
+};
 
 sub _try_rx {
     my $self = shift();
diff --git a/lib/Log/Stash/Output/ZeroMQ.pm b/lib/Log/Stash/Output/ZeroMQ.pm
index 40060e6..c982eca 100644
--- a/lib/Log/Stash/Output/ZeroMQ.pm
+++ b/lib/Log/Stash/Output/ZeroMQ.pm
@@ -3,7 +3,13 @@ use Moose;
 use ZeroMQ ':all';
 use namespace::autoclean;
 
-with 'Log::Stash::ZeroMQ::Role::HasAContext';
+with 'Log::Stash::ZeroMQ::Role::HasASocket';
+
+has '+_socket' => (
+    handles => {
+        '_zmq_send' => 'send',
+    },
+);
 
 has connect => (
     isa => 'Str',
@@ -11,23 +17,15 @@ has connect => (
     default => sub { 'tcp://127.0.0.1:5558' },
 );
 
-has _socket => (
-    is => 'ro',
-    isa => 'ZeroMQ::Socket',
-    lazy => 1,
-    default => sub {
-        my $self = shift;
-        my $socket = $self->_ctx->socket(ZMQ_PUB);
-        $socket->setsockopt(ZMQ_HWM, 1000);
-        $socket->connect($self->connect);
-        return $socket;
-    },
-    predicate => '_has_socket',
-    clearer => '_clear_socket',
-    handles => {
-        '_zmq_send' => 'send',
-    },
-);
+sub _socket_type { ZMQ_PUB }
+
+around _build_socket => sub {
+    my ($orig, $self, @args) = @_;
+    my $socket = $self->$orig(@args);
+    $socket->setsockopt(ZMQ_HWM, 1000);
+    $socket->connect($self->connect);
+    return $socket;
+};
 
 sub consume {
     my $self = shift;
diff --git a/lib/Log/Stash/ZeroMQ.pm b/lib/Log/Stash/ZeroMQ.pm
index f115cc4..92ff1c3 100644
--- a/lib/Log/Stash/ZeroMQ.pm
+++ b/lib/Log/Stash/ZeroMQ.pm
@@ -1,34 +1,16 @@
 package Log::Stash::ZeroMQ;
 use Moose ();
-use ZeroMQ;
+use ZeroMQ qw/ :all /;
+use POSIX::AtFork ();
 use namespace::autoclean;
 
 our $VERSION = "0.001";
 $VERSION = eval $VERSION;
 
-use base 'Exporter';
-
-our @EXPORT = qw/
-    fork
-/;
-
-our @CONTEXTS;
-
-our $FORK;
-sub import {
-    $FORK ||= *{'CORE::GLOBAL::fork'};
-    # Test this even works?
-    *{'CORE::GLOBAL::fork'} = \⋔
-    shift->SUPER::import(@_);
+sub at_fork {
 }
 
-sub fork {
-    foreach my $ctx (grep { defined $_ } @CONTEXTS) {
-        $ctx->term;
-    }
-    # FIXME - What if fork has already been replaced?
-    $FORK->(@_);
-}
+POSIX::AtFork->add_to_prepare(\&at_fork);
 
 1;
 
diff --git a/lib/Log/Stash/ZeroMQ/Role/HasASocket.pm b/lib/Log/Stash/ZeroMQ/Role/HasASocket.pm
new file mode 100644
index 0000000..bbc8729
--- /dev/null
+++ b/lib/Log/Stash/ZeroMQ/Role/HasASocket.pm
@@ -0,0 +1,37 @@
+package Log::Stash::ZeroMQ::Role::HasASocket;
+use Moose::Role;
+use ZeroMQ ':all';
+use namespace::autoclean;
+
+with 'Log::Stash::ZeroMQ::Role::HasAContext';
+
+has _socket => (
+    is => 'ro',
+    isa => 'ZeroMQ::Socket',
+    lazy => 1,
+    builder => '_build_socket',
+    predicate => '_has_socket',
+    clearer => '_clear_socket',
+);
+
+requires '_socket_type';
+
+sub _build_socket {
+    my $self = shift;
+   $self->_ctx->socket($self->_socket_type);
+}
+
+1;
+
+=head1 SPONSORSHIP
+
+This module exists due to the wonderful people at
+L<Suretec Systems|http://www.suretecsystems.com/> who sponsored it's
+development.
+
+=head1 AUTHOR, COPYRIGHT AND LICENSE
+
+See L<Log::Stash>.
+
+=cut
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmessage-passing-zeromq-perl.git



More information about the Pkg-perl-cvs-commits mailing list