lib/POE/Component/MessageQueue/Storage/Remote.pm

filter: Statementstmt 83.5% 61 / 73 Branchbran 41.6% 5 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 4 totaltotal 73.6% 81 / 110 CC 9 SCAR 11.0
CC
9
Cov
77%
__ANON__
12.0
__ANON__
3.1
remote_call
3.0
CRAP 3.0
« lib/POE/Component/MessageQueue/Storage/Remote/Server.pm ↑ Summary lib/POE/Component/MessageQueue/Queue.pm »
1 #
2 # Copyright 2008 Paul Driver <frodwith@gmail.com>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17  
18 package POE::Component::MessageQueue::Storage::Remote;
19 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use POE::Component::Client::TCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use POE::Component::MessageQueue::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 has servers => (
26 is => 'ro',
27 isa => 'ArrayRef[HashRef]',
28 required => 1,
29 );
30  
31 2 has session_id => (
32 is => 'rw',
33 isa => 'Int',
34 init_arg => undef,
35 );
36  
37 has idmaker => (
38 is => 'ro',
39 isa => 'Data::UUID',
40 init_arg => undef,
41 1 ▶ default => sub { Data::UUID->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 2 );
43  
44 sub add_server_method
45 {
46 20 ▶ my ($class, $method) = @_;
Subroutine add_server_method: called CC=1 SCAR=0.4
Pod undocumented
47 $class->meta->add_method($method, sub {
48 0 ▶ my $self = shift;
Subroutine get: not called CC=1 SCAR=1.2
Subroutine remove: called CC=1 SCAR=1.2
49 0 $poe_kernel->post($self->session_id, remote_call => {
50 method => $method,
51 args => [@_],
52 });
53 20 });
54 }
55  
56 2 my %methods = map {$_ => 1}
57 POE::Component::MessageQueue::Storage->meta->get_required_method_list;
58  
59 2 delete $methods{storage_shutdown};
60  
61 2 __PACKAGE__->add_server_method($_) foreach (keys %methods);
62  
63 sub storage_shutdown
64 {
65 1 ▶ my ($self, $callback) = @_;
Subroutine storage_shutdown: called CC=1 SCAR=0.0
Pod undocumented
66 1 $poe_kernel->post($self->session_id, 'shutdown');
67 1 goto $callback;
68 }
69  
70 2 with qw(POE::Component::MessageQueue::Storage);
71  
72 sub BUILD
73 {
74 1 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=1 SCAR=0.7
Pod undocumented
75  
76 1 my $servers = $self->servers;
77 1 my $si = 1;
78 1 my $total_fail = 0;
79 my $retry = sub {
80 0 ▶ if ($si >= @$servers)
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($si >= @$servers) 0 0 0
81 {
82 0 ▶ if (++$total_fail > 2)
Branch: 1 branch
BranchTrueFalseTotal
if (++$total_fail > 2) 0 0 0
83 {
84 0 $self->log(emergency =>
85 "Tried to connect to all servers $total_fail times without success."
86 );
87 }
88 0 $si = 0;
89 }
90 0 my $info = $servers->[$si++];
91 0 $poe_kernel->delay(connect => 2 => $info->{host} => $info->{port});
92 1 };
93  
94 1 my ($host, $port) = @{$servers->[0]}{'host', 'port'};
95  
96 $self->session_id(POE::Component::Client::TCP->new(
97 RemoteAddress => $host,
98 RemotePort => $port,
99 ConnectTimeout => 3,
100 Filter => POE::Filter::Reference->new,
101 ObjectStates => [ $self => ['remote_call'] ],
102  
103 Connected => sub {
104 1 ▶ my $heap = $_[HEAP];
Subroutine __ANON__: called CC=2 SCAR=6.9
105 $heap->{server}->put($_)
106 1 foreach map { $_->{request} } (values %{ $heap->{calls} });
107 1 $total_fail = 0;
108 },
109  
110 ConnectError => sub {
111 0 ▶ $self->log(error => "Could not connect to $host:$port ($total_fail)");
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 goto $retry;
113 },
114  
115 Disconnected => sub {
116 1 ▶ goto $retry unless $_[HEAP]->{shutdown};
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[3]{"shutdown"} 0 1 1
117 },
118  
119 ServerInput => sub {
120 1022 ▶ my ($heap, $request) = @_[HEAP, ARG0];
Subroutine __ANON__: called CC=3 SCAR=11.2
121 1022 my $id = $request->{callback};
122 1022 ▶ my $args = $request->{args} || [];
Condition50%1/2
$request->{'args'} || []
l!l
10220
MC/DC0%0/1
$request->{'args'} || []
$request->{'args'}
Truth table50%1/2
A
A= $request->{'args'}
Aresult
11
00
123 1022 my $call = delete $heap->{calls}->{$id};
124 1022 ▶ if (my $code = $call->{callback})
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $call->{"callback"}) 1022 0 1022
125 {
126 1022 $code->(@$args);
127 }
128 },
129  
130 ServerError => sub {
131 0 ▶ $self->log(error => "Remote error on ". $_[ARG0] . ": " . $_[ARG2]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 goto $retry;
133 },
134  
135 1 ));
136 }
137  
138 sub remote_call
139 {
140 1022 ▶ my ($self, $heap, $request) = @_[OBJECT, HEAP, ARG0];
Subroutine remote_call: called CC=3 SCAR=11.0
Pod undocumented
141  
142 1022 my $id = $self->idmaker->create_b64();
143 1022 my $call = $heap->{calls}->{$id} = { request => $request };
144  
145 1022 my $args = $request->{args};
146 1022 my $last = $args->[-1];
147 1022 ▶ if (ref $last eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $last eq "CODE") 1022 0 1022
148 {
149 1022 $call->{callback} = $last;
150 1022 $args->[-1] = $id;
151 }
152  
153 1022 ▶ $heap->{server}->put($request) if $heap->{connected};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"connected"} 1021 1 1022
154 }
155  
156 2 1;
157  
158 __END__
159  
160 =pod
161  
162 =head1 NAME
163  
164 POE::Component::MessageQueue::Storage::Remote -- Access a remote storage
165 engine via a TCP socket
166  
167 =head1 DESCRIPTION
168  
169 With this module, you can talk to a storage engine running under
170 L<POE::Component::MessageQueue::Storage::Remote::Server> transparently. You
171 can treat this like a normal local store once it's set up, and it can
172 optionally failover to other stores.
173  
174 =head1 CONSTRUCTOR PARAMETERS
175  
176 =over 2
177  
178 =item servers
179  
180 An arrayref of hashrefs of the form C<< {host => 'hostname, port => port} >>.
181 Remote will try these servers in a round robin fashion whenever it fails to
182 connect or gets disconnected. Passing in just one server is an effective way
183 to say "keep connecting to this server until it's up and reconnect to it if
184 you get disconnected."
185  
186 =back
187  
188 =head1 SEE ALSO
189  
190 L<POE::Component::MessageQueue::Storage::Remote::Server>
191  
192 =cut
193  
« lib/POE/Component/MessageQueue/Storage/Remote/Server.pm ↑ Summary lib/POE/Component/MessageQueue/Queue.pm »