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