[libmessage-passing-zeromq-perl] 19/78: Get the linger semantic correct, and get atfork working

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 04c4a8c46c962bc5554ee2f33344e5003c26a33f
Author: Tomas Doran <bobtfish at bobtfish.net>
Date:   Mon Mar 5 23:29:58 2012 +0000

    Get the linger semantic correct, and get atfork working
---
 Makefile.PL                              |    1 +
 lib/Log/Stash/ZeroMQ.pm                  |   11 ++++++++---
 lib/Log/Stash/ZeroMQ/Role/HasAContext.pm |    6 ++++--
 lib/Log/Stash/ZeroMQ/Role/HasASocket.pm  |   21 ++++++++++++++++++++-
 4 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/Makefile.PL b/Makefile.PL
index c538630..6b11d8c 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -16,6 +16,7 @@ requires 'Try::Tiny';
 requires 'Task::Weaken';
 requires 'Log::Stash' => '0.001';
 requires 'POSIX::AtFork' => '0.02';
+requires 'Sub::Name';
 
 test_requires 'Test::More' => '0.88';
 
diff --git a/lib/Log/Stash/ZeroMQ.pm b/lib/Log/Stash/ZeroMQ.pm
index 92ff1c3..28c4585 100644
--- a/lib/Log/Stash/ZeroMQ.pm
+++ b/lib/Log/Stash/ZeroMQ.pm
@@ -2,15 +2,20 @@ package Log::Stash::ZeroMQ;
 use Moose ();
 use ZeroMQ qw/ :all /;
 use POSIX::AtFork ();
+use Sub::Name;
 use namespace::autoclean;
 
 our $VERSION = "0.001";
 $VERSION = eval $VERSION;
 
-sub at_fork {
-}
+our @_WITH_CONTEXTS;
 
-POSIX::AtFork->add_to_prepare(\&at_fork);
+POSIX::AtFork->add_to_prepare(subname at_fork => sub {
+    foreach my $thing (grep { defined $_ } @_WITH_CONTEXTS) {
+        $thing->_clear_ctx;
+    }
+    @_WITH_CONTEXTS = ();
+});
 
 1;
 
diff --git a/lib/Log/Stash/ZeroMQ/Role/HasAContext.pm b/lib/Log/Stash/ZeroMQ/Role/HasAContext.pm
index f4cfc86..5345aa4 100644
--- a/lib/Log/Stash/ZeroMQ/Role/HasAContext.pm
+++ b/lib/Log/Stash/ZeroMQ/Role/HasAContext.pm
@@ -10,12 +10,14 @@ has _ctx => (
     isa => 'ZeroMQ::Context',
     lazy => 1,
     default => sub {
+        my $self = shift;
         my $ctx = ZeroMQ::Context->new();
-        push(@Log::Stash::ZeroMQ::CONTEXTS, $ctx);
-        weaken($Log::Stash::ZeroMQ::CONTEXTS[-1]);
+        push(@Log::Stash::ZeroMQ::_WITH_CONTEXTS, $self);
+        weaken($Log::Stash::ZeroMQ::_WITH_CONTEXTS[-1]);
         $ctx;
     },
     clearer => '_clear_ctx',
 );
 
 1;
+
diff --git a/lib/Log/Stash/ZeroMQ/Role/HasASocket.pm b/lib/Log/Stash/ZeroMQ/Role/HasASocket.pm
index bbc8729..724593f 100644
--- a/lib/Log/Stash/ZeroMQ/Role/HasASocket.pm
+++ b/lib/Log/Stash/ZeroMQ/Role/HasASocket.pm
@@ -14,11 +14,30 @@ has _socket => (
     clearer => '_clear_socket',
 );
 
+before _clear_ctx => sub {
+    my $self = shift;
+    if (!$self->linger) {
+        $self->_socket->setsockopt(ZMQ_LINGER, 0);
+    }
+    $self->_socket->close;
+    $self->_clear_socket;
+};
+
 requires '_socket_type';
 
+has linger => (
+    is => 'ro',
+    isa => 'Bool',
+    default => 0,
+);
+
 sub _build_socket {
     my $self = shift;
-   $self->_ctx->socket($self->_socket_type);
+    my $socket = $self->_ctx->socket($self->_socket_type);
+    if (!$self->linger) {
+        $socket->setsockopt(ZMQ_LINGER, 0);
+    }
+    $socket;
 }
 
 1;

-- 
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