| 1 | package Net::Async::AMQP::ConnectionManager::Channel; | ||||||||||
| 2 | 7 | $Net::Async::AMQP::ConnectionManager::Channel::VERSION = '0.031'; | |||||||||
| 3 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Net::Async::AMQP::ConnectionManager::Channel - channel proxy object | ||||||||||
| 9 | |||||||||||
| 10 | =head1 VERSION | ||||||||||
| 11 | |||||||||||
| 12 | version 0.031 | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 7 | ▶ | use Time::HiRes (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 7 | ▶ | use Future::Utils qw(fmap_void); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | use overload | ||||||||||
| 21 | 0 | ▶ | '""' => sub { shift->as_string }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 22 | 0 | ▶ | '0+' => sub { 0 + shift->id }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 23 | 0 | ▶ | bool => sub { 1 }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 24 | 7 | ▶ | fallback => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | =head1 METHODS | ||||||||||
| 27 | |||||||||||
| 28 | =head2 new | ||||||||||
| 29 | |||||||||||
| 30 | Instantiate. Expects at least the manager and channel named parameters. | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | sub new { | ||||||||||
| 35 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 36 | 0 | my $self = bless { @_ }, $class; | |||||||||
| 37 | 0 | Scalar::Util::weaken($_) for @{$self}{qw(manager channel)}; | |||||||||
| 38 | |||||||||||
| 39 | # ->bus proxies to L<Net::Async::Channel/bus> via AUTOLOAD | ||||||||||
| 40 | 0 | $self->bus->subscribe_to_event( | |||||||||
| 41 | my @ev = ( | ||||||||||
| 42 | listener_start => $self->curry::weak::_listener_start, | ||||||||||
| 43 | listener_stop => $self->curry::weak::_listener_stop, | ||||||||||
| 44 | ) | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | $self->{cleanup}{events} = sub { | ||||||||||
| 48 | 0 | ▶ | shift->bus->unsubscribe_from_event(splice @ev); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 49 | 0 | Future->wrap; | |||||||||
| 50 | 0 | }; | |||||||||
| 51 | 0 | $self | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | =head2 _listener_start | ||||||||||
| 55 | |||||||||||
| 56 | Apply cleanup handler for a consumer: since we'll be releasing the | ||||||||||
| 57 | channel back into the general pool, any consumer that's still active | ||||||||||
| 58 | must be cancelled first. | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | |||||||||||
| 62 | sub _listener_start { | ||||||||||
| 63 | 0 | ▶ | my ($self, $ev, $ctag) = @_; | ||||||||
|
Subroutine
_listener_start: not called CC=2 SCAR=17.9
| |||||||||||
| 64 | |||||||||||
| 65 | 0 | my $k = "listener-$ctag"; | |||||||||
| 66 | 0 | ▶ | if(exists $self->{cleanup}{$k}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | # This is bad, since we should never have the same ctag twice | ||||||||||
| 68 | 0 | die "Already had consumer tag $ctag"; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | $self->{cleanup}{$k} = sub { | ||||||||||
| 72 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 73 | 0 | Net::Async::AMQP::Queue->new( | |||||||||
| 74 | amqp => $self->amqp, | ||||||||||
| 75 | channel => $self, | ||||||||||
| 76 | future => Future->done() | ||||||||||
| 77 | )->cancel( | ||||||||||
| 78 | consumer_tag => $ctag | ||||||||||
| 79 | ) | ||||||||||
| 80 | 0 | }; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | =head2 _listener_stop | ||||||||||
| 84 | |||||||||||
| 85 | Called when a listener has stopped. This will remove the associated cleanup task. | ||||||||||
| 86 | |||||||||||
| 87 | =cut | ||||||||||
| 88 | |||||||||||
| 89 | sub _listener_stop { | ||||||||||
| 90 | 0 | ▶ | my ($self, $ev, $ctag) = @_; | ||||||||
|
Subroutine
_listener_stop: not called CC=1 SCAR=6.9
| |||||||||||
| 91 | 0 | delete $self->{cleanup}{"listener-$ctag"}; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | =head2 queue_declare | ||||||||||
| 95 | |||||||||||
| 96 | Override the usual queue declaration to ensure that we attach the wrapped channel | ||||||||||
| 97 | object (ourselves) rather than a raw L<Net::Async::AMQP::Channel> instance. | ||||||||||
| 98 | |||||||||||
| 99 | =cut | ||||||||||
| 100 | |||||||||||
| 101 | sub queue_declare { | ||||||||||
| 102 | 0 | ▶ | my ($self, %args) = @_; | ||||||||
|
Subroutine
queue_declare: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 103 | $self->channel->queue_declare(%args)->transform( | ||||||||||
| 104 | done => sub { | ||||||||||
| 105 | 0 | ▶ | my ($q) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 106 | # Ensure that this wrapped channel is used | ||||||||||
| 107 | # as the stored channel value. This means | ||||||||||
| 108 | # the channel holds the queue, we hold a weakref | ||||||||||
| 109 | # to the channel, and the queue holds a strong | ||||||||||
| 110 | # ref to our channel wrapper. | ||||||||||
| 111 | 0 | $q->configure(channel => $self); | |||||||||
| 112 | 0 | $q | |||||||||
| 113 | } | ||||||||||
| 114 | ) | ||||||||||
| 115 | 0 | } | |||||||||
| 116 | |||||||||||
| 117 | =head2 confirm_mode | ||||||||||
| 118 | |||||||||||
| 119 | Don't allow this. If we want a confirm-mode-channel, it has to be assigned by passing | ||||||||||
| 120 | the appropriate request to the connection manager. | ||||||||||
| 121 | |||||||||||
| 122 | Without this we run the risk of burning through channels, for example: | ||||||||||
| 123 | |||||||||||
| 124 | =over 4 | ||||||||||
| 125 | |||||||||||
| 126 | =item * Assign a channel with no options | ||||||||||
| 127 | |||||||||||
| 128 | =item * Enable confirm mode on that channel | ||||||||||
| 129 | |||||||||||
| 130 | =item * Release the channel | ||||||||||
| 131 | |||||||||||
| 132 | =item * Repeat | ||||||||||
| 133 | |||||||||||
| 134 | =back | ||||||||||
| 135 | |||||||||||
| 136 | This would eventually cause all available channels to end up in the "confirm mode" | ||||||||||
| 137 | available pool. | ||||||||||
| 138 | |||||||||||
| 139 | =cut | ||||||||||
| 140 | |||||||||||
| 141 | sub confirm_mode { | ||||||||||
| 142 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
confirm_mode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 143 | 0 | die "Cannot apply confirm mode to an existing channel"; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 0 | ▶ | sub last_call { shift->{last_call} } | ||||||||
|
Subroutine
last_call: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 147 | |||||||||||
| 148 | =head2 channel | ||||||||||
| 149 | |||||||||||
| 150 | Returns the underlying AMQP channel. | ||||||||||
| 151 | |||||||||||
| 152 | =cut | ||||||||||
| 153 | |||||||||||
| 154 | 0 | ▶ | sub channel { shift->{channel} } | ||||||||
|
Subroutine
channel: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 155 | |||||||||||
| 156 | =head2 manager | ||||||||||
| 157 | |||||||||||
| 158 | Returns our ConnectionManager instance. | ||||||||||
| 159 | |||||||||||
| 160 | =cut | ||||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | sub manager { shift->{manager} } | ||||||||
|
Subroutine
manager: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 163 | |||||||||||
| 164 | =head2 as_string | ||||||||||
| 165 | |||||||||||
| 166 | String representation of the channel object. | ||||||||||
| 167 | |||||||||||
| 168 | Takes the form "ManagedChannel[N]", where N is the ID. | ||||||||||
| 169 | |||||||||||
| 170 | =cut | ||||||||||
| 171 | |||||||||||
| 172 | sub as_string { | ||||||||||
| 173 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 174 | 0 | sprintf "ManagedChannel[%d]", $self->id; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | =head2 DESTROY | ||||||||||
| 178 | |||||||||||
| 179 | On destruction we release the channel by informing the connection manager | ||||||||||
| 180 | that we no longer require the data. | ||||||||||
| 181 | |||||||||||
| 182 | There may be some cleanup tasks required before we can release - cancelling | ||||||||||
| 183 | any trailing consumers, for example. These are held in the cleanup hash. | ||||||||||
| 184 | |||||||||||
| 185 | =cut | ||||||||||
| 186 | |||||||||||
| 187 | sub DESTROY { | ||||||||||
| 188 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
DESTROY: not called CC=4 SCAR=30.0
| |||||||||||
| 189 | 0 | ▶ | return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | |||||||||||
| 191 | 0 | ▶ | unless($self->{cleanup}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 192 | 0 | my $conman = delete $self->{manager}; | |||||||||
| 193 | 0 | my $ch = delete $self->{channel}; | |||||||||
| 194 | 0 | ▶ | return unless $conman; # global destruct | ||||||||
|
Branch: 1 branch
| |||||||||||
| 195 | 0 | return $conman->release_channel($ch); | |||||||||
| 196 | } | ||||||||||
| 197 | |||||||||||
| 198 | 0 | my $f; | |||||||||
| 199 | 0 | my $cleanup = delete $self->{cleanup}; | |||||||||
| 200 | $f = ( | ||||||||||
| 201 | fmap_void { | ||||||||||
| 202 | 0 | ▶ | my $k = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 203 | 0 | my $task = delete $cleanup->{$k}; | |||||||||
| 204 | 0 | ▶ | $task ? $task->($self) : Future->wrap | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | } foreach => [ | ||||||||||
| 206 | sort keys %$cleanup | ||||||||||
| 207 | ] | ||||||||||
| 208 | )->on_ready(sub { | ||||||||||
| 209 | 0 | ▶ | my $conman = delete $self->{manager}; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 210 | 0 | my $ch = delete $self->{channel}; | |||||||||
| 211 | 0 | ▶ | $conman->release_channel($ch) if $conman; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 212 | 0 | undef $f; | |||||||||
| 213 | 0 | }); | |||||||||
| 214 | } | ||||||||||
| 215 | |||||||||||
| 216 | =head2 AUTOLOAD | ||||||||||
| 217 | |||||||||||
| 218 | All other methods are proxied to the underlying L<Net::Async::AMQP::Channel>. | ||||||||||
| 219 | |||||||||||
| 220 | =cut | ||||||||||
| 221 | |||||||||||
| 222 | sub AUTOLOAD { | ||||||||||
| 223 | 0 | ▶ | my ($self, @args) = @_; | ||||||||
|
Subroutine
AUTOLOAD: not called CC=1 SCAR=3.5
| |||||||||||
| 224 | 0 | (my $method = our $AUTOLOAD) =~ s/.*:://; | |||||||||
| 225 | |||||||||||
| 226 | # We could check for existence first, but we wouldn't store the resulting coderef anyway, | ||||||||||
| 227 | # so might as well just allow the method call to fail normally (we have no idea what | ||||||||||
| 228 | # subclasses are used for $self->channel, using the first one we find would not be very nice) | ||||||||||
| 229 | # die "attempt to proxy unknown method $method for $self" unless $self->channel->can($method); | ||||||||||
| 230 | |||||||||||
| 231 | my $code = sub { | ||||||||||
| 232 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 233 | 0 | $self->{last_call} = Time::HiRes::time; | |||||||||
| 234 | 0 | $self->channel->$method(@_); | |||||||||
| 235 | 0 | }; | |||||||||
| 236 | 7 | ▶ | { no strict 'refs'; *$method = $code } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||||||||||
| 237 | 0 | $code->(@_) | |||||||||
| 238 | } | ||||||||||
| 239 | |||||||||||
| 240 | 7 | 1; | |||||||||
| 241 | |||||||||||
| 242 | __END__ | ||||||||||