« lib/POE/Component/MessageQueue/Storage/Remote.pm
↑ Summary
lib/POE/Component/MessageQueue/Topic.pm »
| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright 2007-2010 David Snopek <dsnopek@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 5 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 6 | # the Free Software Foundation, either version 2 of the License, or | ||||||||||||||||||||||||||||
| 7 | # (at your option) any later version. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 12 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package POE::Component::MessageQueue::Queue; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 16 | ▶ | use POE; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 16 | ▶ | use POE::Session; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 16 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 16 | with qw(POE::Component::MessageQueue::Destination); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 48 | ▶ | sub flag { has $_[0] => (is => 'rw', default => 0) } | ||||||||||||||||||||||||||
|
Subroutine
flag: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 16 | flag 'pumping'; | |||||||||||||||||||||||||||
| 28 | 16 | flag 'pump_pending'; | |||||||||||||||||||||||||||
| 29 | 16 | flag 'shutting_down'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub stash { | ||||||||||||||||||||||||||||
| 32 | 32 | ▶ | my $n = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
stash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | has $n => ( | ||||||||||||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||||||||||
| 35 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 36 | 8 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 32 | traits => ['Hash'], | |||||||||||||||||||||||||||
| 38 | handles => { | ||||||||||||||||||||||||||||
| 39 | "set_$n" => 'set', | ||||||||||||||||||||||||||||
| 40 | "del_$n" => 'delete', | ||||||||||||||||||||||||||||
| 41 | "${n}_keys" => 'keys', | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 16 | stash 'waiting'; | |||||||||||||||||||||||||||
| 47 | 16 | stash 'serviced'; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub next_subscriber | ||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 932 | ▶ | my $self = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
next_subscriber: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 932 | ▶ | if (my @keys = $self->waiting_keys) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | 405 | my $id = pop(@keys); | |||||||||||||||||||||||||||
| 55 | 405 | my $s = $self->del_waiting($id); | |||||||||||||||||||||||||||
| 56 | 405 | ▶ | if($s->client) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | 405 | $self->set_serviced($id, $s); | |||||||||||||||||||||||||||
| 59 | 405 | return $s; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else | ||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | $self->delete_subscription($id); | |||||||||||||||||||||||||||
| 64 | 0 | return $self->next_subscriber; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else | ||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||
| 69 | 527 | my $serviced = $self->serviced; | |||||||||||||||||||||||||||
| 70 | 527 | $self->serviced($self->waiting); | |||||||||||||||||||||||||||
| 71 | 527 | $self->waiting($serviced); | |||||||||||||||||||||||||||
| 72 | 527 | ▶ | return (keys %$serviced) && $self->next_subscriber; | ||||||||||||||||||||||||||
|
Condition66%2/3
keys %$serviced && $self->next_subscriber
MC/DC50%1/2
keys %$serviced && $self->next_subscriber
keys %$serviced
$self->next_subscriber
Truth table66%2/3
A && B
A= keys %$servicedB= $self->next_subscriber
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub next_ready | ||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 414 | ▶ | my $self = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
next_ready: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 414 | my ($s, %seen); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 414 | ▶ | while ($s = $self->next_subscriber and !exists $seen{$s}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$s = $self->next_subscriber and not exists $seen{$s}
MC/DC100%2/2
$s = $self->next_subscriber and not exists $seen{$s}
$s = $self->next_subscriber
not exists $seen{$s}
Truth table100%3/3
A and not(B)
A= $s = $self->next_subscriberB= not exists $seen{$s}
| |||||||||||||||||||||||||||||
| 82 | { | ||||||||||||||||||||||||||||
| 83 | 271 | ▶ | return $s if $s->ready; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 134 | $seen{$s} = 1; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 277 | return; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 16 | after set_subscription => __PACKAGE__->can('set_waiting'); | |||||||||||||||||||||||||||
| 90 | after delete_subscription => sub { | ||||||||||||||||||||||||||||
| 91 | 6 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 6 | $self->del_waiting(@args); | |||||||||||||||||||||||||||
| 93 | 6 | $self->del_serviced(@args); | |||||||||||||||||||||||||||
| 94 | 16 | }; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 16 | __PACKAGE__->meta->make_immutable(); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub BUILD | ||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||
| 100 | 4 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | 4 | POE::Session->create( | |||||||||||||||||||||||||||
| 102 | object_states => [ $self => [qw(_start _shutdown _pump_state _pump_timer)]], | ||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub _start | ||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||
| 108 | 4 | ▶ | my ($self, $kernel) = @_[OBJECT, KERNEL]; | ||||||||||||||||||||||||||
|
Subroutine
_start: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 109 | 4 | $kernel->alias_set($self->name); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 4 | ▶ | $kernel->delay(_pump_timer => $self->parent->pump_frequency) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | if ($self->parent->pump_frequency); | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub shutdown { | ||||||||||||||||||||||||||||
| 116 | 4 | ▶ | my $self = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
shutdown: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | 4 | $self->shutting_down(1); | |||||||||||||||||||||||||||
| 118 | 4 | $poe_kernel->post($self->name, '_shutdown') | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub _shutdown | ||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||
| 123 | 4 | ▶ | my ($self, $kernel) = @_[OBJECT, KERNEL]; | ||||||||||||||||||||||||||
|
Subroutine
_shutdown: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 124 | 4 | $kernel->alias_remove($self->name); | |||||||||||||||||||||||||||
| 125 | 4 | $kernel->alarm_remove_all(); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # This is the pumping philosophy: When we receive a pump request, we will | ||||||||||||||||||||||||||||
| 129 | # give everyone a chance to claim a message. If any pumps are asked for while | ||||||||||||||||||||||||||||
| 130 | # this is happening, we will remember and do another pump when this one is | ||||||||||||||||||||||||||||
| 131 | # finished (just one). | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # This means we're serializing claim and retrieve requests. More work needs | ||||||||||||||||||||||||||||
| 134 | # to be done to determine whether this is good or necessary. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub is_persistent { return 1 } | ||||||||||||||||||||||||||
|
Subroutine
is_persistent: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub _pump_state | ||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | 264 | ▶ | my $self = $_[OBJECT]; | ||||||||||||||||||||||||||
|
Subroutine
_pump_state: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 141 | 264 | ▶ | return if $self->shutting_down; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 264 | ▶ | if (my $s = $self->next_ready) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 105 | $s->ready(0); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | $self->storage->claim_and_retrieve($self->name, $s->client->id, sub { | ||||||||||||||||||||||||||||
| 148 | 105 | ▶ | if (my $msg = $_[0]) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | 98 | $self->dispatch_message($msg, $s); | |||||||||||||||||||||||||||
| 151 | 98 | $poe_kernel->post($self->name, '_pump_state'); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | else | ||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 7 | $s->ready(1); | |||||||||||||||||||||||||||
| 156 | 7 | $self->_done_pumping(); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 105 | }); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else | ||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||
| 162 | 159 | $self->_done_pumping(); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | sub _done_pumping | ||||||||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||||||||
| 168 | 166 | ▶ | my $self = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
_done_pumping: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 169 | 166 | $self->pumping(0); | |||||||||||||||||||||||||||
| 170 | 166 | ▶ | $self->pump() if $self->pump_pending; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub _pump_timer | ||||||||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | my ($self, $kernel) = @_[OBJECT, KERNEL]; | ||||||||||||||||||||||||||
|
Subroutine
_pump_timer: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | return if $self->shutting_down; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # pump (the 1 means that we won't set pump_pending if | ||||||||||||||||||||||||||||
| 179 | # we are already in a pumping state). | ||||||||||||||||||||||||||||
| 180 | 0 | $self->pump(1); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | $kernel->delay(_pump_timer => $self->parent->pump_frequency); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | sub pump | ||||||||||||||||||||||||||||
| 186 | { | ||||||||||||||||||||||||||||
| 187 | 230 | ▶ | my ($self, $skip_pending) = @_; | ||||||||||||||||||||||||||
|
Subroutine
pump: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 230 | ▶ | if($self->pumping and not $skip_pending) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->pumping and not $skip_pending
MC/DC50%1/2
$self->pumping and not $skip_pending
$self->pumping
not $skip_pending
Truth table66%2/3
A and not(B)
A= $self->pumpingB= not $skip_pending
| |||||||||||||||||||||||||||||
| 190 | { | ||||||||||||||||||||||||||||
| 191 | 61 | $self->pump_pending(1); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | else | ||||||||||||||||||||||||||||
| 194 | { | ||||||||||||||||||||||||||||
| 195 | 169 | $self->log(debug => ' -- PUMP QUEUE: '.$self->name.' -- '); | |||||||||||||||||||||||||||
| 196 | 169 | $self->notify('pump'); | |||||||||||||||||||||||||||
| 197 | 169 | $self->pump_pending(0); | |||||||||||||||||||||||||||
| 198 | 169 | $self->pumping(1); | |||||||||||||||||||||||||||
| 199 | 169 | $poe_kernel->call($self->name, '_pump_state'); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | sub send | ||||||||||||||||||||||||||||
| 204 | { | ||||||||||||||||||||||||||||
| 205 | 150 | ▶ | my ($self, $message) = @_; | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 206 | 150 | ▶ | return if $self->shutting_down; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | # If we already have a ready subscriber, we'll dispatch before we | ||||||||||||||||||||||||||||
| 209 | # store to give the subscriber a headstart on processing. | ||||||||||||||||||||||||||||
| 210 | 150 | ▶ | if (not $message->has_delay and my $s = $self->next_ready) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $message->has_delay and my $s = $self->next_ready
MC/DC50%1/2
not $message->has_delay and my $s = $self->next_ready
not $message->has_delay
my $s = $self->next_ready
Truth table66%2/3
not(A) and B
A= not $message->has_delayB= my $s = $self->next_ready
| |||||||||||||||||||||||||||||
| 211 | { | ||||||||||||||||||||||||||||
| 212 | 32 | my $mid = $message->id; | |||||||||||||||||||||||||||
| 213 | 32 | my $cid = $s->client->id; | |||||||||||||||||||||||||||
| 214 | 32 | ▶ | if ($s->client_ack) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | { | ||||||||||||||||||||||||||||
| 216 | 2 | $message->claim($cid); | |||||||||||||||||||||||||||
| 217 | 2 | $self->log(info => "QUEUE: Message $mid claimed by $cid during send"); | |||||||||||||||||||||||||||
| 218 | 2 | $self->storage->store($message); | |||||||||||||||||||||||||||
| 219 | 2 | $self->notify(store => $message); | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | else | ||||||||||||||||||||||||||||
| 222 | { | ||||||||||||||||||||||||||||
| 223 | 30 | $self->log(info => "QUEUE: Message $mid not stored, sent to $cid"); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | 32 | $self->dispatch_message($message, $s); | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | else | ||||||||||||||||||||||||||||
| 228 | { | ||||||||||||||||||||||||||||
| 229 | $self->storage->store($message, sub { | ||||||||||||||||||||||||||||
| 230 | 118 | ▶ | $self->notify(store => $message); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 231 | 118 | $self->pump(); | |||||||||||||||||||||||||||
| 232 | 118 | }); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 16 | 1; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||