Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use ZMQ only for now

Signed-off-by: Pedro Melo <melo@simplicidade.org>
  • Loading branch information...
commit 45d8438fd3b8f4b6a0227d0f9bb1e94a3c422ce2 1 parent e419989
@melo authored
Showing with 22 additions and 21 deletions.
  1. +22 −21 message-passing/plack_message_passing_logger/app.psgi
View
43 message-passing/plack_message_passing_logger/app.psgi
@@ -9,63 +9,64 @@
# https://github.com/lestrrat/ZeroMQ-Perl/issues/42
#
# Define BOOM=1 when you run it and watch.
-#
+#
# To startup this app:
-#
+#
# starman --workers 5 --port 127.0.0.1:5000 --max-requests 3 --preload-app app.psgi
-#
+#
# Then on another terminal:
-#
+#
# ab -n 100 http://127.0.0.1:5000/
#
# Without BOOM=1 all is well, define BOOM=1 and it will die after 11
# requests...
-#
+#
# Message::Passing::ZeroMQ has code to clear all ZeroMQ contexts
# *before* the fork, and that is the solution. I'll take that code and
# write a module that hides all of this, and provide a saner API
-#
+#
# Pedro Melo, Tue Aug 21 17:14:26 UTC 2012
use strict;
use warnings;
use Plack::Request;
-use Message::Passing::Filter::Encoder::JSON;
-use Message::Passing::Output::ZeroMQ;
use ZMQ::LibZMQ2;
use ZMQ::Constants ':v2.1.11', ':all';
use JSON 'encode_json';
-
-my $log_mp =
- Message::Passing::Filter::Encoder::JSON->new(
- output_to => Message::Passing::Output::ZeroMQ->new(connect => 'tcp://127.0.0.1:5552', linger => 1));
+use Proc::PidChange ':all';
my ($ctx, $log_zq);
-if ($ENV{BOOM}) {
- $ctx = zmq_init(1);
- $log_zq = zmq_socket($ctx, ZMQ_PUB);
- zmq_setsockopt($log_zq, ZMQ_LINGER, 5);
- zmq_connect($log_zq, 'tcp://127.0.0.1:5552');
+unless ($ENV{SKIP_BOOM}) {
+ my $cb = sub {
+ print STDERR "[$$] CONNECT\n";
+ $ctx = zmq_init(1);
+ $log_zq = zmq_socket($ctx, ZMQ_PUB);
+ zmq_connect($log_zq, 'tcp://127.0.0.1:5552');
+ };
+ $cb->();
+ register_pid_change_callback($cb);
+ die "No realtime detection of pid changes???" unless $Proc::PidChange::RT;
}
sub logger {
my ($msg, @rest) = @_;
+ return unless $log_zq;
$msg = { pid => $$, msg => $msg, args => \@rest };
- $log_mp->consume($msg);
- zmq_send($log_zq, encode_json({more => $msg})) if $log_zq;
+ zmq_send($log_zq, encode_json({ more => $msg })) if $log_zq;
}
logger('Ready to rumble!');
-my $app = sub {
+my $count = 0;
+my $app = sub {
my ($env) = @_;
my $req = Plack::Request->new($env);
my $m = $req->method;
my $p = $req->path_info;
- logger("Hit $m at $p", { ip => $req->address, method => $m, path => $p });
+ logger("Hit $m at $p", { ip => $req->address, method => $m, path => $p, count => ++$count });
[200, ['Content-Type' => 'text/html'], ["OK"]];
};
Please sign in to comment.
Something went wrong with that request. Please try again.