IProto/Connection/Async.pm

filter: Statementstmt 15.8% 23 / 145 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 16.0% 4 / 25 Podpod 50.0% 2 / 4 totaltotal 13.1% 29 / 220 CC 25 SCAR 24.3
CC
25
Cov
12%
_send
30.0
__ANON__
30.0
_try_to_send
12.0
CRAP 11.4
« IProto/Cluster.pm ↑ Summary IProto/Response.pm »
1 package MR::IProto::Connection::Async;
2  
3 =head1 NAME
4  
5 MR::IProto::Connection::Async - async communication
6  
7 =head1 DESCRIPTION
8  
9 Used to perform asynchronous communication.
10  
11 =cut
12  
13 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 extends 'MR::IProto::Connection';
15  
16 1 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 has _handle => (
20 is => 'ro',
21 isa => 'AnyEvent::Handle',
22 predicate => '_has_handle',
23 lazy_build => 1,
24 );
25  
26 1 has _queue => (
27 is => 'ro',
28 isa => 'ArrayRef',
29 lazy_build => 1,
30 );
31  
32 1 has _in_progress => (
33 is => 'rw',
34 isa => 'Int',
35 default => 0,
36 );
37  
38 1 has _callbacks => (
39 is => 'ro',
40 isa => 'HashRef',
41 lazy_build => 1,
42 );
43  
44 1 has _read_reply => (
45 is => 'ro',
46 isa => 'CodeRef',
47 lazy_build => 1,
48 );
49  
50 1 has _no_reply => (
51 is => 'ro',
52 isa => 'ArrayRef',
53 lazy_build => 1,
54 );
55  
56 1 has _on_drain => (
57 is => 'ro',
58 isa => 'CodeRef',
59 lazy_build => 1,
60 );
61  
62 =head1 PUBLIC METHODS
63  
64 =over
65  
66 =item send
67  
68 Enqueue message send.
69 For list of arguments see L</_send>.
70  
71 =cut
72  
73 0 ▶sub fh { return $_[0]->_has_handle && $_[0]->_handle }
Subroutine fh: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]->_has_handle && $_[0]->_handle
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]->_has_handle && $_[0]->_handle
$_[0]->_has_handle $_[0]->_handle
Truth table0%0/3
A && B
A= $_[0]->_has_handleB= $_[0]->_handle
ABresult
0X0
100
111
74  
75 0 ▶sub send {
Subroutine send: not called CC=2 SCAR=17.9
Pod documented
76 0 my $self = shift;
77 0 ▶ if( $self->_in_progress < $self->max_parallel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_progress < $self->max_parallel) { } 0 0 0
78 0 $self->_in_progress( $self->_in_progress + 1 );
79 0 $self->_send(@_);
80 }
81 else {
82 0 push @{$self->_queue}, [@_];
83 }
84 0 return;
85 }
86  
87 =item set_timeout( $timeout )
88  
89 Set timeout value for existing connection.
90  
91 =cut
92  
93 0 ▶sub set_timeout {
Subroutine set_timeout: not called CC=2 SCAR=17.9
Pod documented
94 0 my ($self, $timeout) = @_;
95 0 ▶ $self->_handle->timeout($timeout) if $self->_has_handle();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_handle 0 0 0
96 0 return;
97 }
98  
99 =back
100  
101 =head1 PROTECTED METHODS
102  
103 =over
104  
105 =item _send( $msg, $payload, $callback, $no_reply )
106  
107 Send message to server.
108  
109 =cut
110  
111 0 ▶sub _send {
Subroutine _send: not called CC=5 SCAR=34.0
112 0 my ($self, $msg, undef, $callback, $no_reply, $sync) = @_;
113 0 ▶ $sync = $self->_choose_sync() unless defined $sync;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sync 0 0 0
114 0 my $header = $self->_pack_header($msg, length $_[2], $sync);
115 0 my $server = $self->server;
116 0 $self->_callbacks->{$sync} = $callback;
117 0 $server->_send_started($sync, $msg, $_[2]);
118 0 my $handle = $self->_handle;
119 0 ▶ if( $server->debug >= 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server->debug >= 5) 0 0 0
120 0 $server->_debug_dump('send header: ', $header);
121 0 $server->_debug_dump('send payload: ', $_[2]);
122 }
123 0 $handle->push_write($header);
124 0 $handle->push_write($_[2]);
125 0 ▶ if( $no_reply ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_reply) { } 0 0 0
126 0 push @{$self->_no_reply}, $sync;
127 0 ▶ $handle->on_drain( $self->_on_drain ) unless defined $handle->{on_drain};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle->{"on_drain"} 0 0 0
128 }
129 else {
130 0 $handle->push_read( chunk => 12, $self->_read_reply );
131 }
132 0 return;
133 }
134  
135 0 ▶sub _build__read_reply {
Subroutine _build__read_reply: not called CC=1 SCAR=6.9
136 0 my ($self) = @_;
137 0 my $server = $self->server;
138 0 weaken($self);
139 0 weaken($server);
140 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
141 0 my ($handle, $data) = @_;
142 0 my $dump_resp = $server->debug >= 6;
143 0 ▶ $server->_debug_dump('recv header: ', $data) if $dump_resp;
Branch: 1 branch
BranchTrueFalseTotal
if $dump_resp 0 0 0
144 0 my ($msg, $payload_length, $sync) = $self->_unpack_header($data);
145 0 ▶ $handle->unshift_read( chunk => $payload_length, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
146 0 my ($handle, $data) = @_;
147 0 ▶ $server->_debug_dump('recv payload: ', $data) if $dump_resp;
Branch: 1 branch
BranchTrueFalseTotal
if $dump_resp 0 0 0
148 0 $server->_recv_finished($sync, $msg, $data);
149 0 $self->_finish_and_start();
150 0 delete($self->_callbacks->{$sync})->($msg, $data);
151 0 return;
152 0 });
153 0 return;
154 0 };
155 }
156  
157 0 ▶sub _try_to_send {
Subroutine _try_to_send: not called CC=3 SCAR=24.8
158 0 my ($self) = @_;
159 0 ▶ while( $self->_in_progress < $self->max_parallel && (my $task = shift @{ $self->_queue }) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_progress < $self->max_parallel and my $task = shift @{$self->_queue;}) 0 0 0
Condition0%0/3
$self->_in_progress < $self->max_parallel and my $task = shift @{$self->_queue;}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_in_progress < $self->max_parallel and my $task = shift @{$self->_queue;}
$self->_in_progress < $self->max_parallel my $task = shift @{$self->_queue;}
Truth table0%0/3
A and B
A= $self->_in_progress < $self->max_parallelB= my $task = shift @{$self->_queue;}
ABresult
0X0
100
111
160 0 $self->_in_progress( $self->_in_progress + 1 );
161 0 $self->_send(@$task);
162 }
163 0 return;
164 }
165  
166 0 ▶sub _finish_and_start {
Subroutine _finish_and_start: not called CC=2 SCAR=17.9
167 0 my ($self) = @_;
168 0 ▶ if( my $task = shift @{$self->_queue} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $task = shift @{$self->_queue;}) { } 0 0 0
169 0 $self->_send(@$task);
170 }
171 else {
172 0 $self->_in_progress( $self->_in_progress - 1 );
173 }
174 0 return;
175 }
176  
177 0 ▶sub _build__handle {
Subroutine _build__handle: not called CC=2 SCAR=17.9
178 0 my ($self) = @_;
179 0 my $server = $self->server;
180 0 ▶ $server->_debug("connecting") if $server->debug >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $server->debug >= 4 0 0 0
181 0 weaken($self);
182 0 weaken($server);
183 return AnyEvent::Handle->new(
184 connect => [ $self->host, $self->port ],
185 no_delay => $self->tcp_nodelay,
186 keepalive => $self->tcp_keepalive,
187 timeout => $self->timeout,
188 0 ▶ on_prepare => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 return $self->connect_timeout;
190 },
191 0 ▶ on_connect => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
192 0 my ($handle) = @_;
193 0 ▶ $server->_debug("connected") if $server->debug >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $server->debug >= 1 0 0 0
194 0 return;
195 },
196 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
197 0 my ($handle, $fatal, $message) = @_;
198 0 my $errno = $!;
199 0 ▶ $server->_debug(($fatal ? 'fatal ' : '') . 'error: ' . $message);
Branch: 1 branch
BranchTrueFalseTotal
$fatal ? : 0 0 0
200 0 my @callbacks;
201 0 foreach my $sync ( keys %{$self->_callbacks} ) {
202 0 $server->_recv_finished($sync, undef, undef, $message, $errno);
203 0 $self->_in_progress( $self->_in_progress - 1 );
204 0 push @callbacks, $self->_callbacks->{$sync};
205 }
206 0 $server->active(0);
207 0 $self->_clear_handle();
208 0 $self->_clear_callbacks();
209 0 $self->_clear_no_reply();
210 0 ▶ $server->_debug('closing socket') if $server->debug >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $server->debug >= 1 0 0 0
211 0 $handle->destroy();
212 0 $self->_try_to_send();
213 0 $_->(undef, undef, $message, $errno) foreach @callbacks;
214 0 return;
215 },
216 0 ▶ on_timeout => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
217 0 my ($handle) = @_;
218 0 ▶ return unless keys %{$self->_callbacks};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$self->_callbacks;} 0 0 0
219 0 ▶ $handle->_error( Errno::ETIMEDOUT ) if keys %{$self->_callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$self->_callbacks;} 0 0 0
220 0 return;
221 },
222 0 );
223 }
224  
225 0 ▶sub _build__on_drain {
Subroutine _build__on_drain: not called CC=1 SCAR=6.9
226 0 my ($self) = @_;
227 0 my $server = $self->server;
228 0 weaken($self);
229 0 weaken($server);
230 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
231 0 my ($handle) = @_;
232 0 ▶ if( $self->_has_no_reply() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_no_reply) 0 0 0
233 0 foreach my $sync ( @{$self->_no_reply} ) {
234 0 $server->_recv_finished($sync, undef, undef);
235 0 $self->_in_progress( $self->_in_progress - 1 );
236 0 delete($self->_callbacks->{$sync})->(undef, undef);
237 }
238 0 $self->_clear_no_reply();
239 0 $self->_try_to_send();
240 0 $handle->on_drain(undef);
241 }
242 0 return;
243 0 };
244 }
245  
246 0 ▶sub _build__queue {
Subroutine _build__queue: not called CC=1 SCAR=6.9
247 0 my ($self) = @_;
248 0 return [];
249 }
250  
251 0 ▶sub _build__callbacks {
Subroutine _build__callbacks: not called CC=1 SCAR=6.9
252 0 my ($self) = @_;
253 0 return {};
254 }
255  
256 0 ▶sub _build__no_reply {
Subroutine _build__no_reply: not called CC=1 SCAR=6.9
257 0 my ($self) = @_;
258 0 return [];
259 }
260  
261 0 ▶around _choose_sync => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
262 0 my ($orig, $self) = @_;
263 0 my $sync;
264 0 my $callbacks = $self->_callbacks;
265 0 for( 1 .. 50 ) {
266 0 $sync = $self->$orig();
267 0 ▶ return $sync unless exists $callbacks->{$sync};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $callbacks->{$sync} 0 0 0
268 }
269 0 die "Can't choose sync value after 50 iterations";
270 1 };
271  
272 0 ▶sub Close { die "This is not what should be done" }
Subroutine Close: not called CC=1 SCAR=6.9
Pod undocumented
273  
274 =back
275  
276 =head1 SEE ALSO
277  
278 L<MR::IProto::Connection>, L<MR::IProto::Cluster::Server>.
279  
280 =cut
281  
282 1 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
283 1 __PACKAGE__->meta->make_immutable();
284  
285 1 1;
« IProto/Cluster.pm ↑ Summary IProto/Response.pm »