Tatsumaki/MessageQueue.pm

filter: Statementstmt 80.4% 78 / 97 Branchbran 60.0% 12 / 20 Conditioncond 62.5% 5 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 78.2% 18 / 23 Podpod 37.5% 3 / 8 totaltotal 72.0% 116 / 161 CC 15 SCAR 12.7
CC
15
Cov
76%
poll_once
8.3
__ANON__
6.0
poll
6.0
CRAP 3.6
« Tatsumaki/Template/Micro.pm ↑ Summary Tatsumaki/HTTPClient.pm »
1 package Tatsumaki::MessageQueue;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use constant DEBUG => $ENV{TATSUMAKI_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 has channel => (is => 'rw', isa => 'Str');
12 4 ▶has backlog => (is => 'rw', isa => 'ArrayRef', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
13 4 ▶has clients => (is => 'rw', isa => 'HashRef', default => sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 4 our $BacklogLength = 30; # TODO configurable
16  
17 4 my %instances;
18  
19 0 ▶sub channels {
Subroutine channels: not called CC=1 SCAR=6.9
Pod undocumented
20 0 values %instances;
21 }
22  
23 15 ▶sub instance {
Subroutine instance: called CC=2 SCAR=7.1
Pod undocumented
24 15 my($class, $name) = @_;
25 15 ▶ $instances{$name} ||= $class->new(channel => $name);
Condition66%2/3
$instances{$name} ||= $class->new("channel", $name)
l!l&&r!l&&!r
1140
MC/DC0%0/2
$instances{$name} ||= $class->new("channel", $name)
$instances{$name} $class->new("channel", $name)
Truth table66%2/3
A ||= B
A= $instances{$name}B= $class->new("channel", $name)
ABresult
1X1
011
000
26 }
27  
28 8 ▶sub backlog_events {
Subroutine backlog_events: called CC=1 SCAR=0.0
Pod undocumented
29 8 my $self = shift;
30 8 reverse grep defined, @{$self->backlog};
31 }
32  
33 5 ▶sub append_backlog {
Subroutine append_backlog: called CC=1 SCAR=0.0
Pod undocumented
34 5 my($self, @events) = @_;
35 5 my @new_backlog = (reverse(@events), @{$self->backlog});
36 5 $self->backlog([ splice @new_backlog, 0, $BacklogLength ]);
37 }
38  
39 5 ▶sub publish {
Subroutine publish: called CC=3 SCAR=11.0
Pod documented
40 5 my($self, @events) = @_;
41  
42 5 for my $client_id (keys %{$self->clients}) {
43 8 my $client = $self->clients->{$client_id};
44 8 ▶ if ($client->{cv}->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->{'cv'}->cb) { } 6 2 8
45 # currently listening: flush and send the events right away
46 6 $self->flush_events($client_id, @events);
47 } else {
48 # between long poll comet: buffer the events
49 # TODO: limit buffer length
50 2 warn "Buffering new events for $client_id" if DEBUG;
51 2 push @{$client->{buffer}}, @events;
52 }
53 }
54 5 $self->append_backlog(@events);
55 }
56  
57 10 ▶sub flush_events {
Subroutine flush_events: called CC=2 SCAR=9.4
Pod undocumented
58 10 my($self, $client_id, @events) = @_;
59  
60 10 ▶ my $client = $self->clients->{$client_id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $client = $self->clients->{$client_id} or return 10 0 10
61 10 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=8.4
62 10 my $cb = $client->{cv}->cb;
63 10 $client->{cv}->send(@events);
64 10 $client->{cv} = AE::cv;
65 10 $client->{buffer} = [];
66  
67 10 ▶ if ($client->{persistent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->{'persistent'}) { } 0 10 10
68 0 $client->{cv}->cb($cb);
69 } else {
70 10 undef $client->{longpoll_timer};
71 0 ▶ $client->{reconnect_timer} = AE::timer 30, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 Scalar::Util::weaken $self;
73 0 warn "Sweep $client_id (no long-poll reconnect)" if DEBUG;
74 0 undef $client;
75 0 delete $self->clients->{$client_id};
76 10 };
77 }
78 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
79 0 ▶ /Tatsumaki::Error::ClientDisconnect/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/Tatsumaki::Error::ClientDisconnect/ and do { '???'; undef $client; delete $self->clients->{$client_id} } 0 0 0
80 0 warn "Client $client_id disconnected" if DEBUG;
81 0 undef $client;
82 0 delete $self->clients->{$client_id};
83 };
84 10 };
85 }
86  
87 10 ▶sub poll_once {
Subroutine poll_once: called CC=8 SCAR=21.1
Pod documented
88 10 my($self, $client_id, $cb, $timeout) = @_;
89  
90 10 my $is_new;
91 10 ▶ my $client = $self->clients->{$client_id} ||= do {
Condition66%2/3
$self->clients->{$client_id} ||= do { $is_new = 1; +{"cv", &AE::cv(), "persistent", 0, "buffer", []} }
l!l&&r!l&&!r
280
MC/DC0%0/2
$self->clients->{$client_id} ||= do { $is_new = 1; +{"cv", &AE::cv(), "persistent", 0, "buffer", []} }
$self->clients->{$client_id} do { $is_new = 1; +{"cv", &AE::cv(), "persistent", 0, "buffer", []} }
Truth table66%2/3
A ||= B
A= $self->clients->{$client_id}B= do { $is_new = 1; +{"cv", &AE::cv(), "persistent", 0, "buffer", []} }
ABresult
1X1
011
000
92 8 $is_new = 1;
93 8 + { cv => AE::cv, persistent => 0, buffer => [] };
94 };
95  
96 10 ▶ if ( $client->{longpoll_timer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->{"longpoll_timer"}) 0 10 10
97 # close last connection from the same client_id
98 0 $self->flush_events($client_id);
99 0 undef $client->{longpoll_timer};
100 }
101 10 undef $client->{reconnect_timer};
102  
103 10 ▶ $client->{cv}->cb(sub { $cb->($_[0]->recv) });
Subroutine __ANON__: called CC=1 SCAR=0.0
104  
105 # reset garbage collection timeout with the long-poll timeout
106 # $timeout = 0 is a valid timeout for interval-polling
107 10 ▶ $timeout = 55 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 9 1 10
108 1 ▶ $client->{longpoll_timer} = AE::timer $timeout || 55, 0, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 1 Scalar::Util::weaken $self;
110 1 warn "Timing out $client_id long-poll" if DEBUG;
111 1 $self->flush_events($client_id);
112 10 ▶ };
Condition50%1/2
$timeout || 55
l!l
100
MC/DC0%0/1
$timeout || 55
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
113  
114 10 ▶ if ($is_new) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_new) { } 8 2 10
elsif (@{$client->{'buffer'};}) { } 2 0 2
115 # flush backlog for a new client
116 8 my @events = $self->backlog_events;
117 8 ▶ $self->flush_events($client_id, @events) if @events;
Branch: 1 branch
BranchTrueFalseTotal
if @events 1 7 8
118 2 }elsif ( @{ $client->{buffer} } ) {
119 # flush buffer for a long-poll client
120 2 $self->flush_events($client_id, @{ $client->{buffer} });
121 }
122 }
123  
124 0 ▶sub poll {
Subroutine poll: not called CC=2 SCAR=17.9
Pod documented
125 0 my($self, $client_id, $cb) = @_;
126  
127 # TODO register client info like names and remote host in $client
128 0 my $cv = AE::cv;
129 0 ▶ $cv->cb(sub { $cb->($_[0]->recv) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 my $s = $self->clients->{$client_id} = {
131 cv => $cv, persistent => 1, buffer => [],
132 };
133  
134 0 my @events = $self->backlog_events;
135 0 ▶ $self->flush_events($client_id, @events) if @events;
Branch: 1 branch
BranchTrueFalseTotal
if @events 0 0 0
136 }
137  
138 4 1;
139  
140 __END__
« Tatsumaki/Template/Micro.pm ↑ Summary Tatsumaki/HTTPClient.pm »