AMQP/ConnectionManager.pm

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