AMQP/ConnectionManager.pm

filter: Statementstmt 16.2% 35 / 215 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 18.9% 11 / 58 Podpod 70.8% 17 / 24 totaltotal 16.1% 63 / 391 CC 39 SCAR 26.8
CC
39
Cov
12%
request_channel
56.0
request_connection
56.0
connect
30.0
CRAP 14.6
« AMQP/Channel.pm ↑ Summary AMQP/Queue.pm »
1 package Net::Async::AMQP::ConnectionManager;
2 6 $Net::Async::AMQP::ConnectionManager::VERSION = '0.031';
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::Async::AMQP::ConnectionManager - handle MQ connections
11  
12 =head1 VERSION
13  
14 version 0.031
15  
16 =head1 SYNOPSIS
17  
18 use IO::Async::Loop;
19 use Net::Async::AMQP;
20 my $loop = IO::Async::Loop->new;
21 $loop->add(
22 my $cm = Net::Async::AMQP::ConnectionManager->new
23 );
24 $cm->add(
25 host => 'localhost',
26 user => 'guest',
27 pass => 'guest',
28 vhost => 'vhost',
29 );
30 $cm->request_channel->then(sub {
31 my $ch = shift;
32 Future->needs_all(
33 $ch->declare_exchange(
34 'exchange_name'
35 ),
36 $ch->declare_queue(
37 'queue_name'
38 ),
39 )->transform(done => sub { $ch })
40 })->then(sub {
41 my $ch = shift;
42 $ch->bind_queue(
43 'exchange_name',
44 'queue_name',
45 '*'
46 )
47 })->get;
48  
49 =cut
50  
51 6 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 6 ▶use Future::Utils qw(call try_repeat fmap_void);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 6 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55 6 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
56 6 ▶use List::UtilsBy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 6 ▶use Net::Async::AMQP;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 6 ▶use Net::Async::AMQP::ConnectionManager::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 6 ▶use Net::Async::AMQP::ConnectionManager::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 =head1 DESCRIPTION
63  
64 =head2 Channel management
65  
66 Each connection has N total available channels, recorded in a hash. The total number
67 of channels per connection is negotiated via the initial AMQP Tune/TuneOk sequence on
68 connection.
69  
70 We also maintain lists:
71  
72 =over 4
73  
74 =item * Unassigned channel - these are channels which were in use and have now been released.
75  
76 =item * Closed channel - any time a channel is closed, the ID is pushed onto this list so we can reopen it later without needing to scan the hash, contains arrayrefs of [$mq_conn, $id]
77  
78 =back
79  
80 Highest-assigned ID is also recorded per connection.
81  
82 if(have unassigned) {
83 return shift unassigned
84 } elsif(have closed) {
85 my $closed = shift closed;
86 return $closed->{mq}->open_channel($closed->{id})
87 } elsif(my $next_id = $mq->next_id) {
88 return $mq->open_channel($next_id)
89 } else {
90
91 }
92  
93 Calling methods on the channel proxy will establish
94 a cycle for the duration of the pending request.
95 This cycle will not be resolved until after all
96 the callbacks have completed for a given request.
97  
98 The channel object does not expose any methods that allow
99 altering QoS or other channel state settings. These must be
100 requested on channel assignment. This does not necessarily
101 mean that any QoS change will require allocation of a new
102 channel.
103  
104 Bypassing the proxy object to change QoS flags is not recommended.
105  
106 =head2 Connection pool
107  
108 Connections are established on demand.
109  
110 =head1 METHODS
111  
112 =cut
113  
114 sub configure {
115 0 ▶ my ($self, %args) = @_;
Subroutine configure: not called CC=3 SCAR=24.8
Pod documented
116 0 for(qw(channel_retry_count connect_timeout)) {
117 0 ▶ $self->{$_} = delete $args{$_} if exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 0 0 0
118 }
119 0 $self->SUPER::configure(%args);
120 }
121  
122 =head2 request_channel
123  
124 Attempts to assign a channel with the given QoS settings.
125  
126 Available QoS settings are:
127  
128 =over 4
129  
130 =item * prefetch_count - number of messages that can be delivered at a time
131  
132 =item * prefetch_size - total size of messages allowed before acknowledging
133  
134 =item * confirm_mode - explicit publish ack
135  
136 =back
137  
138 Confirm mode isn't really QoS but it fits in with the others since it modifies
139 the channel state (and once enabled, cannot be disabled without closing and
140 reopening the channel).
141  
142 Will resolve to a L<Net::Async::AMQP::ConnectionManager::Channel> instance on success.
143  
144 =cut
145  
146 sub request_channel {
147 0 ▶ my $self = shift;
Subroutine request_channel: not called CC=7 SCAR=40.3
Pod documented
148 0 my %args = @_;
149  
150 0 ▶ die "We are shutting down" if $self->{shutdown_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown_future"} 0 0 0
151  
152 # Assign channel with matching QoS if available
153 0 my $k = $self->key_for_args(\%args);
154 0 ▶ if(exists $self->{channel_by_key}{$k} && @{$self->{channel_by_key}{$k}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"channel_by_key"}{$k} and @{$self->{"channel_by_key"}{$k};}) 0 0 0
Condition0%0/3
exists $self->{"channel_by_key"}{$k} and @{$self->{"channel_by_key"}{$k};}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"channel_by_key"}{$k} and @{$self->{"channel_by_key"}{$k};}
exists $self->{"channel_by_key"}{$k} @{$self->{"channel_by_key"}{$k};}
Truth table0%0/3
A and B
A= exists $self->{"channel_by_key"}{$k}B= @{$self->{"channel_by_key"}{$k};}
ABresult
0X0
100
111
155 0 my $ch = shift @{$self->{channel_by_key}{$k}};
156 0 $self->debug_printf("Assigning %d from by_key cache", $ch->id);
157 0 return Future->wrap(
158 Net::Async::AMQP::ConnectionManager::Channel->new(
159 channel => $ch,
160 manager => $self,
161 )
162 )
163 }
164  
165 # If we get here, we don't have an appropriate channel already available,
166 # so whichever means we use to obtain a channel will need to set QoS afterwards
167 0 my $f;
168  
169 0 ▶ if($self->can_reopen_channels && exists $self->{closed_channel} && @{$self->{closed_channel}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can_reopen_channels and exists $self->{'closed_channel'} and @{$self->{'closed_channel'};}) { } 0 0 0
Condition0%0/6
$self->can_reopen_channels and exists $self->{"closed_channel"} and @{$self->{"closed_channel"};}
!ll&&!rl&&r
000
$self->can_reopen_channels and exists $self->{"closed_channel"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->can_reopen_channels and exists $self->{"closed_channel"} and @{$self->{"closed_channel"};}
$self->can_reopen_channels exists $self->{"closed_channel"} @{$self->{"closed_channel"};}
Truth table0%0/4
A and B and C
A= $self->can_reopen_channelsB= exists $self->{"closed_channel"}C= @{$self->{"closed_channel"};}
ABCresult
0XX0
10X0
1100
1111
170 # If we have an ID for a closed channel then reuse that first.
171 0 my ($mq, $id) = @{shift @{$self->{closed_channel}}};
172 0 $self->debug_printf("Reopening closed channel %d", $id);
173 0 $f = $mq->open_channel(
174 channel => $id
175 );
176 } else {
177 # Try to get a channel - limit this to 3 attempts
178 0 my $count = 0;
179 $f = try_repeat {
180 $self->request_connection->then(sub {
181 0 ▶ my $mq = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 call {
183 # If we have any spare IDs on this connection, attempt to open
184 # a channel here
185 0 ▶ if(my $id = $mq->next_channel) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $mq->next_channel) 0 0 0
186 0 return $mq->open_channel(
187 channel => $id
188 )
189 }
190  
191 # No spare IDs, so record this to avoid hitting this MQ connection
192 # on the next request as well
193 0 $self->mark_connection_full($mq->amqp);
194  
195 # We can safely fail at this point, since we're in a loop and the
196 # next iteration should get a new MQ connection to try with
197 0 Future->fail(channel => 'no spare channels on connection');
198 }
199 0 ▶ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
200 } until => sub {
201 0 ▶ my $f = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
202 0 ▶ return 1 if $f->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_done 0 0 0
203 0 ▶ return 0 unless defined(my $retry = $self->channel_retry_count);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $retry = $self->channel_retry_count) 0 0 0
204 0 ▶ return 1 if ++$count > $retry
Branch: 1 branch
BranchTrueFalseTotal
if ++$count > $retry 0 0 0
205 }
206 0 }
207  
208 # Apply our QoS on the channel if we ever get one
209 return $f->then(sub {
210 0 ▶ my $ch = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 call {
212 0 ▶ $ch->bus->subscribe_to_event(
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 close => $self->curry::weak::on_channel_close($ch),
214 );
215 0 $self->apply_qos($ch => %args)
216 }
217 0 })->set_label(
218 'Channel QoS'
219 )->transform(
220 done => sub {
221 0 ▶ my $ch = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 0 $self->{channel_args}{$ch->id} = \%args;
223 0 $self->debug_printf("Assigning newly-created channel %d", $ch->id);
224 0 Net::Async::AMQP::ConnectionManager::Channel->new(
225 channel => $ch,
226 manager => $self,
227 )
228 }
229 0 );
230 }
231  
232 =head2 can_reopen_channels
233  
234 A constant which indicates whether we can reopen channels. The AMQP0.9.1
235 spec doesn't seem to explicitly allow this, but it works with RabbitMQ 3.4.3
236 (and probably older versions) so it's enabled by default.
237  
238 =cut
239  
240 0 ▶sub can_reopen_channels { 1 }
Subroutine can_reopen_channels: not called CC=1 SCAR=6.9
Pod documented
241  
242 =head2 channel_retry_count
243  
244 Returns the channel retry count. The default is 10, call L</configure>
245 with undef to retry indefinitely, 0 to avoid retrying at all:
246  
247 # Keep trying until it works
248 $mq->configure(channel_retry_count => undef);
249 # Don't retry at all
250 $mq->configure(channel_retry_count => 0);
251  
252 =cut
253  
254 sub channel_retry_count {
255 0 ▶ my $self = shift;
Subroutine channel_retry_count: not called CC=2 SCAR=17.9
Pod documented
256 # undef is a valid entry here
257 0 ▶ if(!exists $self->{channel_retry_count}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"channel_retry_count"}) 0 0 0
258 0 $self->{channel_retry_count} = 10;
259 }
260 $self->{channel_retry_count}
261 0 }
262  
263 =head2 connect_timeout
264  
265 Returns the current connection timeout. undef/zero means "no timeout".
266  
267 =cut
268  
269 0 ▶sub connect_timeout { shift->{connect_timeout} }
Subroutine connect_timeout: not called CC=1 SCAR=6.9
Pod documented
270  
271 =head2 apply_qos
272  
273 Set QoS on the given channel.
274  
275 Expects the L<Net::Async::AMQP::Channel> object as the first
276 parameter, followed by the key/value pairs corresponding to
277 the desired QoS settings:
278  
279 =over 4
280  
281 =item * prefetch_count - number of messages that can be delivered before ACK
282 is required
283  
284 =back
285  
286 Returns a L<Future> which will resolve to the original
287 L<Net::Async::AMQP::Channel> instance.
288  
289 =cut
290  
291 sub apply_qos {
292 0 ▶ my ($self, $ch, %args) = @_;
Subroutine apply_qos: not called CC=1 SCAR=6.9
Pod documented
293 (fmap_void {
294 0 ▶ my $k = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
295 0 my $v = $args{$k};
296 0 my $method = "qos_$k";
297 0 ▶ my $code = $self->can($method) or die "Unknown QoS setting $k (value $v)";
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can($method) or die "Unknown QoS setting $k (value $v)" 0 0 0
298 0 $code->($self, $ch, $k => $v);
299 } foreach => [
300 sort keys %args
301 ])->transform(
302 0 ▶ done => sub { $ch }
Subroutine __ANON__: not called CC=1 SCAR=6.9
303 0 )->set_label(
304 'Apply QoS settings'
305 );
306 }
307  
308 sub qos_prefetch_size {
309 0 ▶ my ($self, $ch, $k, $v) = @_;
Subroutine qos_prefetch_size: not called CC=1 SCAR=6.9
Pod undocumented
310 0 return $ch->qos(
311 $k => $v
312 )->set_label("Apply $k QoS");
313 }
314  
315 sub qos_prefetch_count {
316 0 ▶ my ($self, $ch, $k, $v) = @_;
Subroutine qos_prefetch_count: not called CC=1 SCAR=6.9
Pod undocumented
317 0 return $ch->qos(
318 $k => $v
319 )->set_label("Apply $k QoS");
320 }
321  
322 sub qos_confirm_mode {
323 0 ▶ my ($self, $ch) = @_;
Subroutine qos_confirm_mode: not called CC=1 SCAR=6.9
Pod undocumented
324 0 return $ch->confirm_mode(
325 )->set_label("Apply confirm_mode QoS");
326 }
327  
328 =head2 request_connection
329  
330 Attempts to connect to one of the known AMQP servers.
331  
332 =cut
333  
334 sub request_connection {
335 0 ▶ my ($self) = @_;
Subroutine request_connection: not called CC=7 SCAR=40.3
Pod documented
336 0 ▶ die "We are shutting down" if $self->{shutdown_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown_future"} 0 0 0
337 0 ▶ if(my $conn = $self->{pending_connection}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = $self->{"pending_connection"}) 0 0 0
338 0 $self->debug_printf("Requested connection and we have one pending, returning that");
339 0 return $conn
340 }
341  
342 0 ▶ if(exists $self->{available_connections} && @{$self->{available_connections}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"available_connections"} and @{$self->{"available_connections"};}) 0 0 0
Condition0%0/3
exists $self->{"available_connections"} and @{$self->{"available_connections"};}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"available_connections"} and @{$self->{"available_connections"};}
exists $self->{"available_connections"} @{$self->{"available_connections"};}
Truth table0%0/3
A and B
A= exists $self->{"available_connections"}B= @{$self->{"available_connections"};}
ABresult
0X0
100
111
343 0 $self->debug_printf("Assigning existing connection");
344 return Future->wrap(
345 Net::Async::AMQP::ConnectionManager::Connection->new(
346 0 amqp => $self->{available_connections}[0],
347 manager => $self,
348 )
349 )
350 }
351 0 ▶ die "No connection details available" unless $self->{amqp_host};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"amqp_host"} 0 0 0
352  
353 0 $self->debug_printf("New connection is required");
354 0 my $timeout = $self->connect_timeout;
355 Future->wait_any(
356 $self->{pending_connection} = $self->connect(
357 0 ▶ %{$self->next_host}
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 0 0 0
358 )->on_ready(sub {
359 0 ▶ delete $self->{pending_connection};
Subroutine __ANON__: not called CC=1 SCAR=6.9
360 })->transform(
361 done => sub {
362 0 ▶ my $mq = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
363 $mq->bus->subscribe_to_event(
364 close => sub {
365 # Drop this connection on close.
366 0 ▶ my ($ev) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
367 0 eval { $ev->unsubscribe; };
368 0 my $ref = Scalar::Util::refaddr($mq);
369 List::UtilsBy::extract_by {
370 0 ▶ Scalar::Util::refaddr($_) eq $ref
Subroutine __ANON__: not called CC=1 SCAR=6.9
371 0 } @{$self->{available_connections}};
372  
373 # Also remove from the full list...
374 List::UtilsBy::extract_by {
375 0 ▶ Scalar::Util::refaddr($_) eq $ref
Subroutine __ANON__: not called CC=1 SCAR=6.9
376 0 } @{$self->{full_connections}};
377  
378 # ... and any channels we had stashed
379 List::UtilsBy::extract_by {
380 0 ▶ Scalar::Util::refaddr($_->[0]) eq $ref
Subroutine __ANON__: not called CC=1 SCAR=6.9
381 0 } @{$self->{closed_channel}};
382  
383 # ... even the active ones
384 0 for my $k (sort keys %{$self->{channel_by_key}}) {
385 List::UtilsBy::extract_by {
386 0 ▶ Scalar::Util::refaddr($_->amqp) eq $ref
Subroutine __ANON__: not called CC=1 SCAR=6.9
387 0 } @{$self->{channel_by_key}{$k}};
388 }
389 }
390 0 );
391 0 my $conn = Net::Async::AMQP::ConnectionManager::Connection->new(
392 amqp => $mq,
393 manager => $self,
394 );
395 0 push @{$self->{available_connections}}, $mq;
396 0 $conn
397 }
398 )->set_label(
399 'Connect to MQ server'
400 ),
401 ( # Cancel the attempt if the timeout expires
402 $timeout ?
403 $self->loop->timeout_future(
404 after => $self->connect_timeout,
405 )->on_fail(sub {
406 0 ▶ $self->{pending_connection}->cancel if $self->{pending_connection} && !$self->{pending_connection}->is_ready;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pending_connection"} and not $self->{"pending_connection"}->is_ready 0 0 0
Condition0%0/3
$self->{"pending_connection"} and not $self->{"pending_connection"}->is_ready
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"pending_connection"} and not $self->{"pending_connection"}->is_ready
$self->{"pending_connection"} not $self->{"pending_connection"}->is_ready
Truth table0%0/3
A and not(B)
A= $self->{"pending_connection"}B= not $self->{"pending_connection"}->is_ready
ABresult
0X0
100
111
407 })
408 # ... if we had a timeout
409 0 : ()
410 )
411 )
412 }
413  
414 =head2 next_host
415  
416 Returns the next AMQP host.
417  
418 =cut
419  
420 sub next_host {
421 0 ▶ my $self = shift;
Subroutine next_host: not called CC=1 SCAR=6.9
Pod documented
422 0 $self->{amqp_host}[rand @{$self->{amqp_host}}]
423 }
424  
425 =head2 connect
426  
427 Attempts a connection to an AMQP host.
428  
429 =cut
430  
431 sub connect {
432 0 ▶ my ($self, %args) = @_;
Subroutine connect: not called CC=5 SCAR=34.0
Pod documented
433 0 ▶ die "We are shutting down" if $self->{shutdown_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown_future"} 0 0 0
434 0 $self->add_child(
435 my $amqp = Net::Async::AMQP->new
436 );
437 0 ▶ $amqp->configure(heartbeat_interval => delete $args{heartbeat}) if exists $args{heartbeat};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"heartbeat"} 0 0 0
438 0 ▶ $amqp->configure(max_channels => delete $args{max_channels}) if exists $args{max_channels};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"max_channels"} 0 0 0
439 0 ▶ $args{port} ||= 5672;
Condition0%0/2
$args{"port"} ||= 5672
l!l
00
MC/DC0%0/1
$args{"port"} ||= 5672
$args{"port"}
Truth table0%0/2
A
A= $args{"port"}
Aresult
11
00
440 0 $amqp->connect(
441 %args
442 )
443 }
444  
445 =head2 mark_connection_full
446  
447 Indicate that this connection has already allocated all available
448 channels.
449  
450 =cut
451  
452 sub mark_connection_full {
453 0 ▶ my ($self, $mq) = @_;
Subroutine mark_connection_full: not called CC=1 SCAR=6.9
Pod documented
454 # Drop this from the available connection list
455 0 push @{$self->{full_connections}}, $self->extract_conn(
456 $mq,
457 $self->{available_connections}
458 0 );
459 0 $self
460 }
461  
462 sub extract_conn {
463 0 ▶ my ($self, $conn, $stash) = @_;
Subroutine extract_conn: not called CC=1 SCAR=6.9
Pod undocumented
464 my @rslt = List::UtilsBy::extract_by {
465 0 ▶ Scalar::Util::refaddr($_) == Scalar::Util::refaddr($conn)
Subroutine __ANON__: not called CC=1 SCAR=6.9
466 0 } @$stash;
467 @rslt
468 0 }
469  
470 =head2 key_for_args
471  
472 Returns a key that represents the given arguments.
473  
474 =cut
475  
476 sub key_for_args {
477 0 ▶ my ($self, $args) = @_;
Subroutine key_for_args: not called CC=1 SCAR=6.9
Pod documented
478 0 join ',', map { "$_=$args->{$_}" } sort keys %$args;
479 }
480  
481 =head2 on_channel_close
482  
483 Called when one of our channels has been closed.
484  
485 =cut
486  
487 sub on_channel_close {
488 0 ▶ my ($self, $ch, $ev, %args) = @_;
Subroutine on_channel_close: not called CC=3 SCAR=24.8
Pod documented
489 0 $self->debug_printf("channel closure: %s", join ' ', @_);
490 # Channel closure only happens once per channel
491 0 eval { $ev->unsubscribe; };
492  
493 0 $self->debug_printf("Adding closed channel %d back to the available list", $ch->id);
494 0 ▶ my $amqp = $ch->amqp or die "This channel (" . $ch->id . ") has no AMQP connection";
Branch: 1 branch
BranchTrueFalseTotal
my $amqp = $ch->amqp or die 'This channel (' . $ch->id . ') has no AMQP connection' 0 0 0
495  
496 # We don't want to do anything with this channel if the parent connection is closed
497 0 ▶ return unless $self->connection_valid($amqp);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->connection_valid($amqp) 0 0 0
498  
499 0 push @{$self->{closed_channel}}, [ $amqp, $ch->id ];
500  
501 # If this connection was in the full list, add it back to the available
502 # list, since it now has spare channels
503 0 push @{$self->{available_connections}}, $self->extract_conn(
504 $amqp,
505 $self->{full_connections}
506 0 );
507 }
508  
509 =head2 release_channel
510  
511 Releases the given channel back to our channel pool.
512  
513 =cut
514  
515 sub release_channel {
516 0 ▶ my ($self, $ch) = @_;
Subroutine release_channel: not called CC=4 SCAR=30.0
Pod documented
517 0 ▶ return $self unless $ch && $ch->amqp && $self->connection_valid($ch->amqp);
Branch: 1 branch
BranchTrueFalseTotal
unless $ch and $ch->amqp and $self->connection_valid($ch->amqp) 0 0 0
Condition0%0/6
$ch and $ch->amqp and $self->connection_valid($ch->amqp)
!ll&&!rl&&r
000
$ch and $ch->amqp
!ll&&!rl&&r
000
MC/DC0%0/3
$ch and $ch->amqp and $self->connection_valid($ch->amqp)
$ch $ch->amqp $self->connection_valid($ch->amqp)
Truth table0%0/4
A and B and C
A= $chB= $ch->amqpC= $self->connection_valid($ch->amqp)
ABCresult
0XX0
10X0
1100
1111
518  
519 0 $self->debug_printf("Releasing channel %d", $ch->id);
520 0 my $args = $self->{channel_args}{$ch->id};
521 0 my $k = $self->key_for_args($args);
522 0 push @{$self->{channel_by_key}{$k}}, $ch;
523 0 $self
524 }
525  
526 =head2 connection_valid
527  
528 Returns true if this connection is one we know about, false if it's
529 closed or otherwise not usable.
530  
531 =cut
532  
533 sub connection_valid {
534 0 ▶ my ($self, $amqp) = @_;
Subroutine connection_valid: not called CC=2 SCAR=17.9
Pod documented
535 0 my $ref = Scalar::Util::refaddr($amqp);
536 return (
537 grep {
538 0 Scalar::Util::refaddr($_) eq $ref
539 0 ▶ } @{$self->{available_connections}}, @{$self->{full_connections}}
Branch: 1 branch
BranchTrueFalseTotal
grep({builtin::refaddr($_) eq $ref;} @{$$self{'available_connections'};}, @{$$self{'full_connections'};}) ? : 0 0 0
540 ) ? 1 : 0;
541 }
542  
543 =head2 add
544  
545 Adds connection details for an AMQP server to the pool.
546  
547 =cut
548  
549 sub add {
550 0 ▶ my ($self, %args) = @_;
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
551 0 push @{$self->{amqp_host}}, \%args;
552 }
553  
554 =head2 exch
555  
556 =cut
557  
558 sub exch {
559 0 ▶ my ($self, $exch) = @_;
Subroutine exch: not called CC=2 SCAR=17.9
Pod documented
560 0 ▶ return $self->{exchange}{$exch} if exists $self->{exchange}{$exch};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"exchange"}{$exch} 0 0 0
561 $self->{exchange}{$exch} = $self->request_channel->then(sub {
562 0 ▶ my $ch = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
563 0 $ch->declare_exchange(
564 $exch
565 )
566 0 });
567 }
568  
569 sub queue {
570 0 ▶ my ($self, $q) = @_;
Subroutine queue: not called CC=2 SCAR=17.9
Pod undocumented
571 0 ▶ return $self->{queue}{$q} if exists $self->{queue}{$q};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"queue"}{$q} 0 0 0
572 $self->{queue}{$q} = $self->request_channel->then(sub {
573 0 ▶ my $ch = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
574 0 $ch->declare_queue(
575 $q
576 )
577 0 });
578 }
579  
580 =head2 release_connection
581  
582 Releases a connection.
583  
584 Doesn't really do anything.
585  
586 =cut
587  
588 sub release_connection {
589 0 ▶ my ($self, $mq) = @_;
Subroutine release_connection: not called CC=1 SCAR=6.9
Pod documented
590 0 $self->debug_printf("Releasing connection %s", $mq);
591 }
592  
593 sub connection_count {
594 0 ▶ my ($self) = @_;
Subroutine connection_count: not called CC=1 SCAR=6.9
Pod undocumented
595 0 @{$self->{available_connections}} + @{$self->{full_connections}}
596 }
597  
598 sub _add_to_loop {
599 0 ▶ my ($self, $loop) = @_;
Subroutine _add_to_loop: not called CC=3 SCAR=24.8
600 0 ▶ $self->{available_connections} ||= [];
Condition0%0/2
$self->{"available_connections"} ||= []
l!l
00
MC/DC0%0/1
$self->{"available_connections"} ||= []
$self->{"available_connections"}
Truth table0%0/2
A
A= $self->{"available_connections"}
Aresult
11
00
601 0 ▶ $self->{full_connections} ||= [];
Condition0%0/2
$self->{"full_connections"} ||= []
l!l
00
MC/DC0%0/1
$self->{"full_connections"} ||= []
$self->{"full_connections"}
Truth table0%0/2
A
A= $self->{"full_connections"}
Aresult
11
00
602 }
603  
604 sub shutdown {
605 0 ▶ my $self = shift;
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod undocumented
606 0 $self->debug_printf("Shutdown started");
607 0 ▶ die "Shutdown already in progress?" if $self->{shutdown_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown_future"} 0 0 0
608 0 my $start = [Time::HiRes::gettimeofday];
609 $self->{shutdown_future} = Future->wait_all(
610 0 map $_->close, @{$self->{available_connections}}
611 )->on_ready(sub {
612 0 ▶ delete $self->{shutdown_future};
Subroutine __ANON__: not called CC=1 SCAR=6.9
613 })->on_done(sub {
614 0 ▶ $self->debug_printf("All connections closed - elapsed %.3fs", Time::HiRes::tv_interval($start, [Time::HiRes::gettimeofday]));
Subroutine __ANON__: not called CC=1 SCAR=6.9
615 0 });
616 }
617  
618 6 1;
619  
620 __END__
« AMQP/Channel.pm ↑ Summary AMQP/Queue.pm »