AMQP/Channel.pm

filter: Statementstmt 27.2% 68 / 250 Branchbran 14.2% 12 / 84 Conditioncond 2.1% 1 / 46 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 41.3% 19 / 46 Podpod 90.4% 19 / 21 totaltotal 25.2% 119 / 472 CC 68 SCAR 39.5
CC
68
Cov
21%
next_pending
162.7
__ANON__
132.0
exchange_declare
56.0
CRAP 51.8
↑ Summary AMQP/ConnectionManager.pm »
1 package Net::Async::AMQP::Channel;
2 16 $Net::Async::AMQP::Channel::VERSION = '0.031';
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 16 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::Async::AMQP::Channel - represents a single channel in an MQ connection
11  
12 =head1 VERSION
13  
14 version 0.031
15  
16 =head1 SYNOPSIS
17  
18 use IO::Async::Loop;
19 use Net::Async::AMQP;
20 my $loop = IO::Async::Loop->new;
21 $loop->add(my $amqp = Net::Async::AMQP->new);
22 $amqp->connect(
23 host => 'localhost',
24 username => 'guest',
25 password => 'guest',
26 )->then(sub {
27 shift->open_channel->publish(
28 type => 'application/json'
29 )
30 });
31  
32 =head1 DESCRIPTION
33  
34 Each Net::Async::AMQP::Channel instance represents a virtual channel for
35 communicating with the MQ server.
36  
37 Channels are layered over the TCP protocol and most of the common AMQP frames
38 operate at channel level - typically you'd connect to the server, open one
39 channel for one-shot requests such as binding/declaring/publishing, and a further
40 channel for every consumer.
41  
42 Since any error typically results in a closed channel, it's not recommended to
43 have multiple consumers on the same channel if there's any chance the Basic.Consume
44 request will fail.
45  
46 =cut
47  
48 16 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 16 ▶use curry::weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 16 ▶use Class::ISA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 16 ▶use Variable::Disposition qw(retain_future);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 16 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 16 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 16 ▶use Net::Async::AMQP;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 16 ▶use Net::Async::AMQP::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 use overload
59 0 ▶ '""' => sub { shift->as_string },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 76 ▶ '0+' => sub { 0 + shift->id },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 5 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 16 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 =head1 METHODS
65  
66 =cut
67  
68 sub configure {
69 209 ▶ my ($self, %args) = @_;
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
70 209 for(grep exists $args{$_}, qw(amqp)) {
71 209 Scalar::Util::weaken($self->{$_} = delete $args{$_})
72 }
73 209 for(grep exists $args{$_}, qw(future id)) {
74 418 $self->{$_} = delete $args{$_};
75 }
76 209 $self->SUPER::configure(%args);
77 }
78  
79 =head2 confirm_mode
80  
81 Switches confirmation mode on for this channel.
82 In confirm mode, all messages must be ACKed
83 explicitly after delivery.
84  
85 Note that this is an irreversible operation - once
86 confirm mode has been enabled on a channel, closing that
87 channel and reopening is the only way to turn off confirm
88 mode again.
89  
90 Returns a L<Future> which will resolve with this
91 channel once complete.
92  
93 $ch->confirm_mode ==> $ch
94  
95 =cut
96  
97 sub confirm_mode {
98 0 ▶ my $self = shift;
Subroutine confirm_mode: not called CC=1 SCAR=6.9
Pod documented
99 0 my %args = @_;
100 0 $self->debug_printf("Enabling confirm mode");
101  
102 0 my $f = $self->loop->new_future;
103 0 my $frame = Net::AMQP::Frame::Method->new(
104 method_frame => Net::AMQP::Protocol::Confirm::Select->new(
105 nowait => 0,
106 )
107 );
108 0 $self->push_pending(
109 'Confirm::SelectOk' => [ $f, $self ]
110 );
111 0 $self->closure_protection($f);
112 0 $self->send_frame($frame);
113 0 return $f;
114 }
115  
116 =head2 exchange_declare
117  
118 Declares a new exchange.
119  
120 Returns a L<Future> which will resolve with this
121 channel once complete.
122  
123 $ch->exchange_declare(
124 exchange => 'some_exchange',
125 type => 'fanout',
126 autodelete => 1,
127 ) ==> $ch
128  
129 =cut
130  
131 sub exchange_declare {
132 0 ▶ my $self = shift;
Subroutine exchange_declare: not called CC=7 SCAR=40.3
Pod documented
133 0 my %args = @_;
134 0 ▶ die "No exchange specified" unless exists $args{exchange};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"exchange"} 0 0 0
135 0 ▶ die "No exchange type specified" unless exists $args{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"type"} 0 0 0
136  
137 0 $self->debug_printf("Declaring exchange [%s]", $args{exchange});
138  
139 0 my $f = $self->loop->new_future;
140 my $frame = Net::AMQP::Frame::Method->new(
141 method_frame => Net::AMQP::Protocol::Exchange::Declare->new(
142 exchange => Net::AMQP::Value::String->new($args{exchange}),
143 type => Net::AMQP::Value::String->new($args{type}),
144 passive => $args{passive} || 0,
145 durable => $args{durable} || 0,
146 auto_delete => $args{auto_delete} || 0,
147 0 ▶ internal => $args{internal} || 0,
Condition0%0/8
$args{'passive'} || 0
l!l
00
$args{'durable'} || 0
l!l
00
$args{'auto_delete'} || 0
l!l
00
$args{'internal'} || 0
l!l
00
MC/DC0%0/1
$args{'passive'} || 0
$args{'passive'}
MC/DC0%0/1
$args{'durable'} || 0
$args{'durable'}
MC/DC0%0/1
$args{'auto_delete'} || 0
$args{'auto_delete'}
MC/DC0%0/1
$args{'internal'} || 0
$args{'internal'}
Truth table0%0/2
A
A= $args{'passive'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'durable'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'auto_delete'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'internal'}
Aresult
11
00
148 ticket => 0,
149 nowait => 0,
150 )
151 );
152 0 $self->push_pending(
153 'Exchange::DeclareOk' => [ $f, $self ]
154 );
155 0 $self->closure_protection($f);
156 0 $self->send_frame($frame);
157 0 return $f;
158 }
159  
160 =head2 exchange_bind
161  
162 Binds an exchange to another exchange. This is a RabbitMQ-specific extension.
163  
164 =cut
165  
166 sub exchange_bind {
167 0 ▶ my $self = shift;
Subroutine exchange_bind: not called CC=4 SCAR=30.0
Pod documented
168 0 my %args = @_;
169 0 ▶ die "No source exchange specified" unless exists $args{source};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"source"} 0 0 0
170 0 ▶ die "No destination exchange specified" unless exists $args{destination};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"destination"} 0 0 0
171  
172 0 $self->debug_printf("Binding exchange [%s] to [%s] with rkey [%s]", $args{source}, $args{destination}, $args{routing_key});
173  
174 0 my $f = $self->loop->new_future;
175 my $frame = Net::AMQP::Frame::Method->new(
176 method_frame => Net::AMQP::Protocol::Exchange::Bind->new(
177 source => Net::AMQP::Value::String->new($args{source}),
178 destination => Net::AMQP::Value::String->new($args{destination}),
179 0 ▶ (exists($args{routing_key}) ? ('routing_key' => Net::AMQP::Value::String->new($args{routing_key})) : ()),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'routing_key'} ? : 0 0 0
180 nowait => 0,
181 )
182 );
183 0 $self->push_pending(
184 'Exchange::BindOk' => [ $f, $self ]
185 );
186 0 $self->closure_protection($f);
187 0 $self->send_frame($frame);
188 0 return $f;
189 }
190  
191 =head2 queue_declare
192  
193 Returns a L<Future> which will resolve with the
194 new L<Net::Async::AMQP::Queue> instance once complete.
195  
196 $ch->queue_declare(
197 queue => 'some_queue',
198 ) ==> $q
199  
200 =cut
201  
202 sub queue_declare {
203 0 ▶ my $self = shift;
Subroutine queue_declare: not called CC=2 SCAR=17.9
Pod documented
204 0 my %args = @_;
205 0 ▶ die "No queue specified" unless defined $args{queue};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"queue"} 0 0 0
206  
207 $self->future->then(sub {
208 0 ▶ my $f = $self->loop->new_future;
Subroutine __ANON__: not called CC=7 SCAR=40.3
209 0 my $ready = $self->loop->new_future;
210 0 my $q = Net::Async::AMQP::Queue->new(
211 amqp => $self->amqp,
212 future => $ready,
213 );
214 0 $self->debug_printf("Declaring queue [%s]", $args{queue});
215 my $frame = Net::AMQP::Frame::Method->new(
216 method_frame => Net::AMQP::Protocol::Queue::Declare->new(
217 queue => Net::AMQP::Value::String->new($args{queue}),
218 passive => $args{passive} || 0,
219 durable => $args{durable} || 0,
220 exclusive => $args{exclusive} || 0,
221 auto_delete => $args{auto_delete} || 0,
222 no_ack => $args{no_ack} || 0,
223 ($args{arguments}
224 ? (arguments => $args{arguments})
225 0 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
$args{'arguments'} ? : 0 0 0
Condition0%0/10
$args{'passive'} || 0
l!l
00
$args{'durable'} || 0
l!l
00
$args{'exclusive'} || 0
l!l
00
$args{'auto_delete'} || 0
l!l
00
$args{'no_ack'} || 0
l!l
00
MC/DC0%0/1
$args{'passive'} || 0
$args{'passive'}
MC/DC0%0/1
$args{'durable'} || 0
$args{'durable'}
MC/DC0%0/1
$args{'exclusive'} || 0
$args{'exclusive'}
MC/DC0%0/1
$args{'auto_delete'} || 0
$args{'auto_delete'}
MC/DC0%0/1
$args{'no_ack'} || 0
$args{'no_ack'}
Truth table0%0/2
A
A= $args{'passive'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'durable'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'exclusive'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'auto_delete'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'no_ack'}
Aresult
11
00
226 ),
227 ticket => 0,
228 nowait => 0,
229 )
230 );
231 $self->push_pending(
232 'Queue::DeclareOk' => sub {
233 0 ▶ my ($amqp, $frame) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
234 0 my $method_frame = $frame->method_frame;
235 0 $q->queue_name($method_frame->queue);
236 0 ▶ $ready->done() unless $ready->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $ready->is_ready 0 0 0
237 0 ▶ $f->done($q) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
238 }
239 0 );
240 0 $self->closure_protection($f);
241 0 $self->send_frame($frame);
242 0 $f;
243 })
244 0 }
245  
246 =head2 publish
247  
248 Publishes a message on this channel.
249  
250 Returns a L<Future> which will resolve with the
251 channel instance once the server has confirmed publishing is complete.
252  
253 $ch->publish(
254 exchange => 'some_exchange',
255 routing_key => 'some.rkey.here',
256 type => 'some_type',
257 ) ==> $ch
258  
259 Some named parameters currently accepted - note that this list is likely to
260 expand in future:
261  
262 =over 4
263  
264 =item * ack - we default to ACK mode, so set this to 0 to turn off explicit server ACK
265 on message routing/delivery
266  
267 =item * immediate - if set, will cause a failure if the message could not be routed
268 immediately to a consumer
269  
270 =item * mandatory - if set, will require that the message ends up in a queue (i.e. will
271 fail messages sent to an exchange that do not have an appropriate binding)
272  
273 =item * content_type - defaults to application/binary
274  
275 =item * content_encoding - defaults to undef (none)
276  
277 =item * timestamp - the message timestamp, defaults to epoch time
278  
279 =item * expiration - use this to set per-message expiry, see L<https://www.rabbitmq.com/ttl.html>
280  
281 =item * priority - defaults to undef (none), use this to take advantage of RabbitMQ 3.5+ priority support
282  
283 =back
284  
285 =cut
286  
287 sub publish {
288 0 ▶ my $self = shift;
Subroutine publish: not called CC=2 SCAR=17.9
Pod documented
289 0 my %args = @_;
290 0 ▶ die "no exchange" unless exists $args{exchange};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"exchange"} 0 0 0
291  
292 $self->future->then(sub {
293 0 ▶ my $f = $self->loop->new_future;
Subroutine __ANON__: not called CC=11 SCAR=48.8
294 { # When publishing a message, we should expect either an ACK, or a return.
295 # Since these are mutually exclusive, we also need to remove the pending
296 # handler for the opposing event once one event has been received. Note
297 # that this crosslinking gives us an unfortunate cycle which we resolve
298 # by weakening the opposite handler once we've removed it.
299 0 my $return;
300 my $ack = sub {
301 0 ▶ my ($amqp, $frame) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
302 0 my $method_frame = $frame->method_frame;
303 0 $self->remove_pending('Basic::Return' => $return);
304 0 ▶ $f->done unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
305 0 weaken $return;
306 0 };
307 $return = sub {
308 0 ▶ my ($amqp, $frame) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
309 0 my $method_frame = $frame->method_frame;
310 # $self->remove_pending('Basic::Ack' => $ack);
311 0 ▶ $f->fail(
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
312 $method_frame->reply_text,
313 code => $method_frame->reply_code,
314 exchange => $method_frame->exchange,
315 rkey => $method_frame->routing_key
316 ) unless $f->is_ready;
317 0 weaken $ack;
318 0 };
319 0 $self->push_pending(
320 'Basic::Return' => $return,
321 );
322 0 $self->push_pending(
323 'Basic::Ack' => $ack,
324 );
325 }
326  
327 my @frames = $self->amqp->split_payload(
328 $args{payload},
329 exchange => Net::AMQP::Value::String->new($args{exchange}),
330 mandatory => $args{mandatory} // 0,
331 immediate => $args{immediate} // 0,
332 (exists $args{routing_key} ? (routing_key => Net::AMQP::Value::String->new($args{routing_key})) : ()),
333 ticket => 0,
334 content_type => $args{content_type} // 'application/binary',
335 content_encoding => $args{content_encoding},
336 timestamp => $args{timestamp} // time,
337 type => Net::AMQP::Value::String->new($args{type}),
338 user_id => $self->amqp->user,
339 no_ack => (
340 # inverse flags are a pain... we default to ACK mode.
341 # If the ack parameter is given, this overrides the
342 # setting - so ack => 1 means no_ack => 0, ack => 0 means no_ack => 1,
343 # and lack of ack means no_ack => 0.
344 exists $args{ack}
345 ? (
346 $args{ack} ? 0 : 1
347 ) : 0
348 ),
349 # headers => {
350 # type => $args{type},
351 # },
352 delivery_mode => 1,
353 priority => $args{priority} // 1,
354 correlation_id => undef,
355 expiration => (
356 exists $args{expiration}
357 # This would seem to make more sense as a numeric value, but the spec
358 # defines this as a shortstr
359 ? Net::AMQP::Value::String->new($args{expiration})
360 0 ▶ : undef
Branch: 4 branches
BranchTrueFalseTotal
exists $args{'routing_key'} ? : 0 0 0
exists $args{'ack'} ? : 0 0 0
$args{'ack'} ? : 0 0 0
exists $args{'expiration'} ? : 0 0 0
Condition0%0/11
$args{"mandatory"} // 0
l!l
00
$args{"immediate"} // 0
l!l
00
$args{"content_type"} // "application/binary"
l!l
00
$args{"timestamp"} // time
l!l&&r!l&&!r
000
$args{"priority"} // 1
l!l
00
MC/DC0%0/1
$args{"mandatory"} // 0
$args{"mandatory"}
MC/DC0%0/1
$args{"immediate"} // 0
$args{"immediate"}
MC/DC0%0/1
$args{"content_type"} // "application/binary"
$args{"content_type"}
MC/DC0%0/2
$args{"timestamp"} // time
$args{"timestamp"} time
MC/DC0%0/1
$args{"priority"} // 1
$args{"priority"}
Truth table0%0/2
A
A= $args{"mandatory"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"immediate"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"content_type"}
Aresult
11
00
Truth table0%0/3
A // B
A= $args{"timestamp"}B= time
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{"priority"}
Aresult
11
00
361 ),
362 message_id => undef,
363 app_id => undef,
364 cluster_id => undef,
365 weight => 0,
366 );
367 0 $self->closure_protection($f);
368 $self->send_frame(
369 $_,
370 0 ) for @frames;
371 0 $f
372 })
373 0 }
374  
375 =head2 qos
376  
377 Changes QOS settings on the channel. Probably most
378 useful for limiting the number of messages that can
379 be delivered to us before we have to ACK/NAK to
380 proceed.
381  
382 Returns a L<Future> which will resolve with the
383 channel instance once the operation is complete.
384  
385 $ch->qos(
386 prefetch_count => 5,
387 prefetch_size => 1048576,
388 ) ==> $ch
389  
390 =cut
391  
392 sub qos {
393 0 ▶ my $self = shift;
Subroutine qos: not called CC=1 SCAR=6.9
Pod documented
394 0 my %args = @_;
395  
396 $self->future->then(sub {
397 0 ▶ my $f = $self->loop->new_future;
Subroutine __ANON__: not called CC=2 SCAR=17.9
398 0 my $channel = $self->id;
399 0 $self->push_pending(
400 'Basic::QosOk' => [ $f, $self ],
401 );
402  
403 my $frame = Net::AMQP::Frame::Method->new(
404 method_frame => Net::AMQP::Protocol::Basic::Qos->new(
405 nowait => 0,
406 prefetch_count => $args{prefetch_count},
407 0 ▶ prefetch_size => $args{prefetch_size} || 0,
Condition0%0/2
$args{'prefetch_size'} || 0
l!l
00
MC/DC0%0/1
$args{'prefetch_size'} || 0
$args{'prefetch_size'}
Truth table0%0/2
A
A= $args{'prefetch_size'}
Aresult
11
00
408 )
409 );
410 0 $self->closure_protection($f);
411 0 $self->send_frame($frame);
412 0 $f
413 0 });
414 }
415  
416 =head2 ack
417  
418 Acknowledge a specific delivery.
419  
420 Returns a L<Future> which will resolve with the
421 channel instance once the operation is complete.
422  
423 $ch->ack(
424 delivery_tag => 123,
425 ) ==> $ch
426  
427 =cut
428  
429 sub ack {
430 0 ▶ my $self = shift;
Subroutine ack: not called CC=1 SCAR=6.9
Pod documented
431 0 my %args = @_;
432  
433 0 my $id = $self->id;
434 $self->future->on_done(sub {
435 0 ▶ my $channel = $id;
Subroutine __ANON__: not called CC=2 SCAR=17.9
436 my $frame = Net::AMQP::Frame::Method->new(
437 method_frame => Net::AMQP::Protocol::Basic::Ack->new(
438 # nowait => 0,
439 delivery_tag => $args{delivery_tag},
440 0 ▶ multiple => $args{multiple} // 0,
Condition0%0/2
$args{"multiple"} // 0
l!l
00
MC/DC0%0/1
$args{"multiple"} // 0
$args{"multiple"}
Truth table0%0/2
A
A= $args{"multiple"}
Aresult
11
00
441 )
442 );
443 0 $self->send_frame($frame);
444 0 });
445 }
446  
447 =pod
448  
449 Example output:
450  
451 'method_id' => 40,
452 'reply_code' => 404,
453 'class_id' => 60,
454 'reply_text' => 'NOT_FOUND - no exchange \'invalidchan\' in vhost \'vhost\''
455  
456 =cut
457  
458 =head2 on_close
459  
460 Called when the channel has been closed.
461  
462 =cut
463  
464 sub on_close {
465 0 ▶ my ($self, $frame) = @_;
Subroutine on_close: not called CC=2 SCAR=17.9
Pod documented
466  
467 0 $self->{is_closed} = 1;
468  
469 # ACK the close first - we have to send a close-ok
470 # before it's legal to reopen this channel ID
471 retain_future(
472 (
473 # If we initiated the close, then the CloseOk comes from the server
474 $self->{closing}
475 ? Future->done
476 : $self->send_frame(
477 Net::AMQP::Frame::Method->new(
478 method_frame => Net::AMQP::Protocol::Channel::CloseOk->new(
479 )
480 )
481 )
482 )->then(sub {
483 # It's important that the MQ instance knows
484 # about the channel closure first before we
485 # go ahead and dispatch events, since any
486 # subscribed handlers might go ahead and
487 # attempt to open the channel again immediately.
488 0 ▶ $self->amqp->channel_closed($self->id);
Subroutine __ANON__: not called CC=1 SCAR=6.9
489 0 $self->bus->invoke_event(
490 'close',
491 code => $frame->reply_code,
492 message => $frame->reply_text,
493 );
494 0 Future->done
495 })
496 0 ▶ )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'closing'} ? : 0 0 0
497 }
498  
499 =head2 send_frame
500  
501 Proxy frame sending requests to the parent
502 L<Net::Async::AMQP> instance.
503  
504 =cut
505  
506 sub send_frame {
507 0 ▶ my $self = shift;
Subroutine send_frame: not called CC=1 SCAR=6.9
Pod documented
508 0 $self->amqp->send_frame(
509 @_,
510 channel => $self->id,
511 )
512 }
513  
514 =head2 close
515  
516 Ask the server to close this channel.
517  
518 Returns a L<Future> which will resolve with the
519 channel instance once the operation is complete.
520  
521 $ch->close(
522 code => 404,
523 text => 'something went wrong',
524 ) ==> $ch
525  
526 =cut
527  
528 sub close {
529 0 ▶ my $self = shift;
Subroutine close: not called CC=5 SCAR=34.0
Pod documented
530 0 my %args = @_;
531 0 $self->debug_printf("Close channel %d", $self->id);
532  
533 # There's a slight chance we'll get called after being
534 # removed from the loop, since we wanted to close anyway then
535 # don't treat that as an error
536 0 ▶ return Future->done if $self->{closing} or !$self->loop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closing"} or not $self->loop 0 0 0
Condition0%0/3
$self->{"closing"} or not $self->loop
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"closing"} or not $self->loop
$self->{"closing"} not $self->loop
Truth table0%0/3
A or not(B)
A= $self->{"closing"}B= not $self->loop
ABresult
1X1
011
000
537  
538 0 $self->{closing} = 1;
539  
540 0 my $f = $self->loop->new_future;
541 my $frame = Net::AMQP::Frame::Method->new(
542 method_frame => Net::AMQP::Protocol::Channel::Close->new(
543 reply_code => $args{code} // 404,
544 0 ▶ reply_text => $args{text} // 'closing',
Condition0%0/4
$args{"code"} // 404
l!l
00
$args{"text"} // "closing"
l!l
00
MC/DC0%0/1
$args{"code"} // 404
$args{"code"}
MC/DC0%0/1
$args{"text"} // "closing"
$args{"text"}
Truth table0%0/2
A
A= $args{"code"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"text"}
Aresult
11
00
545 )
546 );
547 0 $self->push_pending(
548 'Channel::CloseOk' => [ $f, $self ],
549 );
550 0 $self->closure_protection($f);
551 0 $self->send_frame($frame);
552 0 return $f;
553 }
554  
555 =head2 push_pending
556  
557 =cut
558  
559 sub push_pending {
560 1 ▶ my $self = shift;
Subroutine push_pending: called CC=2 SCAR=6.9
Pod documented
561 1 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
562 1 my ($type, $code) = splice @_, 0, 2;
563 1 push @{$self->{pending}{$type}}, $code;
564 }
565 1 return $self;
566 }
567  
568 =head2 remove_pending
569  
570 Removes a coderef from the pending event handler.
571  
572 Returns C< $self >.
573  
574 =cut
575  
576 sub remove_pending {
577 0 ▶ my $self = shift;
Subroutine remove_pending: not called CC=3 SCAR=24.8
Pod documented
578 0 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
579 0 my ($type, $code) = splice @_, 0, 2;
580 # This is the same as extract_by { $_ eq $code } @{$self->{pending}{$type}};,
581 # but since we'll be calling it a lot might as well do it inline:
582 splice
583 0 @{$self->{pending}{$type}},
584 $_,
585 0 1 for grep {
586 0 $self->{pending}{$type}[$_] eq $code
587 0 } reverse 0..$#{$self->{pending}{$type}};
588 }
589 0 return $self;
590 }
591  
592 =head2 next_pending
593  
594 Retrieves the next pending handler for the given incoming frame type (see L<Net::Async::AMQP::Utils/amqp_frame_type>),
595 and calls it.
596  
597 Takes the following parameters:
598  
599 =over 4
600  
601 =item * $frame - the frame itself
602  
603 =back
604  
605 Returns $self.
606  
607 =cut
608  
609 sub next_pending {
610 1 ▶ my ($self, $frame) = @_;
Subroutine next_pending: called CC=19 SCAR=50.9
Pod documented
611  
612 # First part of a frame. There's more to come, so stash a new future
613 # and return.
614 1 ▶ if($frame->isa('Net::AMQP::Frame::Header')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->isa("Net::AMQP::Frame::Header")) 0 1 1
615 # Start with empty payload
616 0 $self->{incoming_message}{type} = $frame->header_frame->type;
617 0 ▶ if($frame->header_frame->headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->header_frame->headers) 0 0 0
618 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"incoming_message"}{"type"} = $frame->header_frame->headers->{"type"} if exists $frame->header_frame->headers->{"type"}; 1 } } or $self->debug_printf("Unexpected exception while doing something: %s", $@) 0 0 0
619 $self->{incoming_message}{type} = $frame->header_frame->headers->{type}
620 0 ▶ if exists $frame->header_frame->headers->{type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $frame->header_frame->headers->{"type"} 0 0 0
621 0 1
622 } or $self->debug_printf("Unexpected exception while doing something: %s", $@);
623 }
624  
625 # Messages may be empty - in this case we'd have no body frames at all, we're done already:
626 0 ▶ if($frame->body_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->body_size) { } 0 0 0
627 0 $self->{incoming_message}{pending} = $frame->body_size;
628 } else {
629 $self->bus->invoke_event(
630 0 message => @{$self->{incoming_message}}{qw(type payload ctag dtag rkey)},
631 );
632 0 delete $self->{incoming_message};
633 }
634  
635 0 return $self;
636 }
637  
638 # Body part of an incoming message.
639 1 ▶ if($frame->isa('Net::AMQP::Frame::Body')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->isa("Net::AMQP::Frame::Body")) 0 1 1
640 0 my $bytes = $frame->payload;
641 0 $self->{incoming_message}{payload} .= $bytes;
642 0 $self->{incoming_message}{pending} -= length $bytes;
643 0 ▶ if($self->{incoming_message}{pending} > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'incoming_message'}{'pending'} > 0) { } 0 0 0
elsif ($self->{'incoming_message'}{'pending'} < 0) { } 0 0 0
644 # We still have more to come, just return for now
645 0 return $self;
646 } elsif($self->{incoming_message}{pending} < 0) {
647 $self->close(
648 code => 500,
649 0 text => -$self->{incoming_message}{pending} . ' excess payload bytes detected in delivery'
650 );
651 0 delete $self->{incoming_message};
652 0 return $self;
653 } else {
654 # We have a full message now - hand it over to the event bus
655 $self->bus->invoke_event(
656 0 message => @{$self->{incoming_message}}{qw(type payload ctag dtag rkey)},
657 );
658 0 delete $self->{incoming_message};
659 0 return $self;
660 }
661 }
662  
663 1 ▶ return $self unless $frame->can('method_frame') && (my $method_frame = $frame->method_frame);
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->can("method_frame") and my $method_frame = $frame->method_frame 0 1 1
Condition33%1/3
$frame->can("method_frame") and my $method_frame = $frame->method_frame
!ll&&!rl&&r
001
MC/DC0%0/2
$frame->can("method_frame") and my $method_frame = $frame->method_frame
$frame->can("method_frame") my $method_frame = $frame->method_frame
Truth table33%1/3
A and B
A= $frame->can("method_frame")B= my $method_frame = $frame->method_frame
ABresult
0X0
100
111
664 1 my $type = amqp_frame_type($frame);
665 1 ▶ if($type eq 'Basic::CancelOk') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Basic::CancelOk") 0 1 1
666 0 my ($ctag) = ($method_frame->consumer_tag);
667 0 $self->debug_printf("Cancel $ctag");
668 0 $self->bus->invoke_event(
669 'cancel',
670 ctag => $ctag,
671 );
672 # Also raise this as a "listener_stop"
673 # event, for managed channels
674 0 $self->bus->invoke_event(
675 listener_stop => $ctag
676 );
677 }
678  
679 # Message delivery, part 3: The "Deliver" message.
680 # This is actually where we start.
681 1 ▶ if($type eq 'Basic::Deliver') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Basic::Deliver") 0 1 1
682 0 ▶ $self->debug_printf("Already have incoming_message?") if exists $self->{incoming_message};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"incoming_message"} 0 0 0
683 $self->{incoming_message} = {
684 0 ctag => $method_frame->consumer_tag,
685 dtag => $method_frame->delivery_tag,
686 rkey => $method_frame->routing_key,
687 payload => '',
688 payload_size => undef,
689 };
690 0 return $self;
691 }
692  
693 1 ▶ if($type eq 'Channel::Close') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Channel::Close") 0 1 1
694 0 $self->debug_printf("Channel was %d, calling close", $frame->channel);
695 0 $self->on_close(
696 $method_frame
697 );
698 0 return $self;
699 }
700  
701 1 ▶ if(my $next = shift @{$self->{pending}{$type} || []}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $next = shift @{$self->{"pending"}{$type} or [];}) 1 0 1
$self->{"pending"}{$type} or [] 0 1 1
702 # We have a registered handler for this frame type. This usually
703 # means that we've sent a message and are awaiting a response.
704 1 ▶ if(ref($next) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next eq 'ARRAY') { } 0 1 1
705 0 my ($f, @args) = @$next;
706 0 ▶ $f->done(@args) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
707 } else {
708 1 $next->($self, $frame, @_);
709 }
710 1 return $self;
711 }
712  
713 # It's quite possible we'll see unsolicited frames back from
714 # the server. We don't expect many so report them when in debug mode.
715 0 $self->debug_printf("We had no pending handlers for [%s]", $type);
716 0 return $self;
717 }
718  
719 =head1 METHODS - Accessors
720  
721 =cut
722  
723 =head2 amqp
724  
725 The parent L<Net::Async::AMQP> instance.
726  
727 =cut
728  
729 0 ▶sub amqp { shift->{amqp} }
Subroutine amqp: not called CC=1 SCAR=6.9
Pod documented
730  
731 =head2 bus
732  
733 Event bus. Used for sharing channel-specific events.
734  
735 =cut
736  
737 0 ▶sub bus { $_[0]->{bus} ||= Mixin::Event::Dispatch::Bus->new }
Subroutine bus: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$_[0]{"bus"} ||= "Mixin::Event::Dispatch::Bus"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"bus"} ||= "Mixin::Event::Dispatch::Bus"->new
$_[0]{"bus"} "Mixin::Event::Dispatch::Bus"->new
Truth table0%0/3
A ||= B
A= $_[0]{"bus"}B= "Mixin::Event::Dispatch::Bus"->new
ABresult
1X1
011
000
738  
739 =head2 future
740  
741 The underlying L<Future> for this channel.
742  
743 Will resolve to the L<Net::Async::Channel> instance once the channel is open.
744  
745 =cut
746  
747 1 ▶sub future { shift->{future} }
Subroutine future: called CC=1 SCAR=0.0
Pod documented
748  
749 =head2 id
750  
751 This channel ID.
752  
753 =cut
754  
755 sub id {
756 284 ▶ my $self = shift;
Subroutine id: called CC=2 SCAR=9.2
Pod documented
757 284 ▶ return $self->{id} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 284 0 284
758 0 $self->{id} = shift;
759 0 $self
760 }
761  
762 sub as_string {
763 0 ▶ my $self = shift;
Subroutine as_string: not called CC=1 SCAR=6.9
Pod undocumented
764 0 sprintf "Channel[%d]", $self->id;
765 }
766  
767 =head2 closed
768  
769 Returns true if the channel has been closed, 1 if not (which could mean it is either not yet open,
770 or that it is open and has not yet been closed by either side).
771  
772 =cut
773  
774 0 ▶sub is_closed { shift->{is_closed} }
Subroutine is_closed: not called CC=1 SCAR=6.9
Pod undocumented
775  
776 =head2 closure_protection
777  
778 Helper method for marking any outstanding requests as failed when the channel closes.
779  
780 Takes a L<Future>, returns a L<Future> (probably the same one).
781  
782 =cut
783  
784 sub closure_protection {
785 0 ▶ my ($self, $f) = @_;
Subroutine closure_protection: not called CC=3 SCAR=24.8
Pod documented
786 0 ▶ unless($f) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f) 0 0 0
787 0 $self->debug_printf("Closure protection requested for future which has already disappeared");
788 0 return Future->fail(closed => 'future has already been released');
789 }
790  
791 # No sense in proceeding if the Future has already completed
792 0 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 0 0
793  
794 0 my @ev;
795 0 my $bus = $self->bus;
796 $bus->subscribe_to_event(
797 @ev = (close => sub {
798 0 ▶ my ($ev, %args) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
799 0 $self->debug_printf("Closed channel, code %d, reason: %s", $args{code}, $args{reason});
800 0 ▶ unless($f) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f) 0 0 0
801 0 $self->debug_printf("Future has disappeared already, not marking as failed");
802 # We should have unsubscribed already, but do this just in case.
803 0 splice @ev;
804 0 eval { $ev->unsubscribe; };
805 0 return;
806 }
807  
808 0 ▶ $f->fail(closed => %args) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
809 })
810 0 );
811  
812 # Use return value from ->on_ready, since we may clear $f immediately if the future is already
813 # marked as ready.
814 $f->on_ready(sub {
815 0 ▶ $bus->unsubscribe_from_event(splice @ev);
Subroutine __ANON__: not called CC=1 SCAR=6.9
816 0 undef $f;
817 0 });
818 }
819  
820 16 1;
821  
822 __END__
↑ Summary AMQP/ConnectionManager.pm »