lib/POE/Component/MessageQueue/Storage/Throttled.pm

filter: Statementstmt 100% 55 / 55 Branchbran 83.3% 10 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 3 totaltotal 92.1% 82 / 89 CC 9 SCAR 7.6
CC
9
Cov
96%
__ANON__
4.2
_shutdown_throttle_check
3.0
__ANON__
2.0
CRAP 2.1
« lib/POE/Component/MessageQueue/Subscription.pm ↑ Summary lib/POE/Component/MessageQueue/Storage.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::Storage::Throttled;
19 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 13 with qw(POE::Component::MessageQueue::Storage::Double);
23  
24 13 has throttle_max => (
25 is => 'ro',
26 isa => 'Int',
27 default => 2,
28 );
29  
30 13 has sent => (
31 is => 'ro',
32 isa => 'Num',
33 default => 0,
34 traits => ['Counter'],
35 handles => {
36 'inc_sent' => 'inc',
37 'dec_sent' => 'dec',
38 'reset_sent' => 'reset',
39 }
40 );
41  
42 13 has throttle_count => (
43 is => 'ro',
44 isa => 'Num',
45 default => 0,
46 traits => ['Counter'],
47 handles => {
48 'inc_throttle_count' => 'inc',
49 'dec_throttle_count' => 'dec',
50 'reset_throttle_count' => 'reset',
51 }
52 );
53  
54 13 has shutdown_callback => (
55 is => 'rw',
56 isa => 'CodeRef',
57 clearer => 'stop_shutdown',
58 predicate => 'shutting_down',
59 );
60  
61 sub BUILD
62 {
63 3 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
64 3 $self->children({THROTTLED => $self->front, STORAGE => $self->back});
65 3 $self->add_names('THROTTLED');
66 }
67  
68 sub _backstore_ready
69 {
70 389 ▶ my $self = $_[0];
Subroutine _backstore_ready: called CC=1 SCAR=0.0
71  
72 # Send the next throttled message off to the backing store.
73 $self->front->get_oldest(sub {
74 389 ▶ if (my $msg = $_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $_[0]) { } 186 203 389
75 {
76 186 my $id = $msg->id;
77  
78 186 $self->dec_throttle_count;
79 186 my $tc = $self->throttle_count;
80 186 $self->log(info => "Sending throttled message $id ($tc left)");
81  
82 186 $self->delete_front($id);
83 $self->front->remove($id, sub {
84 $self->back->store($msg, sub {
85 186 ▶ @_ = ($self);
Subroutine __ANON__: called CC=1 SCAR=0.0
86 186 goto &_backstore_ready;
87 186 ▶ });
Subroutine __ANON__: called CC=1 SCAR=0.0
88 186 });
89 }
90 else
91 {
92 203 $self->dec_sent();
93 203 $self->_shutdown_throttle_check();
94 }
95 389 });
96 }
97  
98 before remove => sub {
99 32 ▶ my ($self, $ids) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.5
100 32 ▶ $ids = [$ids] unless (ref $ids eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ids eq "ARRAY" 0 32 32
101 32 foreach my $id (@$ids)
102 {
103 35 ▶ $self->dec_throttle_count if $self->in_front($id);
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_front($id) 0 35 35
104 }
105 13 };
106  
107 sub store
108 {
109 389 ▶ my ($self, $message, $callback) = @_;
Subroutine store: called CC=2 SCAR=6.9
Pod undocumented
110 389 ▶ if ($self->sent < $self->throttle_max)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sent < $self->throttle_max) { } 203 186 389
111 {
112 203 $self->inc_sent();
113 $self->back->store($message, sub {
114 # Do not tail call: the message is stored, but we want to do things
115 # after we satisfy the callback.
116 203 ▶ $callback->() if $callback;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $callback 201 2 203
117 203 $self->_backstore_ready();
118 203 });
119 }
120 else
121 {
122 186 $self->set_front($message->id, {persisted => 0});
123 186 $self->front->store($message, $callback);
124 186 $self->inc_throttle_count;
125 186 my $tc = $self->throttle_count;
126  
127 186 $self->log(debug => "$tc messages throttled");
128 }
129 }
130  
131 sub _shutdown_throttle_check
132 {
133 206 ▶ my $self = shift;
Subroutine _shutdown_throttle_check: called CC=3 SCAR=11.0
134 206 ▶ if ($self->shutting_down && $self->throttle_count == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->shutting_down and $self->throttle_count == 0) 3 203 206
Condition66%2/3
$self->shutting_down and $self->throttle_count == 0
!ll&&!rl&&r
20303
MC/DC50%1/2
$self->shutting_down and $self->throttle_count == 0
$self->shutting_down $self->throttle_count == 0
Truth table66%2/3
A and B
A= $self->shutting_downB= $self->throttle_count == 0
ABresult
0X0
100
111
135 {
136 # We have now finished sending things out of throttled, so -WE- are done.
137 # However, we'll still get message_storeds as our backstore finishes, and
138 # we don't want to continue calling shutdown_callback.
139 3 my $callback = $self->shutdown_callback;
140 3 $self->stop_shutdown();
141 3 goto $callback;
142 }
143 }
144  
145 sub storage_shutdown
146 {
147 3 ▶ my ($self, $complete) = @_;
Subroutine storage_shutdown: called CC=1 SCAR=0.0
Pod undocumented
148 $self->shutdown_callback(sub {
149 $self->front->storage_shutdown(sub {
150 3 ▶ $self->back->storage_shutdown($complete);
Subroutine __ANON__: called CC=1 SCAR=0.0
151 3 ▶ });
Subroutine __ANON__: called CC=1 SCAR=0.0
152 3 });
153  
154 3 $self->_shutdown_throttle_check();
155 }
156  
157 13 1;
158  
159 __END__
160  
161 =pod
162  
163 =head1 NAME
164  
165 POE::Component::MessageQueue::Storage::Throttled -- Wraps around another storage engine to throttle the number of messages sent to be stored at one time.
166  
167 =head1 SYNOPSIS
168  
169 use POE;
170 use POE::Component::MessageQueue;
171 use POE::Component::MessageQueue::Storage::Throttled;
172 use POE::Component::MessageQueue::Storage::DBI;
173 use strict;
174  
175 my $DATA_DIR = '/tmp/perl_mq';
176  
177 POE::Component::MessageQueue->new({
178 storage => POE::Component::MessageQueue::Storage::Throttled->new({
179 storage => POE::Component::MessageQueue::Storage::DBI->new({
180 dsn => $DB_DSN,
181 username => $DB_USERNAME,
182 password => $DB_PASSWORD,
183 }),
184 throttle_max => 2
185 }),
186 });
187  
188 POE::Kernel->run();
189 exit;
190  
191 =head1 DESCRIPTION
192  
193 Wraps around another engine to limit the number of messages sent to be stored at once.
194  
195 Use of this module is B<highly> recommend!
196  
197 If the storage engine is unable to store the messages fast enough (ie. with slow disk IO) it can get really backed up and stall messages coming out of the queue. This allows a client producing execessive amounts of messages to basically monopolize the server, preventing any messages from getting distributed to subscribers.
198  
199 It is suggested to keep the throttle_max very low. In an ideal situation, the underlying storage engine would be able to write each message immediately. This means that there will never be more than one message sent to be stored at a time. The purpose of this module is make the message act as though this were the case even if it isn't. So, a throttle_max of 1, will strictly enforce this, however, for a little bit of leniancy, the suggested default is 2.
200  
201 =head1 CONSTRUCTOR PARAMETERS
202  
203 =over 2
204  
205 =item storage => L<POE::Component::MessageQueue::Storage>
206  
207 The storage engine to wrap.
208  
209 =item throttle_max => SCALAR
210  
211 The max number of messages that can be sent to the DBI store at one time.
212  
213 =back
214  
215 =head1 SUPPORTED STOMP HEADERS
216  
217 Ignored. Passed through to the wrapped storage engine.
218  
219 =head1 SEE ALSO
220  
221 L<POE::Component::MessageQueue>,
222 L<POE::Component::MessageQueue::Storage>,
223 L<POE::Component::MessageQueue::Storage::Double>
224  
225 I<Other storage engines:>
226  
227 L<POE::Component::MessageQueue::Storage::Memory>,
228 L<POE::Component::MessageQueue::Storage::BigMemory>,
229 L<POE::Component::MessageQueue::Storage::FileSystem>,
230 L<POE::Component::MessageQueue::Storage::DBI>,
231 L<POE::Component::MessageQueue::Storage::Generic>,
232 L<POE::Component::MessageQueue::Storage::Generic::DBI>,
233 L<POE::Component::MessageQueue::Storage::Complex>,
234 L<POE::Component::MessageQueue::Storage::Default>
235  
236 =cut
237  
« lib/POE/Component/MessageQueue/Subscription.pm ↑ Summary lib/POE/Component/MessageQueue/Storage.pm »