AMQP.pm

filter: Statementstmt 73.9% 269 / 364 Branchbran 50.0% 53 / 106 Conditioncond 51.5% 34 / 66 MC/DCmcdc 13.8% 5 / 36 Subroutinesub 79.7% 63 / 79 Podpod 93.3% 42 / 45 totaltotal 66.9% 466 / 696 CC 88 SCAR 18.6
CC
88
Cov
66%
on_closed
56.0
__ANON__
12.0
remove_pending
12.0
CRAP 6.4
« AMQP/ConnectionManager/Channel.pm ↑ Summary AMQP/RPC/Base.pm »
1 package Net::Async::AMQP;
2 # ABSTRACT: IO::Async support for the AMQP protocol
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 19 our $VERSION = '2.000';
9  
10 =head1 NAME
11  
12 Net::Async::AMQP - provides client interface to AMQP using L<IO::Async>
13  
14 =head1 VERSION
15  
16 version 2.000
17  
18 =head1 SYNOPSIS
19  
20 use IO::Async::Loop;
21 use Net::Async::AMQP;
22 my $loop = IO::Async::Loop->new;
23 $loop->add(my $amqp = Net::Async::AMQP->new);
24 $amqp->connect(
25 host => 'localhost',
26 user => 'guest',
27 pass => 'guest',
28 )->get;
29  
30 =head1 DESCRIPTION
31  
32 Does AMQP things. Note that the API may change before the stable 1.000
33 release - L</ALTERNATIVE AMQP IMPLEMENTATIONS> are listed below if you want
34 to evaluate other options.
35  
36 If you want a higher-level API which manages channels and connections, try
37 L<Net::Async::AMQP::ConnectionManager>.
38  
39 Examples are in the C<examples/> directory.
40  
41 =head2 AMQP support
42  
43 The following AMQP features are supported:
44  
45 =over 4
46  
47 =item * Queue declare, bind, delete
48  
49 =item * Exchange declare, delete
50  
51 =item * Consumer setup and cancellation
52  
53 =item * Message publishing
54  
55 =item * Explicit ACK
56  
57 =item * QoS
58  
59 =item * SSL
60  
61 =back
62  
63 =head2 RabbitMQ-specific features
64  
65 RabbitMQ provides some additional features:
66  
67 =over 4
68  
69 =item * Exchange-to-exchange binding
70  
71 =item * Server flow control notification
72  
73 =item * Consumer cancellation notification
74  
75 =item * Reject
76  
77 =item * TTL for message expiry
78  
79 =item * 255-level priorities
80  
81 =back
82  
83 =head2 Missing features
84  
85 The following features aren't currently implemented - raise a request via RT or by email (L</AUTHOR>)
86 if you want any of these:
87  
88 =over 4
89  
90 =item * Transactions
91  
92 =item * Flow control
93  
94 =item * SASL auth
95  
96 =back
97  
98 This implementation is designed to handle many simultaneous channels and connections. If you just want a
99 single consumer/publisher, one of the librabbitmq-c implementations may be sufficient.
100  
101 =cut
102  
103 19 ▶use Net::AMQP;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 19 ▶use Net::AMQP::Common qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 19 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 19 ▶use curry::weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 19 ▶use Class::ISA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
109 19 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
110 19 ▶use List::UtilsBy qw(extract_by);
Subroutine BEGIN: called CC=1 SCAR=0.0
111 19 ▶use File::ShareDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
112 19 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
113 19 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
114 19 ▶use Mixin::Event::Dispatch::Bus;
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 =head1 CONSTANTS
117  
118 =head2 AUTH_MECH
119  
120 Defines the mechanism used for authentication. Currently only AMQPLAIN
121 is supported.
122  
123 =cut
124  
125 19 ▶use constant AUTH_MECH => 'AMQPLAIN';
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 =head2 PAYLOAD_HEADER_LENGTH
128  
129 Length of header used in payload messages. Defined by the AMQP standard
130 as 8 bytes.
131  
132 =cut
133  
134 19 ▶use constant PAYLOAD_HEADER_LENGTH => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
135  
136 =head2 MAX_FRAME_SIZE
137  
138 Largest amount of data we'll attempt to send in a single frame. Actual
139 frame limit will be negotiated with the remote server. Defaults to 262144.
140  
141 =cut
142  
143 19 ▶use constant MAX_FRAME_SIZE => 262144;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 =head2 MAX_CHANNELS
146  
147 Maximum number of channels to request. Defaults to the AMQP limit (65535).
148 Attempting to set this any higher will not end well, it's an unsigned 16-bit
149 value.
150  
151 =cut
152  
153 19 ▶use constant MAX_CHANNELS => 65535;
Subroutine BEGIN: called CC=1 SCAR=0.0
154  
155 =head2 HEARTBEAT_INTERVAL
156  
157 Interval in seconds between heartbeat frames, zero to disable. Can be
158 overridden by C<PERL_AMQP_HEARTBEAT_INTERVAL> in the environment, default
159 is 0 (disabled).
160  
161 =cut
162  
163 19 ▶use constant HEARTBEAT_INTERVAL => $ENV{PERL_AMQP_HEARTBEAT_INTERVAL} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"PERL_AMQP_HEARTBEAT_INTERVAL"} // 0
l!l
019
MC/DC0%0/1
$ENV{"PERL_AMQP_HEARTBEAT_INTERVAL"} // 0
$ENV{"PERL_AMQP_HEARTBEAT_INTERVAL"}
Truth table50%1/2
A
A= $ENV{"PERL_AMQP_HEARTBEAT_INTERVAL"}
Aresult
11
00
164  
165 19 ▶use Net::Async::AMQP::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 19 ▶use Net::Async::AMQP::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 19 ▶use Net::Async::AMQP::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 =head1 PACKAGE VARIABLES
170  
171 =head2 $XML_SPEC
172  
173 This defines the path to the AMQP XML spec, which L<Net::AMQP> uses
174 to create methods and handlers for the appropriate version of the MQ
175 protocol.
176  
177 Defaults to an extended version of the 0.9.1 protocol as used by RabbitMQ,
178 this is found in the C<amqp0-9-1.extended.xml> distribution sharedir (see
179 L<File::ShareDir>).
180  
181 Normally, you should be able to ignore this. If you want to load an alternative
182 spec, note that (a) this is global, rather than per-instance, (b) it needs to
183 be set before you C<use> this module.
184  
185 BEGIN { $Net::Async::AMQP::XML_SPEC = '/tmp/amqp.xml' }
186 use Net::Async::AMQP;
187  
188 Once loaded, this module will not attempt to apply the spec again.
189  
190 =cut
191  
192 19 our $XML_SPEC;
193 19 our $SPEC_LOADED;
194 19 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=13.1
195 19 ▶ $XML_SPEC //= File::ShareDir::dist_file(
Condition33%1/3
$Net::Async::AMQP::XML_SPEC //= File::ShareDir::dist_file("Net-Async-AMQP", "amqp0-9-1.extended.xml")
l!l&&r!l&&!r
0190
MC/DC0%0/2
$Net::Async::AMQP::XML_SPEC //= File::ShareDir::dist_file("Net-Async-AMQP", "amqp0-9-1.extended.xml")
$Net::Async::AMQP::XML_SPEC File::ShareDir::dist_file("Net-Async-AMQP", "amqp0-9-1.extended.xml")
Truth table33%1/3
A //= B
A= $Net::Async::AMQP::XML_SPECB= File::ShareDir::dist_file("Net-Async-AMQP", "amqp0-9-1.extended.xml")
ABresult
1X1
011
000
196 'Net-Async-AMQP',
197 'amqp0-9-1.extended.xml'
198 );
199  
200 # Load the appropriate protocol definitions. RabbitMQ uses a
201 # modified version of AMQP 0.9.1
202 19 ▶ Net::AMQP::Protocol->load_xml_spec($XML_SPEC) unless $SPEC_LOADED++;
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::Async::AMQP::SPEC_LOADED++ 19 0 19
203 }
204  
205 =head1 %CONNECTION_DEFAULTS
206  
207 The default parameters to use for L</connect>. Changing these values is permitted,
208 but do not attempt to delete from or add any entries to the hash.
209  
210 Passing parameters directly to L</connect> is much safer, please do that instead.
211  
212 =cut
213  
214 19 our %CONNECTION_DEFAULTS = (
215 port => 5672,
216 host => 'localhost',
217 user => 'guest',
218 pass => 'guest',
219 );
220  
221 =head1 METHODS
222  
223 =cut
224  
225 =head2 configure
226  
227 Set up variables. Takes the following optional named parameters:
228  
229 =over 4
230  
231 =item * heartbeat_interval - (optional) interval between heartbeat messages,
232 default is set by the L</HEARTBEAT_INTERVAL> constant
233  
234 =item * max_channels - how many channels to allow on this connection,
235 default is defined by the L</MAX_CHANNELS> constant
236  
237 =back
238  
239 Returns the new instance.
240  
241 =cut
242  
243 3 ▶sub configure {
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
244 3 my ($self, %args) = @_;
245 3 for (qw(heartbeat_interval max_channels)) {
246 6 ▶ $self->{$_} = delete $args{$_} if exists $args{$_}
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 1 5 6
247 }
248 3 $self->SUPER::configure(%args)
249 }
250  
251 =head2 bus
252  
253 Event bus. Used for sharing global events such as connection closure.
254  
255 =cut
256  
257 8 ▶sub bus { $_[0]->{bus} ||= Mixin::Event::Dispatch::Bus->new }
Subroutine bus: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
$_[0]{"bus"} ||= "Mixin::Event::Dispatch::Bus"->new
l!l&&r!l&&!r
710
MC/DC0%0/2
$_[0]{"bus"} ||= "Mixin::Event::Dispatch::Bus"->new
$_[0]{"bus"} "Mixin::Event::Dispatch::Bus"->new
Truth table66%2/3
A ||= B
A= $_[0]{"bus"}B= "Mixin::Event::Dispatch::Bus"->new
ABresult
1X1
011
000
258  
259 =head2 connect
260  
261 Takes the following parameters:
262  
263 =over 4
264  
265 =item * port - the AMQP port, defaults to 5672, can be a service name if preferred
266  
267 =item * host - host to connect to, defaults to localhost
268  
269 =item * local_host - our local IP to connect from
270  
271 =item * user - which user to connect as, defaults to guest
272  
273 =item * pass - the password for this user, defaults to guest
274  
275 =item * ssl - true if you want to connect over SSL
276  
277 =item * SSL_* - SSL-specific parameters, see L<IO::Async::SSL> and L<IO::Socket::SSL> for details
278  
279 =back
280  
281 Returns $self.
282  
283 =cut
284  
285 1 ▶sub connect {
Subroutine connect: called CC=8 SCAR=23.2
Pod documented
286 1 my $self = shift;
287 1 my %args = @_;
288  
289 1 ▶ die 'no loop' unless my $loop = $self->loop;
Branch: 1 branch
BranchTrueFalseTotal
unless my $loop = $self->loop 0 1 1
290  
291 1 my $f = $self->loop->new_future;
292  
293 # Apply defaults
294 1 ▶ $self->{$_} = $args{$_} //= $CONNECTION_DEFAULTS{$_} for keys %CONNECTION_DEFAULTS;
Condition33%1/3
$args{$_} //= $Net::Async::AMQP::CONNECTION_DEFAULTS{$_}
l!l&&r!l&&!r
400
MC/DC0%0/2
$args{$_} //= $Net::Async::AMQP::CONNECTION_DEFAULTS{$_}
$args{$_} $Net::Async::AMQP::CONNECTION_DEFAULTS{$_}
Truth table33%1/3
A //= B
A= $args{$_}B= $Net::Async::AMQP::CONNECTION_DEFAULTS{$_}
ABresult
1X1
011
000
295  
296 # Remember our event callbacks so we can unsubscribe
297 1 my $connected;
298 my $close;
299  
300 # Clean up once we succeed/fail
301 1 ▶ $f->on_ready(sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
302 1 ▶ $self->bus->unsubscribe_from_event(close => $close) if $close;
Branch: 1 branch
BranchTrueFalseTotal
if $close 1 0 1
303 1 ▶ $self->bus->unsubscribe_from_event(connected => $connected) if $connected;
Branch: 1 branch
BranchTrueFalseTotal
if $connected 1 0 1
304 1 undef $close;
305 1 undef $connected;
306 1 undef $self;
307 1 undef $f;
308 1 });
309  
310 # One-shot event on connection
311 1 ▶ $self->bus->subscribe_to_event(connected => $connected = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
312 1 ▶ $f->done($self) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 1 0 1
313 1 });
314 # Also pick up connection termination
315 0 ▶ $self->bus->subscribe_to_event(close => $close = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
316 0 ▶ $f->fail(connect => 'Remote closed connection') unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
317 1 });
318  
319 # Support SSL connection
320 1 ▶ require IO::Async::SSL if $args{ssl};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"ssl"} 0 1 1
321 1 ▶ my $method = $args{ssl} ? 'SSL_connect' : 'connect';
Branch: 1 branch
BranchTrueFalseTotal
$args{'ssl'} ? : 0 1 1
322 $loop->$method(
323 host => $self->{host},
324 # local_host can be used to send from a different source address,
325 # sometimes useful for routing purposes or loadtesting
326 (exists $args{local_host} ? (local_host => $args{local_host}) : ()),
327 service => $self->{port},
328 socktype => 'stream',
329  
330 on_stream => $self->curry::on_stream(\%args),
331  
332 on_resolve_error => $f->curry::fail('resolve'),
333 on_connect_error => $f->curry::fail('connect'),
334 ($args{ssl}
335 ? (on_ssl_error => $f->curry::fail('ssl'))
336 : ()
337 ),
338 0 ▶ (map {; $_ => $args{$_} } grep /^SSL/, keys %args)
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'local_host'} ? : 0 1 1
$args{'ssl'} ? : 0 1 1
339 );
340 1 $f;
341 }
342  
343 =head2 on_stream
344  
345 Called once the underlying TCP connection has been established.
346  
347 Returns nothing of importance.
348  
349 =cut
350  
351 1 ▶sub on_stream {
Subroutine on_stream: called CC=2 SCAR=7.0
Pod documented
352 1 my ($self, $args, $stream) = @_;
353 1 $self->debug_printf("Stream received");
354 1 $self->{stream} = $stream;
355 1 $stream->configure(
356 on_read => $self->curry::weak::on_read,
357 );
358 1 $self->add_child($stream);
359 1 ▶ $self->apply_heartbeat_timer if $self->heartbeat_interval;
Branch: 1 branch
BranchTrueFalseTotal
if $self->heartbeat_interval 0 1 1
360 1 $self->post_connect(%$args);
361 1 return;
362 }
363  
364 0 ▶sub dump_frame {
Subroutine dump_frame: not called CC=2 SCAR=17.9
Pod undocumented
365 0 my ($self, $pkt) = @_;
366 0 my ($type) = unpack 'C1', substr $pkt, 0, 1, '';
367 printf "Type: %02x (%s)\n", $type, {
368 1 => 'Method',
369 0 }->{$type};
370  
371 0 my ($chan) = unpack 'n1', substr $pkt, 0, 2, '';
372 0 printf "Channel: %d\n", $chan;
373  
374 0 my ($len) = unpack 'N1', substr $pkt, 0, 4, '';
375 0 printf "Length: %d bytes\n", $len;
376  
377 0 ▶ if($type == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) 0 0 0
378 0 my ($class, $method) = unpack 'n1n1', substr $pkt, 0, 4, '';
379 0 printf "Class: %s\n", $class;
380 0 printf "Method: %s\n", $method;
381 }
382 }
383  
384 =head2 on_read
385  
386 Called whenever there's data available to be read.
387  
388 =cut
389  
390 5 ▶sub on_read {
Subroutine on_read: called CC=3 SCAR=11.1
Pod documented
391 5 my ($self, $stream, $buffref, $eof) = @_;
392  
393 5 $self->last_frame_time(Time::HiRes::time);
394  
395 # As each frame is parsed it will be removed from the buffer
396 5 $self->process_frame($_) for Net::AMQP->parse_raw_frames($buffref);
397 5 ▶ $self->on_closed if $eof;
Branch: 1 branch
BranchTrueFalseTotal
if $eof 0 5 5
398 5 return 0;
399 }
400  
401 =head2 on_closed
402  
403 Called when the TCP connection is closed.
404  
405 =cut
406  
407 0 ▶sub on_closed {
Subroutine on_closed: not called CC=7 SCAR=40.3
Pod documented
408 0 my $self = shift;
409 0 ▶ my $reason = shift // 'unknown';
Condition0%0/2
shift() // "unknown"
l!l
00
MC/DC0%0/1
shift() // "unknown"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
410 0 $self->debug_printf("Connection closed [%s]", $reason);
411 0 delete $self->{connected};
412  
413 0 for my $ch (grep $_, values %{$self->{channel_by_id}}) {
414 0 $ch->bus->invoke_event(
415 'close',
416 code => undef,
417 reason => 'Connection closed: ' . $reason,
418 );
419 0 $self->channel_closed($ch->id);
420 }
421  
422 # Clean up any mismatching entries in the Future map
423 0 $_->cancel for grep !$_->is_ready, values %{$self->{channel_map}};
424 0 $self->{channel_map} = {};
425  
426 0 ▶ $self->stream->close if $self->stream;
Branch: 1 branch
BranchTrueFalseTotal
if $self->stream 0 0 0
427 0 for (qw(stream heartbeat_send_timer heartbeat_receive_timer)) {
428 0 $self->debug_printf("Remove child %s", $_);
429 0 ▶ (delete $self->{$_})->remove_from_parent if $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$_} 0 0 0
430 }
431 0 $self->bus->invoke_event(close => $reason)
432 }
433  
434 =head2 post_connect
435  
436 Sends initial startup header and applies listener for the C< Connection::Start > message.
437  
438 Returns $self.
439  
440 =cut
441  
442 1 ▶sub post_connect {
Subroutine post_connect: called CC=6 SCAR=19.1
Pod documented
443 1 my $self = shift;
444 1 my %args = @_;
445  
446 my %client_prop = (
447 platform => $args{platform} // 'Perl/NetAsyncAMQP',
448 product => $args{product} // __PACKAGE__,
449 information => $args{information} // 'http://search.cpan.org/perldoc?Net::Async::AMQP',
450 version => $args{version} // $VERSION,
451 1 ▶ ($args{client_properties} ? %{$args{client_properties}} : ()),
Branch: 1 branch
BranchTrueFalseTotal
$args{'client_properties'} ? : 1 0 1
Condition44%4/9
$args{"platform"} // "Perl/NetAsyncAMQP"
l!l
01
$args{"product"} // __PACKAGE__
l!l
01
$args{"information"} // "http://search.cpan.org/perldoc?Net::Async::AMQP"
l!l
01
$args{"version"} // $Net::Async::AMQP::VERSION
l!l&&r!l&&!r
010
MC/DC0%0/1
$args{"platform"} // "Perl/NetAsyncAMQP"
$args{"platform"}
MC/DC0%0/1
$args{"product"} // __PACKAGE__
$args{"product"}
MC/DC0%0/1
$args{"information"} // "http://search.cpan.org/perldoc?Net::Async::AMQP"
$args{"information"}
MC/DC0%0/2
$args{"version"} // $Net::Async::AMQP::VERSION
$args{"version"} $Net::Async::AMQP::VERSION
Truth table50%1/2
A
A= $args{"platform"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"product"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"information"}
Aresult
11
00
Truth table33%1/3
A // B
A= $args{"version"}B= $Net::Async::AMQP::VERSION
ABresult
1X1
011
000
452 );
453  
454 $self->push_pending(
455 1 ▶ 'Connection::Start' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
456 1 my ($self, $frame) = @_;
457 1 my $method_frame = $frame->method_frame;
458 1 my @mech = split ' ', $method_frame->mechanisms;
459 1 ▶ die "Auth mechanism " . AUTH_MECH . " not supported, unable to continue - options were: @mech" unless grep $_ eq AUTH_MECH, @mech;
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq "AMQPLAIN"), @mech) 0 1 1
460 my $output = Net::AMQP::Frame::Method->new(
461 channel => 0,
462 method_frame => Net::AMQP::Protocol::Connection::StartOk->new(
463 client_properties => \%client_prop,
464 mechanism => AUTH_MECH,
465 locale => $args{locale} // 'en_GB',
466 response => {
467 LOGIN => $args{user},
468 PASSWORD => $args{pass},
469 },
470 1 ▶ ),
Condition50%1/2
$args{"locale"} // "en_GB"
l!l
01
MC/DC0%0/1
$args{"locale"} // "en_GB"
$args{"locale"}
Truth table50%1/2
A
A= $args{"locale"}
Aresult
11
00
471 );
472 1 $self->setup_tuning(%args);
473 1 $self->send_frame($output);
474 }
475 1 );
476  
477 # Send the initial header bytes. It'd be nice
478 # if we could use L<Net::AMQP::Protocol/header>
479 # for this, but it seems to be sending 1 for
480 # the protocol ID, and the revision number is
481 # before the major/minor version.
482 # $self->write(Net::AMQP::Protocol->header);
483 1 $self->write($self->header_bytes);
484 1 $self
485 }
486  
487 =head2 setup_tuning
488  
489 Applies listener for the Connection::Tune message, used for determining max frame size and heartbeat settings.
490  
491 Returns $self.
492  
493 =cut
494  
495 1 ▶sub setup_tuning {
Subroutine setup_tuning: called CC=1 SCAR=0.1
Pod documented
496 1 my $self = shift;
497 1 my %args = @_;
498 $self->push_pending(
499 1 ▶ 'Connection::Tune' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
500 1 my ($self, $frame) = @_;
501 1 my $method_frame = $frame->method_frame;
502 # Lowest value for frame max wins - our predef constant, or whatever the server suggests
503 1 $self->frame_max(my $frame_max = min $method_frame->frame_max, $self->MAX_FRAME_SIZE);
504 1 ▶ $self->channel_max(my $channel_max = $method_frame->channel_max || $self->max_channels || $self->MAX_CHANNELS);
Condition33%2/6
$method_frame->channel_max || $self->max_channels || $self->MAX_CHANNELS
l!l&&r!l&&!r
100
$method_frame->channel_max or $self->max_channels
l!l&&r!l&&!r
100
MC/DC0%0/3
$method_frame->channel_max || $self->max_channels || $self->MAX_CHANNELS
$method_frame->channel_max $self->max_channels $self->MAX_CHANNELS
Truth table25%1/4
A or B || C
A= $method_frame->channel_maxB= $self->max_channelsC= $self->MAX_CHANNELS
ABCresult
1XX1
01X1
0011
0000
505 1 $self->debug_printf("Remote says %d channels, will use %d", $method_frame->channel_max, $channel_max);
506 1 $self->{channel} = 0;
507 1 $self->send_frame(
508 Net::AMQP::Protocol::Connection::TuneOk->new(
509 channel_max => $channel_max,
510 frame_max => $frame_max,
511 heartbeat => $self->heartbeat_interval,
512 )
513 );
514 1 $self->open_connection(%args);
515 }
516 1 );
517 }
518  
519 =head2 open_connection
520  
521 Establish a new connection to a vhost - this is called after tuning is complete,
522 and must happen before any channel connections are attempted.
523  
524 Returns $self.
525  
526 =cut
527  
528 1 ▶sub open_connection {
Subroutine open_connection: called CC=2 SCAR=7.0
Pod documented
529 1 my $self = shift;
530 1 my %args = @_;
531 1 $self->setup_connection(%args);
532 $self->send_frame(
533 Net::AMQP::Frame::Method->new(
534 method_frame => Net::AMQP::Protocol::Connection::Open->new(
535 1 ▶ virtual_host => $args{vhost} // '/',
Condition50%1/2
$args{"vhost"} // "/"
l!l
10
MC/DC0%0/1
$args{"vhost"} // "/"
$args{"vhost"}
Truth table50%1/2
A
A= $args{"vhost"}
Aresult
11
00
536 capabilities => '',
537 insist => 1,
538 ),
539 )
540 );
541 1 $self
542 }
543  
544 =head2 setup_connection
545  
546 Applies listener for the Connection::OpenOk message, which triggers the
547 C<connected> event.
548  
549 Returns $self.
550  
551 =cut
552  
553 1 ▶sub setup_connection {
Subroutine setup_connection: called CC=1 SCAR=0.0
Pod documented
554 1 my $self = shift;
555 1 my %args = @_;
556 $self->push_pending(
557 1 ▶ 'Connection::OpenOk' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
558 1 my ($self, $frame) = @_;
559 1 my $method_frame = $frame->method_frame;
560 1 $self->debug_printf("OpenOk received");
561 1 $self->connected->done;
562 1 $self->bus->invoke_event(connected =>);
563 }
564 1 );
565 1 $self
566 }
567  
568 =head2 connected
569  
570 Returns a L<Future> which will resolve when the MQ connection is ready
571 for use.
572  
573 =cut
574  
575 1 ▶sub connected {
Subroutine connected: called CC=2 SCAR=8.1
Pod documented
576 1 my ($self) = @_;
577 1 ▶ $self->{connected} ||= $self->future(set_label => 'MQ connection');
Condition33%1/3
$self->{"connected"} ||= $self->future("set_label", "MQ connection")
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"connected"} ||= $self->future("set_label", "MQ connection")
$self->{"connected"} $self->future("set_label", "MQ connection")
Truth table33%1/3
A ||= B
A= $self->{"connected"}B= $self->future("set_label", "MQ connection")
ABresult
1X1
011
000
578 }
579  
580 =head2 next_channel
581  
582 Returns the next available channel ready for L</open_channel>.
583 Note that whatever it reports will be completely wrong if you've
584 manually specified a channel anywhere, so don't do that.
585  
586 If channels have been closed on this connection, those IDs will be
587 reused in preference to handing out a new ID.
588  
589 =cut
590  
591 217 ▶sub next_channel {
Subroutine next_channel: called CC=5 SCAR=16.1
Pod documented
592 217 my $self = shift;
593 217 ▶ $self->{channel} //= 0;
Condition100%2/2
$self->{"channel"} //= 0
l!l
2152
MC/DC100%1/1
$self->{"channel"} //= 0
$self->{"channel"}
Truth table100%2/2
A
A= $self->{"channel"}
Aresult
11
00
594 217 ▶ return shift @{$self->{available_channel_id}} if @{$self->{available_channel_id} ||= [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"available_channel_id"} ||= [];} 10 207 217
Condition100%2/2
$self->{"available_channel_id"} ||= []
l!l
2143
MC/DC100%1/1
$self->{"available_channel_id"} ||= []
$self->{"available_channel_id"}
Truth table100%2/2
A
A= $self->{"available_channel_id"}
Aresult
11
00
595 207 ▶ return undef if $self->{channel} >= $self->channel_max;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"channel"} >= $self->channel_max 6 201 207
596 ++$self->{channel}
597 201 }
598  
599 =head2 create_channel
600  
601 Returns a new ::Channel instance, populating the map of assigned channels in the
602 process. Takes a single parameter:
603  
604 =over 4
605  
606 =item * $id - the channel ID, can be undef to assign via L</next_channel>
607  
608 =back
609  
610 =cut
611  
612 211 ▶sub create_channel {
Subroutine create_channel: called CC=3 SCAR=11.0
Pod documented
613 211 my ($self, $id) = @_;
614 211 ▶ $id //= $self->next_channel;
Condition100%3/3
$id //= $self->next_channel
l!l&&r!l&&!r
12082
MC/DC100%2/2
$id //= $self->next_channel
$id $self->next_channel
Truth table100%3/3
A //= B
A= $idB= $self->next_channel
ABresult
1X1
011
000
615 211 ▶ die "No channel available" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 2 209 211
616  
617 209 my $f = $self->loop->new_future;
618 209 $self->{channel_map}{$id} = $f;
619 209 $self->add_child(
620 my $c = Net::Async::AMQP::Channel->new(
621 amqp => $self,
622 future => $f,
623 id => $id,
624 )
625 );
626 209 $self->{channel_by_id}{$id} = $c;
627 209 $self->debug_printf("Record channel %d as %s", $id, $c);
628 209 return $c;
629 }
630  
631 =head2 open_channel
632  
633 Opens a new channel.
634  
635 Returns the new L<Net::Async::AMQP::Channel> instance.
636  
637 =cut
638  
639 1 ▶sub open_channel {
Subroutine open_channel: called CC=4 SCAR=15.1
Pod documented
640 1 my $self = shift;
641 1 my %args = @_;
642 1 my $channel;
643 1 ▶ if($args{channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'channel'}) { } 0 1 1
644 0 $channel = delete $args{channel};
645 0 ▶ extract_by { $channel == $_ } @{$self->{available_channel_id}} if exists $self->{available_channel_id};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"available_channel_id"} 0 0 0
646 } else {
647 1 $channel = $self->next_channel;
648 }
649 1 ▶ die "Channel " . $channel . " exists already: " . $self->{channel_map}{$channel} if exists $self->{channel_map}{$channel};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"channel_map"}{$channel} 0 1 1
650 1 my $c = $self->create_channel($channel);
651 1 my $f = $c->future;
652  
653 1 my $frame = Net::AMQP::Frame::Method->new(
654 method_frame => Net::AMQP::Protocol::Channel::Open->new,
655 );
656 1 $frame->channel($channel);
657 $c->push_pending(
658 1 ▶ 'Channel::OpenOk' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
659 1 my ($c, $frame) = @_;
660 1 my $f = $self->{channel_map}{$frame->channel};
661 1 ▶ $f->done($c) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 1 0 1
662 }
663 1 );
664 1 $self->send_frame($frame);
665 1 return $f;
666 }
667  
668 =head2 close
669  
670 Close the connection.
671  
672 Returns a L<Future> which will resolve with C<$self> when the connection is closed.
673  
674 =cut
675  
676 1 ▶sub close {
Subroutine close: called CC=4 SCAR=17.6
Pod documented
677 1 my $self = shift;
678 1 my %args = @_;
679  
680 1 ▶ $self->heartbeat_send_timer->stop if $self->heartbeat_send_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $self->heartbeat_send_timer 0 1 1
681  
682 1 my $f = $self->loop->new_future;
683  
684 # We might end up with a connection shutdown rather
685 # than a clean Connection::Close response, so
686 # we need to handle both possibilities
687 1 my @handler;
688 $self->bus->subscribe_to_event(
689 @handler = (
690 0 ▶ close => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
691 0 my ($ev, $reason) = @_;
692 0 splice @handler;
693 0 eval { $ev->unsubscribe; };
694 0 ▶ return unless $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 0 0
695 0 ▶ $f->done($reason) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
696 0 weaken $f;
697 }
698 )
699 1 );
700  
701 my $frame = Net::AMQP::Frame::Method->new(
702 method_frame => Net::AMQP::Protocol::Connection::Close->new(
703 reply_code => $args{code} // 320,
704 1 ▶ reply_text => $args{reason} // 'Request connection close',
Condition50%2/4
$args{"code"} // 320
l!l
01
$args{"reason"} // "Request connection close"
l!l
01
MC/DC0%0/1
$args{"code"} // 320
$args{"code"}
MC/DC0%0/1
$args{"reason"} // "Request connection close"
$args{"reason"}
Truth table50%1/2
A
A= $args{"code"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"reason"}
Aresult
11
00
705 ),
706 );
707 1 $self->push_pending(
708 'Connection::CloseOk' => [ $f, $self ],
709 );
710 1 $self->send_frame($frame);
711  
712 # ... and make sure we clean up after ourselves
713 1 ▶ $f->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
714 1 $self->bus->unsubscribe_from_event(
715 @handler
716 );
717 1 ▶ weaken $f if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 1 0 1
718 1 });
719 }
720  
721 =head2 channel_closed
722  
723 =cut
724  
725 10 ▶sub channel_closed {
Subroutine channel_closed: called CC=3 SCAR=11.2
Pod documented
726 10 my ($self, $id) = @_;
727 10 ▶ my $f = delete $self->{channel_map}{$id}
Branch: 1 branch
BranchTrueFalseTotal
my $f = delete $self->{"channel_map"}{$id} or die "Had a close indication for channel $id but this channel is unknown" 10 0 10
728 or die "Had a close indication for channel $id but this channel is unknown";
729 10 ▶ $f->cancel unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 10 0 10
730 10 $self->remove_child(delete $self->{channel_by_id}{$id});
731  
732 # Record this ID as available for the next time we need to open a new channel
733 10 push @{$self->{available_channel_id}}, $id;
734 10 $self
735 }
736  
737 5 ▶sub channel_by_id { my $self = shift; $self->{channel_by_id}{+shift} }
Subroutine channel_by_id: called CC=1 SCAR=0.0
Pod undocumented
738  
739 =head2 next_pending
740  
741 Retrieves the next pending handler for the given incoming frame type (see L<Net::Async::AMQP::Utils/amqp_frame_type>),
742 and calls it.
743  
744 Takes the following parameters:
745  
746 =over 4
747  
748 =item * $type - the frame type, such as 'Basic::ConnectOk'
749  
750 =item * $frame - the frame itself
751  
752 =back
753  
754 Returns $self.
755  
756 =cut
757  
758 4 ▶sub next_pending {
Subroutine next_pending: called CC=6 SCAR=19.9
Pod documented
759 4 my ($self, $type, $frame) = @_;
760 4 $self->debug_printf("Check next pending for %s", $type);
761  
762 4 ▶ if($type eq 'Connection::Close') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Connection::Close") 0 4 4
763 0 $self->on_closed($frame->method_frame->reply_text);
764 0 return $self;
765 }
766  
767 4 ▶ if(my $next = shift @{$self->{pending}{$type} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = shift @{$self->{'pending'}{$type} or [];}) { } 4 0 4
Condition50%1/2
$self->{'pending'}{$type} || []
l!l
40
MC/DC0%0/1
$self->{'pending'}{$type} || []
$self->{'pending'}{$type}
Truth table50%1/2
A
A= $self->{'pending'}{$type}
Aresult
11
00
768 # We have a registered handler for this frame type. This usually
769 # means that we've sent a frame and are awaiting a response.
770 4 ▶ if(ref($next) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next eq 'ARRAY') { } 1 3 4
771 1 my ($f, @args) = @$next;
772 1 ▶ $f->done(@args) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 1 0 1
773 } else {
774 3 $next->($self, $frame, @_);
775 }
776 } else {
777 # It's quite possible we'll see unsolicited frames back from
778 # the server: these will typically be errors, connection close,
779 # or consumer cancellation if the consumer_cancel_notify
780 # option is set (RabbitMQ). We don't expect many so report
781 # them when in debug mode.
782 0 $self->debug_printf("We had no pending handlers for %s, raising as event", $type);
783 0 $self->bus->invoke_event(
784 unexpected_frame => $type, $frame
785 );
786 }
787 4 $self
788 }
789  
790 =head1 METHODS - Accessors
791  
792 =head2 host
793  
794 The current host.
795  
796 =cut
797  
798 0 ▶sub host { shift->{host} }
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
799  
800 =head2 vhost
801  
802 Virtual host.
803  
804 =cut
805  
806 0 ▶sub vhost { shift->{vhost} }
Subroutine vhost: not called CC=1 SCAR=6.9
Pod documented
807  
808 =head2 port
809  
810 Port number. Usually 5672.
811  
812 =cut
813  
814 0 ▶sub port { shift->{port} }
Subroutine port: not called CC=1 SCAR=6.9
Pod documented
815  
816 =head2 user
817  
818 MQ user.
819  
820 =cut
821  
822 0 ▶sub user { shift->{user} }
Subroutine user: not called CC=1 SCAR=6.9
Pod documented
823  
824 =head2 frame_max
825  
826 Maximum number of bytes allowed in any given frame. This is the
827 value negotiated with the remote server.
828  
829 =cut
830  
831 1 ▶sub frame_max {
Subroutine frame_max: called CC=2 SCAR=7.0
Pod documented
832 1 my $self = shift;
833 1 ▶ return $self->{frame_max} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
834  
835 1 $self->{frame_max} = shift;
836 1 $self
837 }
838  
839 =head2 channel_max
840  
841 Maximum number of channels. This is whatever we ended up with after initial negotiation.
842  
843 =cut
844  
845 208 ▶sub channel_max {
Subroutine channel_max: called CC=4 SCAR=14.0
Pod documented
846 208 my $self = shift;
847 208 ▶ return $self->{channel_max} ||= $self->{max_channels} || $self->MAX_CHANNELS unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 207 1 208
Condition66%4/6
$self->{"channel_max"} ||= $self->{"max_channels"} || $self->MAX_CHANNELS
l!l&&r!l&&!r
20520
$self->{'max_channels'} || $self->MAX_CHANNELS
l!l&&r!l&&!r
110
MC/DC0%0/3
$self->{"channel_max"} ||= $self->{"max_channels"} || $self->MAX_CHANNELS
$self->{"channel_max"} $self->{'max_channels'} $self->MAX_CHANNELS
Truth table0%0/4
A ||= B || C
A= $self->{"channel_max"}B= $self->{'max_channels'}C= $self->MAX_CHANNELS
ABCresult
1XX1
01X1
0011
0000
848  
849 1 $self->{channel_max} = shift;
850 1 $self
851 }
852  
853 2 ▶sub max_channels { shift->{max_channels} }
Subroutine max_channels: called CC=1 SCAR=0.0
Pod documented
854  
855 =head2 last_frame_time
856  
857 Timestamp of the last frame we received from the remote. Used for handling heartbeats.
858  
859 =cut
860  
861 5 ▶sub last_frame_time {
Subroutine last_frame_time: called CC=3 SCAR=11.3
Pod documented
862 5 my $self = shift;
863 5 ▶ return $self->{last_frame_time} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 5 5
864  
865 5 $self->{last_frame_time} = shift;
866 5 ▶ $self->heartbeat_receive_timer->reset if $self->heartbeat_receive_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $self->heartbeat_receive_timer 0 5 5
867 5 $self
868 }
869  
870 =head2 stream
871  
872 Returns the current L<IO::Async::Stream> for the AMQP connection.
873  
874 =cut
875  
876 6 ▶sub stream { shift->{stream} }
Subroutine stream: called CC=1 SCAR=0.0
Pod documented
877  
878 =head2 incoming_message
879  
880 L<Future> for the current incoming message (received in two or more parts:
881 the header then all body chunks).
882  
883 =cut
884  
885 0 ▶sub incoming_message { shift->{incoming_message} }
Subroutine incoming_message: not called CC=1 SCAR=6.9
Pod documented
886  
887 =head1 METHODS - Internal
888  
889 The following methods are intended for internal use. They are documented
890 for completeness but should not normally be needed outside this library.
891  
892 =cut
893  
894 =head2 heartbeat_interval
895  
896 Current maximum interval between frames.
897  
898 =cut
899  
900 2 ▶sub heartbeat_interval { shift->{heartbeat_interval} //= HEARTBEAT_INTERVAL }
Subroutine heartbeat_interval: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"heartbeat_interval"} //= 0
l!l
11
MC/DC100%1/1
+(shift())->{"heartbeat_interval"} //= 0
+(shift())->{"heartbeat_interval"}
Truth table100%2/2
A
A= +(shift())->{"heartbeat_interval"}
Aresult
11
00
901  
902 =head2 missed_heartbeats_allowed
903  
904 How many times we allow the remote to miss the frame-sending deadline in a row
905 before we give up and close the connection. Defined by the protocol, should be
906 3x heartbeats.
907  
908 =cut
909  
910 0 ▶sub missed_heartbeats_allowed { 3 }
Subroutine missed_heartbeats_allowed: not called CC=1 SCAR=6.9
Pod documented
911  
912 =head2 apply_heartbeat_timer
913  
914 Enable both heartbeat timers.
915  
916 =cut
917  
918 0 ▶sub apply_heartbeat_timer {
Subroutine apply_heartbeat_timer: not called CC=1 SCAR=6.9
Pod documented
919 0 my $self = shift;
920 { # On expiry, will trigger a heartbeat send from us to the server
921 0 my $timer = IO::Async::Timer::Countdown->new(
922 delay => $self->heartbeat_interval,
923 on_expire => $self->curry::weak::send_heartbeat,
924 );
925 0 $self->add_child($timer);
926 0 $timer->start;
927 0 Scalar::Util::weaken($self->{heartbeat_send_timer} = $timer);
928 }
929 { # This timer indicates no traffic from the remote for 3*heartbeat
930 0 my $timer = IO::Async::Timer::Countdown->new(
931 delay => $self->missed_heartbeats_allowed * $self->heartbeat_interval,
932 on_expire => $self->curry::weak::handle_heartbeat_failure,
933 );
934 0 $self->add_child($timer);
935 0 $timer->start;
936 0 Scalar::Util::weaken($self->{heartbeat_receive_timer} = $timer);
937 }
938 $self
939 0 }
940  
941 =head2 reset_heartbeat
942  
943 Resets our side of the heartbeat timer.
944  
945 This is used to ensure we send data at least once every L</heartbeat_interval>
946 seconds.
947  
948 =cut
949  
950 5 ▶sub reset_heartbeat {
Subroutine reset_heartbeat: called CC=2 SCAR=8.1
Pod documented
951 5 my $self = shift;
952 5 ▶ return unless my $timer = $self->heartbeat_send_timer;
Branch: 1 branch
BranchTrueFalseTotal
unless my $timer = $self->heartbeat_send_timer 5 0 5
953  
954 0 $timer->reset;
955 }
956  
957  
958 =head2 heartbeat_receive_timer
959  
960 Timer for tracking frames we've received.
961  
962 =cut
963  
964 5 ▶sub heartbeat_receive_timer { shift->{heartbeat_receive_timer} }
Subroutine heartbeat_receive_timer: called CC=1 SCAR=0.0
Pod documented
965  
966 =head2 heartbeat_send_timer
967  
968 Timer for tracking when we're due to send out something.
969  
970 =cut
971  
972 6 ▶sub heartbeat_send_timer { shift->{heartbeat_send_timer} }
Subroutine heartbeat_send_timer: called CC=1 SCAR=0.0
Pod documented
973  
974 =head2 handle_heartbeat_failure
975  
976 Called when heartbeats are enabled and we've had no response from the server for 3 heartbeat
977 intervals (see L</missed_heartbeats_allowed>). We'd expect some frame from the remote - even
978 if just a heartbeat frame - at least once every heartbeat interval so if this triggers then
979 we're likely dealing with a dead or heavily loaded server.
980  
981 This will invoke the L</heartbeat_failure event> then close the connection.
982  
983 =cut
984  
985 0 ▶sub handle_heartbeat_failure {
Subroutine handle_heartbeat_failure: not called CC=1 SCAR=6.9
Pod documented
986 0 my $self = shift;
987 0 $self->debug_printf("Heartbeat timeout: no data received from server since %s, closing connection", $self->last_frame_time);
988  
989 0 $self->bus->invoke_event(
990 heartbeat_failure => $self->last_frame_time
991 );
992 0 $self->close;
993 }
994  
995 =head2 send_heartbeat
996  
997 Sends the heartbeat frame.
998  
999 =cut
1000  
1001 0 ▶sub send_heartbeat {
Subroutine send_heartbeat: not called CC=2 SCAR=17.9
Pod documented
1002 0 my $self = shift;
1003 0 $self->debug_printf("Sending heartbeat frame");
1004  
1005 # Heartbeat messages apply to the connection rather than
1006 # individual channels, so we use channel 0 to represent this
1007 0 $self->send_frame(
1008 Net::AMQP::Frame::Heartbeat->new,
1009 channel => 0,
1010 );
1011  
1012 # Ensure heartbeat timer is active for next time
1013 0 ▶ if(my $timer = $self->heartbeat_send_timer) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timer = $self->heartbeat_send_timer) 0 0 0
1014 0 $timer->reset;
1015 0 $timer->start;
1016 }
1017 }
1018  
1019 =head2 push_pending
1020  
1021 Adds the given handler(s) to the pending handler list for the given type(s).
1022  
1023 Takes one or more of the following parameter pairs:
1024  
1025 =over 4
1026  
1027 =item * $type - the frame type, see L<Net::Async::AMQP::Utils/amqp_frame_type>
1028  
1029 =item * $code - the coderef to call, will be invoked once as follows when a matching frame is received:
1030  
1031 $code->($self, $frame, @_)
1032  
1033 =back
1034  
1035 Returns C< $self >.
1036  
1037 =cut
1038  
1039 4 ▶sub push_pending {
Subroutine push_pending: called CC=2 SCAR=6.9
Pod documented
1040 4 my $self = shift;
1041 4 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 4 8
1042 4 my ($type, $code) = splice @_, 0, 2;
1043 4 push @{$self->{pending}{$type}}, $code;
1044 }
1045 4 return $self;
1046 }
1047  
1048 =head2 remove_pending
1049  
1050 Removes a coderef from the pending event handler.
1051  
1052 Returns C< $self >.
1053  
1054 =cut
1055  
1056 0 ▶sub remove_pending {
Subroutine remove_pending: not called CC=3 SCAR=24.8
Pod documented
1057 0 my $self = shift;
1058 0 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1059 0 my ($type, $code) = splice @_, 0, 2;
1060 # This is the same as extract_by { $_ eq $code } @{$self->{pending}{$type}};,
1061 # but since we'll be calling it a lot might as well do it inline:
1062 splice
1063 0 @{$self->{pending}{$type}},
1064 $_,
1065 0 1 for grep {
1066 0 $self->{pending}{$type}[$_] eq $code
1067 0 } reverse 0..$#{$self->{pending}{$type}};
1068 }
1069 0 return $self;
1070 }
1071  
1072 =head2 write
1073  
1074 Writes data to the server.
1075  
1076 Returns a L<Future> which will resolve to an empty list when
1077 done.
1078  
1079 =cut
1080  
1081 6 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod documented
1082 6 my $self = shift;
1083 6 $self->stream->write(@_)
1084 }
1085  
1086 =head2 process_frame
1087  
1088 Process a single incoming frame.
1089  
1090 Takes the following parameters:
1091  
1092 =over 4
1093  
1094 =item * $frame - the L<Net::AMQP::Frame> instance
1095  
1096 =back
1097  
1098 Returns $self.
1099  
1100 =cut
1101  
1102 5 ▶sub process_frame {
Subroutine process_frame: called CC=5 SCAR=18.0
Pod documented
1103 5 my ($self, $frame) = @_;
1104 5 $self->debug_printf("Received %s", amqp_frame_info($frame));
1105  
1106 5 my $frame_type = amqp_frame_type($frame);
1107  
1108 5 ▶ if($frame_type eq 'Heartbeat') {
Branch: 2 branches
BranchTrueFalseTotal
if ($frame_type eq 'Heartbeat') { } 0 5 5
elsif (my $ch = $self->channel_by_id($frame->channel)) { } 1 4 5
1109 # Ignore these completely. Since we have the last frame update at the data-read
1110 # level, there's nothing for us to do here.
1111 0 $self->debug_printf("Heartbeat received");
1112  
1113 # A peer that receives an invalid heartbeat frame MUST raise a connection
1114 # exception with reply code 501 (frame error)
1115 0 ▶ $self->close(
Branch: 1 branch
BranchTrueFalseTotal
if $frame->channel 0 0 0
1116 code => 501,
1117 reason => 'Frame error - heartbeat should have channel 0'
1118 ) if $frame->channel;
1119  
1120 0 return $self;
1121 } elsif(my $ch = $self->channel_by_id($frame->channel)) {
1122 1 $self->debug_printf("Processing frame %s on channel %d", $frame_type, $ch);
1123 1 ▶ return $self if $ch->next_pending($frame);
Branch: 1 branch
BranchTrueFalseTotal
if $ch->next_pending($frame) 1 0 1
1124 }
1125  
1126 4 $self->debug_printf("Processing connection frame %s", $frame_type);
1127  
1128 4 $self->next_pending($frame_type, $frame);
1129  
1130 4 return $self;
1131 }
1132  
1133 =head2 split_payload
1134  
1135 Splits a message into separate frames.
1136  
1137 Takes the $payload as a scalar containing byte data, and the following parameters:
1138  
1139 =over 4
1140  
1141 =item * exchange - where we're sending the message
1142  
1143 =item * routing_key - other part of message destination
1144  
1145 =back
1146  
1147 Additionally, the following headers can be passed:
1148  
1149 =over 4
1150  
1151 =item * content_type
1152  
1153 =item * content_encoding
1154  
1155 =item * headers
1156  
1157 =item * delivery_mode
1158  
1159 =item * priority
1160  
1161 =item * correlation_id
1162  
1163 =item * reply_to
1164  
1165 =item * expiration
1166  
1167 =item * message_id
1168  
1169 =item * timestamp
1170  
1171 =item * type
1172  
1173 =item * user_id
1174  
1175 =item * app_id
1176  
1177 =item * cluster_id
1178  
1179 =back
1180  
1181 Returns list of frames suitable for passing to L</send_frame>.
1182  
1183 =cut
1184  
1185 0 ▶sub split_payload {
Subroutine split_payload: not called CC=3 SCAR=24.8
Pod documented
1186 0 my $self = shift;
1187 0 my $payload = shift;
1188 0 my %opts = @_;
1189  
1190 # Get the original content length first
1191 0 my $payload_size = length $payload;
1192  
1193 0 my @body_frames;
1194 0 ▶ while (length $payload) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $payload) 0 0 0
1195 0 my $chunk = substr $payload, 0, $self->frame_max - PAYLOAD_HEADER_LENGTH, '';
1196 0 push @body_frames, Net::AMQP::Frame::Body->new(
1197 payload => $chunk
1198 );
1199 }
1200  
1201 return
1202 Net::AMQP::Protocol::Basic::Publish->new(
1203 0 ▶ map {; $_ => $opts{$_} } grep defined($opts{$_}), qw(ticket exchange routing_key mandatory immediate)
Condition0%0/2
$opts{'weight'} || 0
l!l
00
MC/DC0%0/1
$opts{'weight'} || 0
$opts{'weight'}
Truth table0%0/2
A
A= $opts{'weight'}
Aresult
11
00
1204 ),
1205 Net::AMQP::Frame::Header->new(
1206 weight => $opts{weight} || 0,
1207 body_size => $payload_size,
1208 header_frame => Net::AMQP::Protocol::Basic::ContentHeader->new(
1209 0 map {; $_ => $opts{$_} } grep defined($opts{$_}), qw(
1210 content_type
1211 content_encoding
1212 headers
1213 delivery_mode
1214 priority
1215 correlation_id
1216 reply_to
1217 expiration
1218 message_id
1219 timestamp
1220 type
1221 user_id
1222 app_id
1223 cluster_id
1224 )
1225 ),
1226 ),
1227 @body_frames;
1228 }
1229  
1230 =head2 send_frame
1231  
1232 Send a single frame.
1233  
1234 Takes the $frame instance followed by these optional named parameters:
1235  
1236 =over 4
1237  
1238 =item * channel - which channel we should send on
1239  
1240 =back
1241  
1242 Returns a L<Future> which will resolve to an empty list
1243 when the frame has been written (this does not guarantee that the server has received it).
1244  
1245 =cut
1246  
1247 5 ▶sub send_frame {
Subroutine send_frame: called CC=7 SCAR=20.2
Pod documented
1248 5 my $self = shift;
1249 5 my $frame = shift;
1250 5 my %args = @_;
1251  
1252 # Apply defaults and wrap as required
1253 5 ▶ $frame = $frame->frame_wrap if $frame->isa("Net::AMQP::Protocol::Base");
Branch: 1 branch
BranchTrueFalseTotal
if $frame->isa("Net::AMQP::Protocol::Base") 1 4 5
1254 die "Frame has channel ID " . $frame->channel . " but we wanted " . $args{channel}
1255 5 ▶ if defined $frame->channel && defined $args{channel} && $frame->channel != $args{channel};
Branch: 1 branch
BranchTrueFalseTotal
if defined $frame->channel and defined $args{"channel"} and $frame->channel != $args{"channel"} 0 5 5
Condition50%3/6
defined $frame->channel and defined $args{"channel"} and $frame->channel != $args{"channel"}
!ll&&!rl&&r
500
defined $frame->channel and defined $args{"channel"}
!ll&&!rl&&r
320
MC/DC0%0/3
defined $frame->channel and defined $args{"channel"} and $frame->channel != $args{"channel"}
defined $frame->channel defined $args{"channel"} $frame->channel != $args{"channel"}
Truth table50%2/4
A and B and C
A= defined $frame->channelB= defined $args{"channel"}C= $frame->channel != $args{"channel"}
ABCresult
0XX0
10X0
1100
1111
1256  
1257 5 ▶ $frame->channel($args{channel} // 0) unless defined $frame->channel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame->channel 3 2 5
Condition50%1/2
$args{"channel"} // 0
l!l
03
MC/DC0%0/1
$args{"channel"} // 0
$args{"channel"}
Truth table50%1/2
A
A= $args{"channel"}
Aresult
11
00
1258  
1259 5 $self->debug_printf("Sending %s", amqp_frame_info($frame));
1260  
1261 # Get bytes to send across our transport
1262 5 my $data = $frame->to_raw_frame;
1263  
1264 # warn "Sending data: " . Dumper($frame) . "\n";
1265 5 $self->write(
1266 $data,
1267 )->on_done($self->curry::reset_heartbeat)
1268 }
1269  
1270 =head2 header_bytes
1271  
1272 Byte string representing the header bytes we should send on initial TCP connect.
1273 Net::AMQP uses AMQP\x01\x01\x09\x01, which does not appear to comply with AMQP 0.9.1
1274 section 4.2.2.
1275  
1276 =cut
1277  
1278 2 ▶sub header_bytes { "AMQP\x00\x00\x09\x01" }
Subroutine header_bytes: called CC=1 SCAR=0.0
Pod documented
1279  
1280 3 ▶sub _add_to_loop {
Subroutine _add_to_loop: called CC=1 SCAR=0.0
1281 3 my ($self, $loop) = @_;
1282 3 $self->debug_printf("Added %s to loop", $self);
1283 }
1284  
1285 =head1 future
1286  
1287 Returns a new L<IO::Async::Future> instance.
1288  
1289 Supports optional named parameters for setting label etc.
1290  
1291 =cut
1292  
1293 1 ▶sub future {
Subroutine future: called CC=3 SCAR=11.0
Pod undocumented
1294 1 my $self = shift;
1295 1 my $f = $self->loop->new_future;
1296 1 ▶ while(my ($k, $v) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 1 1 2
1297 1 ▶ $f->can($k) ? $f->$k($v) : die "Unable to call method $k on $f";
Branch: 1 branch
BranchTrueFalseTotal
$f->can($k) ? : 1 0 1
1298 }
1299 $f
1300 1 }
1301  
1302 19 1;
1303  
1304 __END__
« AMQP/ConnectionManager/Channel.pm ↑ Summary AMQP/RPC/Base.pm »