AMQP/Channel.pm

filter: Statementstmt 20.5% 69 / 336 Branchbran 13.7% 16 / 116 Conditioncond 3.5% 3 / 84 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 35.8% 19 / 53 Podpod 81.4% 22 / 27 totaltotal 19.5% 129 / 661 CC 107 SCAR 50.5
CC
107
Cov
16%
next_pending
590.6
__ANON__
210.0
exchange_declare
72.0
CRAP 156.7
↑ Summary AMQP/ConnectionManager.pm »
1 package Net::Async::AMQP::Channel;
2 19 $Net::Async::AMQP::Channel::VERSION = '2.000';
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶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 2.000
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 19 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 19 ▶use curry::weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 19 ▶use Class::ISA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 19 ▶use Variable::Disposition qw(retain_future);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 19 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 19 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 19 ▶use Net::Async::AMQP;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 19 ▶use Net::Async::AMQP::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 use overload
59 0 ▶ '""' => sub { shift->as_string },
Subroutine BEGIN: called CC=1 SCAR=0.0
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 3 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 19 fallback => 1;
63  
64 =head1 METHODS
65  
66 =cut
67  
68 209 ▶sub configure {
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
69 209 my ($self, %args) = @_;
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 0 ▶sub confirm_mode {
Subroutine confirm_mode: not called CC=3 SCAR=24.8
Pod documented
98 0 my $self = shift;
99 0 my %args = @_;
100 0 $self->debug_printf("Enabling confirm mode");
101 0 ▶ die "already requested confirm_mode for this channel" if $self->{confirm_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"confirm_mode"} 0 0 0
102  
103 0 my $f = $self->loop->new_future->set_label("set confirm_mode on channel " . $self->id);
104 0 $self->{delivery_tag} = 0;
105 0 $self->{confirm_mode} = $f;
106 0 my $nowait = $self->nowait_from_args(%args);
107 0 my $frame = Net::AMQP::Frame::Method->new(
108 method_frame => Net::AMQP::Protocol::Confirm::Select->new(
109 nowait => $nowait,
110 )
111 );
112  
113 # No-wait mode means we don't expect the SelectOk frame back
114 0 ▶ if($nowait) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nowait) { } 0 0 0
115 0 $f->done
116 } else {
117 0 $self->closure_protection($f);
118 }
119 0 $self->send_frame($frame);
120 0 ▶ return $f->transform(done => sub { $self });
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 }
122  
123 =head2 nowait_from_args
124  
125 If we have a C<wait> argument, then return the inverse of that.
126  
127 Otherwise, return zero.
128  
129 =cut
130  
131 0 ▶sub nowait_from_args {
Subroutine nowait_from_args: not called CC=3 SCAR=24.8
Pod documented
132 0 my ($self, %args) = @_;
133 0 ▶ return 0 unless exists $args{wait};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"wait"} 0 0 0
134 0 ▶ return $args{wait} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$args{'wait'} ? : 0 0 0
135 }
136  
137 =head2 exchange_declare
138  
139 Declares a new exchange.
140  
141 Returns a L<Future> which will resolve with this
142 channel once complete.
143  
144 $ch->exchange_declare(
145 exchange => 'some_exchange',
146 type => 'fanout',
147 autodelete => 1,
148 ) ==> $ch
149  
150 =cut
151  
152 0 ▶sub exchange_declare {
Subroutine exchange_declare: not called CC=8 SCAR=42.8
Pod documented
153 0 my $self = shift;
154 0 my %args = @_;
155 0 ▶ die "No exchange specified" unless exists $args{exchange};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"exchange"} 0 0 0
156 0 ▶ die "No exchange type specified" unless exists $args{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"type"} 0 0 0
157  
158 0 ▶ $args{exchange} //= '';
Condition0%0/2
$args{"exchange"} //= ""
l!l
00
MC/DC0%0/1
$args{"exchange"} //= ""
$args{"exchange"}
Truth table0%0/2
A
A= $args{"exchange"}
Aresult
11
00
159 0 $self->debug_printf("Declaring exchange [%s]", $args{exchange});
160  
161 0 my $f = $self->loop->new_future->set_label("declare exchange [" . $args{exchange} . "]");
162 my $frame = Net::AMQP::Frame::Method->new(
163 method_frame => Net::AMQP::Protocol::Exchange::Declare->new(
164 exchange => Net::AMQP::Value::String->new($args{exchange}),
165 type => Net::AMQP::Value::String->new($args{type}),
166 passive => $args{passive} || 0,
167 durable => $args{durable} || 0,
168 auto_delete => $args{auto_delete} || 0,
169 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
170 ticket => 0,
171 nowait => 0,
172 )
173 );
174 0 $self->push_pending(
175 'Exchange::DeclareOk' => [ $f, $self ]
176 );
177 0 $self->closure_protection($f);
178 0 $self->send_frame($frame);
179 0 return $f;
180 }
181  
182 =head2 exchange_bind
183  
184 Binds an exchange to another exchange. This is a RabbitMQ-specific extension.
185  
186 =cut
187  
188 0 ▶sub exchange_bind {
Subroutine exchange_bind: not called CC=5 SCAR=34.0
Pod documented
189 0 my $self = shift;
190 0 my %args = @_;
191 0 ▶ die "No source exchange specified" unless exists $args{source};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"source"} 0 0 0
192 0 ▶ die "No destination exchange specified" unless exists $args{destination};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"destination"} 0 0 0
193  
194 0 $self->debug_printf("Binding exchange [%s] to [%s] with rkey [%s]", $args{source}, $args{destination}, $args{routing_key});
195  
196 0 ▶ my $f = $self->loop->new_future->set_label("bind exchange [" . $args{source} . "] to [" . $args{destination} . "]" . (exists $args{routing_key} ? (" rkey [" . $args{routing_key} . "]") : ""));
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'routing_key'} ? : 0 0 0
197 my $frame = Net::AMQP::Frame::Method->new(
198 method_frame => Net::AMQP::Protocol::Exchange::Bind->new(
199 source => Net::AMQP::Value::String->new($args{source}),
200 destination => Net::AMQP::Value::String->new($args{destination}),
201 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
202 nowait => 0,
203 )
204 );
205 0 $self->push_pending(
206 'Exchange::BindOk' => [ $f, $self ]
207 );
208 0 $self->closure_protection($f);
209 0 $self->send_frame($frame);
210 0 return $f;
211 }
212  
213 =head2 queue_declare
214  
215 Returns a L<Future> which will resolve with the new L<Net::Async::AMQP::Queue> instance,
216 the number of messages in the queue, and the number of consumers.
217  
218 $ch->queue_declare(
219 queue => 'some_queue',
220 ) ==> ($q, $message_count, $consumer_count)
221  
222 =cut
223  
224 0 ▶sub queue_declare {
Subroutine queue_declare: not called CC=2 SCAR=17.9
Pod documented
225 0 my $self = shift;
226 0 my %args = @_;
227 0 ▶ die "No queue specified" unless defined $args{queue};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"queue"} 0 0 0
228  
229 0 ▶ $self->future->then(sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
230 0 my $f = $self->loop->new_future->set_label("declare queue [" . $args{queue} . "]");
231 0 my $ready = $self->loop->new_future->set_label("queue readiness for [" . $args{queue} . "]");
232 0 my $q = Net::Async::AMQP::Queue->new(
233 amqp => $self->amqp,
234 future => $ready,
235 );
236 0 $self->debug_printf("Declaring queue [%s]", $args{queue});
237 my $frame = Net::AMQP::Frame::Method->new(
238 method_frame => Net::AMQP::Protocol::Queue::Declare->new(
239 queue => Net::AMQP::Value::String->new($args{queue}),
240 passive => $args{passive} || 0,
241 durable => $args{durable} || 0,
242 exclusive => $args{exclusive} || 0,
243 auto_delete => $args{auto_delete} || 0,
244 no_ack => $args{no_ack} || 0,
245 ($args{arguments}
246 ? (arguments => $args{arguments})
247 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
248 ),
249 ticket => 0,
250 nowait => 0,
251 )
252 );
253 $self->push_pending(
254 0 ▶ 'Queue::DeclareOk' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
255 0 my ($amqp, $frame) = @_;
256 0 my $method_frame = $frame->method_frame;
257 0 $q->queue_name($method_frame->queue);
258 0 my $messages = $method_frame->message_count;
259 0 my $consumer_count = $method_frame->consumer_count;
260 0 ▶ $ready->done() unless $ready->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $ready->is_ready 0 0 0
261 0 ▶ $f->done($q, $messages, $consumer_count) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
262 }
263 0 );
264 0 $self->closure_protection($f);
265 0 $self->send_frame($frame);
266 0 $f;
267 })
268 0 }
269  
270 0 ▶sub next_dtag { ++shift->{delivery_tag} }
Subroutine next_dtag: not called CC=1 SCAR=6.9
Pod undocumented
271  
272 =head2 publish
273  
274 Publishes a message on this channel.
275  
276 Returns a L<Future> which will resolve with the
277 channel instance once the server has confirmed publishing is complete.
278  
279 $ch->publish(
280 exchange => 'some_exchange',
281 routing_key => 'some.rkey.here',
282 type => 'some_type',
283 ) ==> $ch
284  
285 Some named parameters currently accepted - note that this list is likely to
286 expand in future:
287  
288 =over 4
289  
290 =item * ack - we default to ACK mode, so set this to 0 to turn off explicit server ACK
291 on message routing/delivery
292  
293 =item * immediate - if set, will cause a failure if the message could not be routed
294 immediately to a consumer
295  
296 =item * mandatory - if set, will require that the message ends up in a queue (i.e. will
297 fail messages sent to an exchange that do not have an appropriate binding)
298  
299 =item * content_type - defaults to application/binary
300  
301 =item * content_encoding - defaults to undef (none)
302  
303 =item * timestamp - the message timestamp, defaults to epoch time
304  
305 =item * expiration - use this to set per-message expiry, see L<https://www.rabbitmq.com/ttl.html>
306  
307 =item * priority - defaults to undef (none), use this to take advantage of RabbitMQ 3.5+ priority support
308  
309 =item * reply_to - which queue to reply to (used for RPC, default undef)
310  
311 =item * correlation_id - unique message ID (used for RPC, default undef)
312  
313 =item * delivery_mode - whether to persist message (default 1, don't persist - set to 2 for persistent, see also "durable" flag for queues)
314  
315 =back
316  
317 =cut
318  
319 0 ▶sub publish {
Subroutine publish: not called CC=2 SCAR=17.9
Pod documented
320 0 my $self = shift;
321 0 my %args = @_;
322 0 ▶ die "no exchange" unless exists $args{exchange};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"exchange"} 0 0 0
323  
324 0 ▶ $self->future->then(sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
325 0 my $f = $self->loop->new_future->set_label("publish on [" . $args{exchange} . "]");
326 0 my $dtag = $self->next_dtag;
327 0 ▶ if($self->{confirm_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'confirm_mode'}) { } 0 0 0
328 0 push @{$self->{published}}, [ $dtag => $f ];
329 } else {
330 0 $f->done;
331 }
332  
333 my @frames = $self->amqp->split_payload(
334 $args{payload},
335 exchange => Net::AMQP::Value::String->new($args{exchange}),
336 mandatory => $args{mandatory} // 0,
337 immediate => $args{immediate} // 0,
338 (exists $args{routing_key} ? (routing_key => Net::AMQP::Value::String->new($args{routing_key})) : ()),
339 ticket => 0,
340 content_type => $args{content_type} // 'application/binary',
341 content_encoding => $args{content_encoding},
342 timestamp => $args{timestamp} // time,
343 type => Net::AMQP::Value::String->new($args{type} // ''),
344 user_id => $self->amqp->user,
345 headers => $args{headers} || { },
346 delivery_mode => $args{delivery_mode} // 1,
347 priority => $args{priority} // 1,
348 correlation_id => $args{correlation_id},
349 expiration => (
350 exists $args{expiration}
351 # This would seem to make more sense as a numeric value, but the spec
352 # defines this as a shortstr
353 ? Net::AMQP::Value::String->new($args{expiration})
354 : undef
355 ),
356 message_id => $args{message_id},
357 app_id => $args{app_id},
358 cluster_id => $args{cluster_id},
359 reply_to => $args{reply_to},
360 0 ▶ weight => $args{weight} // 0,
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'routing_key'} ? : 0 0 0
exists $args{'expiration'} ? : 0 0 0
Condition0%0/18
$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
00
$args{"type"} // ""
l!l
00
$args{'headers'} || {}
l!l
00
$args{"delivery_mode"} // 1
l!l
00
$args{"priority"} // 1
l!l
00
$args{"weight"} // 0
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/1
$args{"timestamp"} // time
$args{"timestamp"}
MC/DC0%0/1
$args{"type"} // ""
$args{"type"}
MC/DC0%0/1
$args{'headers'} || {}
$args{'headers'}
MC/DC0%0/1
$args{"delivery_mode"} // 1
$args{"delivery_mode"}
MC/DC0%0/1
$args{"priority"} // 1
$args{"priority"}
MC/DC0%0/1
$args{"weight"} // 0
$args{"weight"}
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/2
A
A= $args{"timestamp"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"type"}
Aresult
11
00
Truth table0%0/2
A
A= $args{'headers'}
Aresult
11
00
Truth table0%0/2
A
A= $args{"delivery_mode"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"priority"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"weight"}
Aresult
11
00
361 );
362 0 $self->closure_protection($f);
363 $self->send_frame(
364 $_,
365 0 ) for @frames;
366 0 $f
367 })
368 0 }
369  
370 =head2 qos
371  
372 Changes QOS settings on the channel. Probably most
373 useful for limiting the number of messages that can
374 be delivered to us before we have to ACK/NAK to
375 proceed.
376  
377 Returns a L<Future> which will resolve with the
378 channel instance once the operation is complete.
379  
380 $ch->qos(
381 prefetch_count => 5,
382 prefetch_size => 1048576,
383 ) ==> $ch
384  
385 =cut
386  
387 0 ▶sub qos {
Subroutine qos: not called CC=1 SCAR=6.9
Pod documented
388 0 my $self = shift;
389 0 my %args = @_;
390  
391 0 ▶ $self->future->then(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
392 0 ▶ my $f = $self->loop->new_future->set_label("set qos count " . ($args{prefetch_count} // 0) . " size " . ($args{prefetch_size} // 0));
Condition0%0/4
$args{"prefetch_count"} // 0
l!l
00
$args{"prefetch_size"} // 0
l!l
00
MC/DC0%0/1
$args{"prefetch_count"} // 0
$args{"prefetch_count"}
MC/DC0%0/1
$args{"prefetch_size"} // 0
$args{"prefetch_size"}
Truth table0%0/2
A
A= $args{"prefetch_count"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"prefetch_size"}
Aresult
11
00
393 0 my $channel = $self->id;
394 0 $self->push_pending(
395 'Basic::QosOk' => [ $f, $self ],
396 );
397  
398 my $frame = Net::AMQP::Frame::Method->new(
399 method_frame => Net::AMQP::Protocol::Basic::Qos->new(
400 nowait => 0,
401 prefetch_count => $args{prefetch_count},
402 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
403 )
404 );
405 0 $self->closure_protection($f);
406 0 $self->send_frame($frame);
407 0 $f
408 0 });
409 }
410  
411 =head2 ack
412  
413 Acknowledge a specific delivery.
414  
415 Returns a L<Future> which will resolve with the
416 channel instance once the operation is complete.
417  
418 $ch->ack(
419 delivery_tag => 123,
420 ) ==> $ch
421  
422 =cut
423  
424 0 ▶sub ack {
Subroutine ack: not called CC=1 SCAR=6.9
Pod documented
425 0 my $self = shift;
426 0 my %args = @_;
427  
428 0 my $id = $self->id;
429 0 ▶ $self->future->on_done(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
430 0 my $channel = $id;
431 my $frame = Net::AMQP::Frame::Method->new(
432 method_frame => Net::AMQP::Protocol::Basic::Ack->new(
433 # nowait => 0,
434 delivery_tag => $args{delivery_tag},
435 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
436 )
437 );
438 0 $self->send_frame($frame);
439 0 });
440 }
441  
442 =head2 nack
443  
444 Negative acknowledgement for a specific delivery.
445  
446 Returns a L<Future> which will resolve with the
447 channel instance once the operation is complete.
448  
449 $ch->nack(
450 delivery_tag => 123,
451 ) ==> $ch
452  
453 =cut
454  
455 0 ▶sub nack {
Subroutine nack: not called CC=1 SCAR=6.9
Pod documented
456 0 my $self = shift;
457 0 my %args = @_;
458  
459 0 my $id = $self->id;
460 0 ▶ $self->future->on_done(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
461 0 my $channel = $id;
462 my $frame = Net::AMQP::Frame::Method->new(
463 method_frame => Net::AMQP::Protocol::Basic::Nack->new(
464 # nowait => 0,
465 delivery_tag => $args{delivery_tag},
466 multiple => $args{multiple} // 0,
467 0 ▶ requeue => $args{requeue} // 0,
Condition0%0/4
$args{"multiple"} // 0
l!l
00
$args{"requeue"} // 0
l!l
00
MC/DC0%0/1
$args{"multiple"} // 0
$args{"multiple"}
MC/DC0%0/1
$args{"requeue"} // 0
$args{"requeue"}
Truth table0%0/2
A
A= $args{"multiple"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"requeue"}
Aresult
11
00
468 )
469 );
470 0 $self->send_frame($frame);
471 0 });
472 }
473  
474 =head2 reject
475  
476 Reject a specific delivery.
477  
478 Returns a L<Future> which will resolve with the
479 channel instance once the operation is complete.
480  
481 $ch->nack(
482 delivery_tag => 123,
483 ) ==> $ch
484  
485 =cut
486  
487 0 ▶sub reject {
Subroutine reject: not called CC=1 SCAR=6.9
Pod documented
488 0 my ($self, %args) = @_;
489  
490 0 my $id = $self->id;
491 0 ▶ $self->future->on_done(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
492 0 my $channel = $id;
493 my $frame = Net::AMQP::Frame::Method->new(
494 method_frame => Net::AMQP::Protocol::Basic::Reject->new(
495 # nowait => 0,
496 delivery_tag => $args{delivery_tag},
497 multiple => $args{multiple} // 0,
498 0 ▶ requeue => $args{requeue} // 0,
Condition0%0/4
$args{"multiple"} // 0
l!l
00
$args{"requeue"} // 0
l!l
00
MC/DC0%0/1
$args{"multiple"} // 0
$args{"multiple"}
MC/DC0%0/1
$args{"requeue"} // 0
$args{"requeue"}
Truth table0%0/2
A
A= $args{"multiple"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"requeue"}
Aresult
11
00
499 )
500 );
501 0 $self->send_frame($frame);
502 0 });
503 }
504  
505 =pod
506  
507 Example output:
508  
509 'method_id' => 40,
510 'reply_code' => 404,
511 'class_id' => 60,
512 'reply_text' => 'NOT_FOUND - no exchange \'invalidchan\' in vhost \'vhost\''
513  
514 =cut
515  
516 =head2 on_close
517  
518 Called when the channel has been closed.
519  
520 =cut
521  
522 0 ▶sub on_close {
Subroutine on_close: not called CC=2 SCAR=17.9
Pod documented
523 0 my ($self, $frame) = @_;
524  
525 0 $self->{is_closed} = 1;
526 0 $self->{future} = Future->fail('closed');
527  
528 # ACK the close first - we have to send a close-ok
529 # before it's legal to reopen this channel ID
530 retain_future(
531 (
532 # If we initiated the close, then the CloseOk comes from the server
533 $self->{closing}
534 ? Future->done
535 : $self->send_frame(
536 Net::AMQP::Frame::Method->new(
537 method_frame => Net::AMQP::Protocol::Channel::CloseOk->new(
538 )
539 )
540 )
541 0 ▶ )->on_ready(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
542 # Any remaining consumers need to be cancelled at this point
543 $self->bus->invoke_event(
544 'cancel',
545 ctag => $_,
546 0 ) for keys %{$self->{consumer_tags}};
547 0 $self->{consumer_tags} = {};
548  
549 0 $_->fail('channel closed') for grep !$_->is_ready, map $_->[1], @{$self->{published}};
550 0 $self->{published} = [];
551  
552 # It's important that the MQ instance knows
553 # about the channel closure first before we
554 # go ahead and dispatch events, since any
555 # subscribed handlers might go ahead and
556 # attempt to open the channel again immediately.
557  
558 0 $self->amqp->channel_closed($self->id);
559 0 $self->bus->invoke_event(
560 'close',
561 code => $frame->reply_code,
562 reason => $frame->reply_text,
563 );
564 0 Future->done
565 })
566 0 ▶ )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'closing'} ? : 0 0 0
567 }
568  
569 =head2 send_frame
570  
571 Proxy frame sending requests to the parent
572 L<Net::Async::AMQP> instance.
573  
574 =cut
575  
576 0 ▶sub send_frame {
Subroutine send_frame: not called CC=1 SCAR=6.9
Pod documented
577 0 my $self = shift;
578 0 $self->amqp->send_frame(
579 @_,
580 channel => $self->id,
581 )
582 }
583  
584 =head2 close
585  
586 Ask the server to close this channel.
587  
588 Returns a L<Future> which will resolve with the
589 channel instance once the operation is complete.
590  
591 $ch->close(
592 code => 404,
593 text => 'something went wrong',
594 ) ==> $ch
595  
596 =cut
597  
598 0 ▶sub close {
Subroutine close: not called CC=5 SCAR=34.0
Pod documented
599 0 my $self = shift;
600 0 my %args = @_;
601 0 $self->debug_printf("Close channel %d", $self->id);
602  
603 # There's a slight chance we'll get called after being
604 # removed from the loop, since we wanted to close anyway then
605 # don't treat that as an error
606 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
607  
608 0 $self->{closing} = 1;
609 0 $self->{future} = Future->fail('closing');
610  
611 0 my $f = $self->loop->new_future->set_label("Close channel " . $self->id);
612 my $frame = Net::AMQP::Frame::Method->new(
613 method_frame => Net::AMQP::Protocol::Channel::Close->new(
614 reply_code => $args{code} // 404,
615 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
616 )
617 );
618 0 $self->push_pending(
619 'Channel::CloseOk' => [ $f, $self ],
620 );
621 0 $self->closure_protection($f);
622 0 $self->send_frame($frame);
623 0 return $f;
624 }
625  
626 =head2 push_pending
627  
628 =cut
629  
630 1 ▶sub push_pending {
Subroutine push_pending: called CC=2 SCAR=6.9
Pod documented
631 1 my $self = shift;
632 1 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
633 1 my ($type, $code) = splice @_, 0, 2;
634 1 push @{$self->{pending}{$type}}, $code;
635 }
636 1 return $self;
637 }
638  
639 =head2 remove_pending
640  
641 Removes a coderef from the pending event handler.
642  
643 Returns C< $self >.
644  
645 =cut
646  
647 0 ▶sub remove_pending {
Subroutine remove_pending: not called CC=3 SCAR=24.8
Pod documented
648 0 my $self = shift;
649 0 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
650 0 my ($type, $code) = splice @_, 0, 2;
651 # This is the same as extract_by { $_ eq $code } @{$self->{pending}{$type}};,
652 # but since we'll be calling it a lot might as well do it inline:
653 splice
654 0 @{$self->{pending}{$type}},
655 $_,
656 0 1 for grep {
657 0 $self->{pending}{$type}[$_] eq $code
658 0 } reverse 0..$#{$self->{pending}{$type}};
659 }
660 0 return $self;
661 }
662  
663 =head2 next_pending
664  
665 Retrieves the next pending handler for the given incoming frame type (see L<Net::Async::AMQP::Utils/amqp_frame_type>),
666 and calls it.
667  
668 Takes the following parameters:
669  
670 =over 4
671  
672 =item * $frame - the frame itself
673  
674 =back
675  
676 Returns $self.
677  
678 =cut
679  
680 1 ▶sub next_pending {
Subroutine next_pending: called CC=33 SCAR=63.8
Pod documented
681 1 my ($self, $frame) = @_;
682  
683 # First part of a frame. There's more to come, so stash a new future
684 # and return.
685 1 ▶ if($frame->isa('Net::AMQP::Frame::Header')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->isa("Net::AMQP::Frame::Header")) 0 1 1
686 # Properties are available directly from the header frame
687 0 my $hdr_frame = $frame->header_frame;
688 0 $self->{incoming_message}{type} = $hdr_frame->type;
689 $self->{incoming_message}{properties} = {
690 0 map {; $_ => scalar($hdr_frame->$_) } qw(
691 content_type
692 content_encoding
693 delivery_mode
694 priority
695 correlation_id
696 reply_to
697 expiration
698 message_id
699 timestamp
700 user_id
701 app_id
702 )
703 };
704 0 ▶ if($frame->header_frame->headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->header_frame->headers) { } 0 0 0
705 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"incoming_message"}{"headers"} = {%{$frame->header_frame->headers;}}; 1 } } or $self->debug_printf("Unexpected exception while doing something: %s", $@) 0 0 0
706 #$self->{incoming_message}{type} = $frame->header_frame->headers->{type}
707 # if exists $frame->header_frame->headers->{type};
708 # Shallow copy for local storage
709 0 $self->{incoming_message}{headers} = { %{$frame->header_frame->headers} };
710 0 1
711 } or $self->debug_printf("Unexpected exception while doing something: %s", $@);
712 } else {
713 0 $self->{incoming_message}{headers} = {};
714 }
715  
716 0 ▶ if($frame->body_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->body_size) { } 0 0 0
717 # Stash the size so we can do some basic validation on the payload frames
718 0 $self->{incoming_message}{pending} = $frame->body_size;
719 } else {
720 # Messages may be empty - in this case we'd have no body frames at all, we're done already:
721 0 $self->deliver_current_message;
722 }
723  
724 0 return $self;
725 }
726  
727 # Body part of an incoming message.
728 1 ▶ if($frame->isa('Net::AMQP::Frame::Body')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->isa("Net::AMQP::Frame::Body")) 0 1 1
729 0 my $bytes = $frame->payload;
730 0 $self->{incoming_message}{payload} .= $bytes;
731 0 $self->{incoming_message}{pending} -= length $bytes;
732 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
733 # We still have more to come, just return for now
734 0 return $self;
735 } elsif($self->{incoming_message}{pending} < 0) {
736 $self->close(
737 code => 500,
738 0 text => -$self->{incoming_message}{pending} . ' excess payload bytes detected in delivery'
739 );
740 0 delete $self->{incoming_message};
741 0 return $self;
742 } else {
743 # We have a full message now - hand it over to the event bus
744 0 $self->deliver_current_message;
745 0 return $self;
746 }
747 }
748  
749 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
750 1 my $type = amqp_frame_type($frame);
751 1 ▶ if($type eq 'Basic::ConsumeOk') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'Basic::ConsumeOk') { } 0 1 1
elsif ($type eq 'Basic::Cancel' or $type eq 'Basic::CancelOk') { } 0 1 1
752 0 my $ctag = $method_frame->consumer_tag;
753 0 $self->{consumer_tags}{$ctag} = 1;
754 ▶ } elsif($type eq 'Basic::Cancel' or $type eq 'Basic::CancelOk') {
Condition33%1/3
$type eq "Basic::Cancel" or $type eq "Basic::CancelOk"
l!l&&r!l&&!r
001
MC/DC0%0/2
$type eq "Basic::Cancel" or $type eq "Basic::CancelOk"
$type eq "Basic::Cancel" $type eq "Basic::CancelOk"
Truth table33%1/3
A or B
A= $type eq "Basic::Cancel"B= $type eq "Basic::CancelOk"
ABresult
1X1
011
000
755 0 my ($ctag) = ($method_frame->consumer_tag);
756 0 $self->debug_printf("Cancel $ctag");
757 0 $self->bus->invoke_event(
758 'cancel',
759 ctag => $ctag,
760 );
761 # Also raise this as a "listener_stop"
762 # event, for managed channels
763 0 $self->bus->invoke_event(
764 listener_stop => $ctag
765 );
766 0 delete $self->{consumer_tags}{$ctag};
767 }
768  
769 # Message delivery, part 3: The "Deliver" message.
770 # This is actually where we start.
771 1 ▶ if($type eq 'Basic::Deliver') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Basic::Deliver") 0 1 1
772 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
773 $self->{incoming_message} = {
774 0 ctag => $method_frame->consumer_tag,
775 dtag => $method_frame->delivery_tag,
776 rkey => $method_frame->routing_key,
777 payload => '',
778 payload_size => undef,
779 };
780 0 return $self;
781 }
782  
783 1 ▶ if($type eq 'Channel::Close') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Channel::Close") 0 1 1
784 0 $self->debug_printf(
785 "Channel was %d, calling close - code %d, text '%s', class:method %d:%d",
786 $frame->channel,
787 $method_frame->reply_code,
788 $method_frame->reply_text,
789 $method_frame->class_id,
790 $method_frame->method_id,
791 );
792 0 $self->on_close(
793 $method_frame
794 );
795 0 return $self;
796 }
797  
798 # Confirm mode => mark pending task as done
799 1 ▶ if($type eq 'Confirm::SelectOk') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'Confirm::SelectOk') { } 0 1 1
elsif ($type eq 'Basic::Ack') { } 0 1 1
elsif ($type eq 'Basic::Nack') { } 0 1 1
elsif ($type eq 'Basic::Return') { } 0 1 1
800 0 $self->debug_printf("Confirm mode enabled");
801 0 $self->{confirm_mode}->done;
802 0 return $self;
803 } elsif($type eq 'Basic::Ack') {
804 0 ▶ shift @{$self->{pending}{'Basic::Return'} || []};
Condition0%0/2
$self->{'pending'}{'Basic::Return'} || []
l!l
00
MC/DC0%0/1
$self->{'pending'}{'Basic::Return'} || []
$self->{'pending'}{'Basic::Return'}
Truth table0%0/2
A
A= $self->{'pending'}{'Basic::Return'}
Aresult
11
00
805 eval {
806 0 my @msg = $self->extract_published($method_frame->delivery_tag, $method_frame->multiple);
807 0 $self->debug_printf("received ack for %d messages", 0 + @msg);
808 0 $_->done for grep !$_->is_ready, map $_->[1], @msg;
809 0 1
810 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my(@msg) = $self->extract_published($method_frame->delivery_tag, $method_frame->multiple); $self->debug_printf("received ack for %d messages", 0 + @msg); $_->done foreach (grep((!$_->is_ready), map($_->[1], @msg))); 1 } } or do { my $err = $@; $self->debug_printf("error retrieving messages for ack - %s", $err); $self->close("code", 406, "text", $err) } 0 0 0
811 0 my $err = $@;
812 0 $self->debug_printf("error retrieving messages for ack - %s", $err);
813 0 $self->close(
814 code => 406,
815 text => $err
816 );
817 };
818 0 return $self;
819 } elsif($type eq 'Basic::Nack') {
820 0 ▶ shift @{$self->{pending}{'Basic::Return'} || []};
Condition0%0/2
$self->{'pending'}{'Basic::Return'} || []
l!l
00
MC/DC0%0/1
$self->{'pending'}{'Basic::Return'} || []
$self->{'pending'}{'Basic::Return'}
Truth table0%0/2
A
A= $self->{'pending'}{'Basic::Return'}
Aresult
11
00
821 eval {
822 0 my @msg = $self->extract_published($method_frame->delivery_tag, $method_frame->multiple);
823 0 $self->debug_printf("received nack for %d messages", 0 + @msg);
824 0 $_->fail('nack') for grep !$_->is_ready, map $_->[1], @msg;
825 0 1
826 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my(@msg) = $self->extract_published($method_frame->delivery_tag, $method_frame->multiple); $self->debug_printf("received nack for %d messages", 0 + @msg); $_->fail("nack") foreach (grep((!$_->is_ready), map($_->[1], @msg))); 1 } } or do { my $err = $@; $self->debug_printf("error retrieving messages for nack - %s", $err); $self->close("code", 406, "text", $err) } 0 0 0
827 0 my $err = $@;
828 0 $self->debug_printf("error retrieving messages for nack - %s", $err);
829 0 $self->close(
830 code => 406,
831 text => $err
832 );
833 };
834 0 return $self;
835 } elsif($type eq 'Basic::Return') {
836 # Basic::Return would always be for the first unacked message in our publish
837 # queue... except when we don't have publisher confirms, in which case... uh...
838 # okay in that case we'd just raise an event maybe
839 0 ▶ if($self->{confirm_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'confirm_mode'}) { } 0 0 0
840 0 $self->debug_printf("basic::return in confirm mode");
841 0 my $f = $self->{published}[0][1];
842 0 ▶ $f->fail(
Branch: 1 branch
BranchTrueFalseTotal
if $f and not $f->is_ready 0 0 0
Condition0%0/3
$f and not $f->is_ready
!ll&&!rl&&r
000
MC/DC0%0/2
$f and not $f->is_ready
$f not $f->is_ready
Truth table0%0/3
A and not(B)
A= $fB= not $f->is_ready
ABresult
0X0
100
111
843 $method_frame->reply_text,
844 code => $method_frame->reply_code,
845 exchange => $method_frame->exchange,
846 rkey => $method_frame->routing_key
847 ) if $f && !$f->is_ready;
848 } else {
849 0 $self->debug_printf("basic::return in normal mode");
850 0 $self->bus->invoke_event(
851 return => $method_frame->reply_text,
852 code => $method_frame->reply_code,
853 exchange => $method_frame->exchange,
854 rkey => $method_frame->routing_key
855 )
856 }
857 0 return $self;
858 }
859  
860 1 ▶ if(my $next = shift @{$self->{pending}{$type} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = shift @{$self->{"pending"}{$type} or [];}) 1 0 1
Condition50%1/2
$self->{'pending'}{$type} || []
l!l
10
MC/DC0%0/1
$self->{'pending'}{$type} || []
$self->{'pending'}{$type}
Truth table50%1/2
A
A= $self->{'pending'}{$type}
Aresult
11
00
861 # We have a registered handler for this frame type. This usually
862 # means that we've sent a message and are awaiting a response.
863 1 ▶ if(ref($next) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next eq 'ARRAY') { } 0 1 1
864 0 my ($f, @args) = @$next;
865 0 ▶ $f->done(@args) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
866 } else {
867 1 $next->($self, $frame, @_);
868 }
869 1 return $self;
870 }
871  
872 # It's quite possible we'll see unsolicited frames back from
873 # the server. We don't expect many so report them when in debug mode.
874 0 $self->debug_printf("We had no pending handlers for [%s]", $type);
875 0 return $self;
876 }
877  
878 0 ▶sub deliver_current_message {
Subroutine deliver_current_message: not called CC=1 SCAR=6.9
Pod undocumented
879 0 my $self = shift;
880 $self->bus->invoke_event(
881 0 message => @{$self->{incoming_message}}{qw(type payload ctag dtag rkey headers properties)},
882 );
883 0 delete $self->{incoming_message};
884 }
885  
886 0 ▶sub extract_published {
Subroutine extract_published: not called CC=8 SCAR=42.8
Pod undocumented
887 0 my ($self, $dtag, $multiple) = @_;
888 0 ▶ if($multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiple) 0 0 0
889 0 my @msg;
890 0 ▶ while(@{$self->{published}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"published"};}) 0 0 0
891 0 my $msg = shift @{$self->{published}};
892 # Nonzero dtag means "up to and including this message"
893 0 ▶ die 'ack for dtag ' . $dtag . ' but our earliest message is ' . $msg->[0] if $dtag && $dtag < $msg->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $dtag and $dtag < $msg->[0] 0 0 0
Condition0%0/3
$dtag and $dtag < $msg->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$dtag and $dtag < $msg->[0]
$dtag $dtag < $msg->[0]
Truth table0%0/3
A and B
A= $dtagB= $dtag < $msg->[0]
ABresult
0X0
100
111
894  
895 # with dtag=0 that's "everything you've got". this is fundamentally
896 # flawed since the server may not have even received the most recent
897 # items... might work if the server *always* uses dtag=0, I guess
898 0 push @msg, $msg;
899 0 ▶ last if $dtag == $msg->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $dtag == $msg->[0] 0 0 0
900 }
901 0 return @msg;
902 }
903 # Single-ack *probably* handles things in order, but the spec does not seem
904 # to mandate this - better to be safe
905 0 for my $idx (0..$#{$self->{published}}) {
906 0 ▶ return splice @{$self->{published}}, $idx, 1 if $self->{published}[$idx][0] == $dtag;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"published"}[$idx][0] == $dtag 0 0 0
907 }
908 0 die 'ack for dtag ' . $dtag . ' but not found in our pending list (we had ' . @{$self->{published}} . ' pending messages)';
909 }
910  
911 =head1 METHODS - Accessors
912  
913 =cut
914  
915 =head2 amqp
916  
917 The parent L<Net::Async::AMQP> instance.
918  
919 =cut
920  
921 0 ▶sub amqp { shift->{amqp} }
Subroutine amqp: not called CC=1 SCAR=6.9
Pod documented
922  
923 =head2 bus
924  
925 Event bus. Used for sharing channel-specific events.
926  
927 =cut
928  
929 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
930  
931 =head2 future
932  
933 The underlying L<Future> for this channel.
934  
935 Will resolve to the L<Net::Async::Channel> instance once the channel is open.
936  
937 =cut
938  
939 1 ▶sub future { shift->{future} }
Subroutine future: called CC=1 SCAR=0.0
Pod documented
940  
941 =head2 id
942  
943 This channel ID.
944  
945 =cut
946  
947 284 ▶sub id {
Subroutine id: called CC=2 SCAR=9.2
Pod documented
948 284 my $self = shift;
949 284 ▶ return $self->{id} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 284 0 284
950 0 $self->{id} = shift;
951 0 $self
952 }
953  
954 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod undocumented
955 0 my $self = shift;
956 0 sprintf "Channel[%d]", $self->id;
957 }
958  
959 =head2 closed
960  
961 Returns true if the channel has been closed, 1 if not (which could mean it is either not yet open,
962 or that it is open and has not yet been closed by either side).
963  
964 =cut
965  
966 0 ▶sub is_closed { shift->{is_closed} }
Subroutine is_closed: not called CC=1 SCAR=6.9
Pod undocumented
967  
968 =head2 closure_protection
969  
970 Helper method for marking any outstanding requests as failed when the channel closes.
971  
972 Takes a L<Future>, returns a L<Future> (probably the same one).
973  
974 =cut
975  
976 0 ▶sub closure_protection {
Subroutine closure_protection: not called CC=3 SCAR=24.8
Pod documented
977 0 my ($self, $f) = @_;
978 0 ▶ unless($f) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f) 0 0 0
979 0 $self->debug_printf("Closure protection requested on channel %d for future which has already disappeared", $self->id);
980 0 return Future->fail(closed => 'future has already been released');
981 }
982  
983 # No sense in proceeding if the Future has already completed
984 0 ▶ if($f->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_ready) 0 0 0
985 0 $self->debug_printf("Closure protection requested for future %s on channel %d which has already compelted", $f->label, $self->id);
986 0 return $f;
987 }
988  
989 0 my $id = $self->id;
990 0 my @ev;
991 0 my $bus = $self->bus;
992 $bus->subscribe_to_event(
993 0 ▶ @ev = (close => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
994 0 my ($ev, %args) = @_;
995 0 ▶ $self->debug_printf("Closure protection engaging for %s on channel %d, code %s, reason: %s", ($f ? $f->label : "(future which no longer exists)"), $id, $args{code} // '(none)', $args{reason});
Branch: 1 branch
BranchTrueFalseTotal
$f ? : 0 0 0
Condition0%0/2
$args{"code"} // "(none)"
l!l
00
MC/DC0%0/1
$args{"code"} // "(none)"
$args{"code"}
Truth table0%0/2
A
A= $args{"code"}
Aresult
11
00
996 0 ▶ if($f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f) { } 0 0 0
997 0 ▶ $f->fail($args{reason}, 'amqp', $args{code}) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
998 } else {
999 0 $self->debug_printf("Future has disappeared already, not marking as failed");
1000 }
1001 # We should have unsubscribed already, but do this just in case.
1002 0 splice @ev;
1003 0 eval { $ev->unsubscribe; };
1004 })
1005 0 );
1006  
1007 # Use return value from ->on_ready, since we may clear $f immediately if the future is already
1008 # marked as ready.
1009 0 ▶ $f->on_ready(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1010 0 ▶ $self->debug_printf("Future %s on channel %d is ready, disengaging closure protection", ($f ? $f->label : "(future which no longer exists)"), $id);
Branch: 1 branch
BranchTrueFalseTotal
$f ? : 0 0 0
1011 0 eval { $bus->unsubscribe_from_event(splice @ev); };
1012 0 undef $f;
1013 0 });
1014 }
1015  
1016 19 1;
1017  
1018 __END__
↑ Summary AMQP/ConnectionManager.pm »