| 1 | package Net::AMQP::ConnectionMgr; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||||||||||||||||
| 8 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 9 | 1 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | my $hostname = shift || 'localhost'; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'localhost'
MC/DC0%0/1
shift() || 'localhost'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | my $options = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | my $conn_class = shift || 'Net::AMQP::RabbitMQ'; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'Net::AMQP::RabbitMQ'
MC/DC0%0/1
shift() || 'Net::AMQP::RabbitMQ'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 13 | 1 | return bless | |||||||||||||||||||||||||||
| 14 | { hostname => $hostname, | ||||||||||||||||||||||||||||
| 15 | options => $options, | ||||||||||||||||||||||||||||
| 16 | conn_class => $conn_class, | ||||||||||||||||||||||||||||
| 17 | }, $class; | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | sub _rmq_connect { | ||||||||||||||||||||||||||
|
Subroutine
_rmq_connect: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 21 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 22 | 2 | ▶ | if (!$self->{conn_class}->can('new')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | eval "require $self->{conn_class}"; | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | die "Failed to load $self->{conn_class}: $@" | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 2 | $self->{rmq} = $self->{conn_class}->new(); | |||||||||||||||||||||||||||
| 29 | 2 | $self->{rmq}->connect($self->{hostname}, $self->{options}); | |||||||||||||||||||||||||||
| 30 | # we only need to run channel declarations, because resource | ||||||||||||||||||||||||||||
| 31 | # declarations get a specific channel which runs them | ||||||||||||||||||||||||||||
| 32 | 2 | $self->_run_channel_declarations(); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | sub _run_channel_declarations { | ||||||||||||||||||||||||||
|
Subroutine
_run_channel_declarations: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 36 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if (!$self->{declare_channel_data}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | $self->{declare_channel_data} = []; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 2 | my $data = $self->{declare_channel_data}; | |||||||||||||||||||||||||||
| 42 | 2 | my $channel_count = 0; | |||||||||||||||||||||||||||
| 43 | 2 | for my $code (@$data) { | |||||||||||||||||||||||||||
| 44 | 4 | $channel_count++; | |||||||||||||||||||||||||||
| 45 | 4 | $self->{rmq}->channel_open($channel_count); | |||||||||||||||||||||||||||
| 46 | 4 | $code->($self->{rmq}, $channel_count); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub with_connection_do { | ||||||||||||||||||||||||||
|
Subroutine
with_connection_do: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 2 | my ($self, $code, $retry) = @_; | |||||||||||||||||||||||||||
| 52 | 2 | ▶ | $retry ||= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$retry ||= 1
MC/DC0%0/1
$retry ||= 1
$retry
Truth table50%1/2
A
A= $retry
| |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | while ($retry >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 3 | eval { | |||||||||||||||||||||||||||
| 55 | 3 | ▶ | if (!$self->{rmq} || !$self->{rmq}->is_connected()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"rmq"} and $self->{"rmq"}->is_connected
MC/DC100%2/2
$self->{"rmq"} and $self->{"rmq"}->is_connected
$self->{"rmq"}
$self->{"rmq"}->is_connected
Truth table100%3/3
A and B
A= $self->{"rmq"}B= $self->{"rmq"}->is_connected
| |||||||||||||||||||||||||||||
| 56 | 2 | $self->_rmq_connect(); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 3 | $code->($self->{rmq}); | |||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||
| 60 | 3 | ▶ | if (!$@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 2 | last; | |||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 1 | $retry--; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | die $@; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub declare_channel { | ||||||||||||||||||||||||||
|
Subroutine
declare_channel: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 2 | my ($self, $code) = @_; | |||||||||||||||||||||||||||
| 73 | 2 | ▶ | if (!$self->{declare_channel_data}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | $self->{declare_channel_data} = []; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 2 | my $data = $self->{declare_channel_data}; | |||||||||||||||||||||||||||
| 77 | 2 | my $next_channel = scalar(@$data) + 1; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # we put a placeholder first, such that we don't get ourselves | ||||||||||||||||||||||||||||
| 80 | # into a bad position in case this code fails. | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $data->[$next_channel - 1] = sub { 1; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | my $rmq = $self->{rmq}; | |||||||||||||||||||||||||||
| 84 | 2 | ▶ | if ($rmq && $rmq->is_connected()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rmq and $rmq->is_connected
MC/DC0%0/2
$rmq and $rmq->is_connected
$rmq
$rmq->is_connected
Truth table33%1/3
A and B
A= $rmqB= $rmq->is_connected
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $self->with_connection_do(sub { $rmq->channel_open($next_channel); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 86 | 0 | $code->($rmq, $next_channel) }); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # if we got here, then we assign | ||||||||||||||||||||||||||||
| 90 | 2 | $data->[$next_channel - 1] = $code; | |||||||||||||||||||||||||||
| 91 | 2 | return $next_channel; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub declare_resource { | ||||||||||||||||||||||||||
|
Subroutine
declare_resource: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 1 | my ($self, $code) = @_; | |||||||||||||||||||||||||||
| 96 | 1 | ▶ | if (!$self->{declare_resouce_data}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | $self->{declare_resouce_data} = []; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 1 | my $data = $self->{declare_resource_data}; | |||||||||||||||||||||||||||
| 100 | 1 | ▶ | if (!$self->{declare_resource_channel}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | $self->{declare_resource_channel} = | ||||||||||||||||||||||||||||
| 102 | 1 | ▶ | $self->declare_channel(sub { $_->(@_) for @$data } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 1 | my $decl_ch = $self->{declare_resource_channel}; | |||||||||||||||||||||||||||
| 105 | 1 | ▶ | if ($self->{rmq} && $self->{rmq}->is_connected()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"rmq"} and $self->{"rmq"}->is_connected
MC/DC0%0/2
$self->{"rmq"} and $self->{"rmq"}->is_connected
$self->{"rmq"}
$self->{"rmq"}->is_connected
Truth table33%1/3
A and B
A= $self->{"rmq"}B= $self->{"rmq"}->is_connected
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $self->with_connection_do(sub { $code->($_[0], $decl_ch) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 1 | push @$data, $code; | |||||||||||||||||||||||||||
| 109 | 1 | return 1; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | 1; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||