| 1 | package Object::RateLimiter; | ||||||||||||||||||||||||||||
| 2 | 2 | $Object::RateLimiter::VERSION = '1.003001'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp 'confess'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use List::Objects::WithUtils 'array'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Time::HiRes 'time'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | use overload | ||||||||||||||||||||||||||||
| 13 | '&{}' => sub { | ||||||||||||||||||||||||||||
| 14 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub { $self->delay } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | }, | |||||||||||||||||||||||||||
| 17 | 2 | ▶ | fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | use Object::ArrayType::New | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | [ events => '', seconds => 'SECS', _queue => 'QUEUE' ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 23 | ▶ | sub seconds { $_[0]->[SECS] } | ||||||||||||||||||||||||||
|
Subroutine
seconds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 24 | ▶ | sub events { $_[0]->[EVENTS] } | ||||||||||||||||||||||||||
|
Subroutine
events: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 22 | ▶ | sub _queue { $_[0]->[QUEUE] } | ||||||||||||||||||||||||||
|
Subroutine
_queue: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use Class::Method::Modifiers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | around new => sub { | ||||||||||||||||||||||||||||
| 27 | 9 | ▶ | my ($orig, $class) = splice @_, 0, 2; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 28 | 9 | my $self = $class->$orig(@_); | |||||||||||||||||||||||||||
| 29 | 9 | ▶ | confess "Constructor requires 'seconds =>' and 'events =>' parameters" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->seconds and defined $self->events
MC/DC100%2/2
defined $self->seconds and defined $self->events
defined $self->seconds
defined $self->events
Truth table100%3/3
A and B
A= defined $self->secondsB= defined $self->events
| |||||||||||||||||||||||||||||
| 30 | unless defined $self->seconds and defined $self->events; | ||||||||||||||||||||||||||||
| 31 | 6 | ▶ | $self->[QUEUE] = array(@{ $self->_queue }) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->_queue and ref $self->_queue eq "ARRAY"
MC/DC100%2/2
$self->_queue and ref $self->_queue eq "ARRAY"
$self->_queue
ref $self->_queue eq "ARRAY"
Truth table100%3/3
A and B
A= $self->_queueB= ref $self->_queue eq "ARRAY"
| |||||||||||||||||||||||||||||
| 32 | if $self->_queue and ref $self->_queue eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 33 | 6 | $self | |||||||||||||||||||||||||||
| 34 | 2 | }; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub clone { | ||||||||||||||||||||||||||||
| 37 | 2 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | $params{events} = $self->events unless defined $params{events}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | $params{seconds} = $self->seconds unless defined $params{seconds}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | $params{_queue} = $self->_queue if $self->_queue; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 2 | $self->new(%params); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub export { | ||||||||||||||||||||||||||||
| 45 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
export: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | +{ | ||||||||||||||||||||||||||||
| 47 | 1 | events => $self->events, | |||||||||||||||||||||||||||
| 48 | seconds => $self->seconds, | ||||||||||||||||||||||||||||
| 49 | _queue => $self->_queue->unbless, | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub delay { | ||||||||||||||||||||||||||||
| 54 | 13 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delay: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 13 | ▶ | my $thisq = $self->[QUEUE] ||= array; | ||||||||||||||||||||||||||
|
Condition66%2/3
$$self[2] ||= array()
MC/DC0%0/2
$$self[2] ||= array()
$$self[2]
array()
Truth table66%2/3
A ||= B
A= $$self[2]B= array()
| |||||||||||||||||||||||||||||
| 56 | 13 | my $ev_limit = $self->events; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 13 | ▶ | if ((my $ev_count = $thisq->count) >= $ev_limit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 4 | my $oldest_ts = $thisq->get(0); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | my $delayed = ( | |||||||||||||||||||||||||||
| 62 | $oldest_ts | ||||||||||||||||||||||||||||
| 63 | + ( $ev_count * $self->seconds / $ev_limit ) | ||||||||||||||||||||||||||||
| 64 | ) - time; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | $delayed > 0 ? return($delayed) : $thisq->shift | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 9 | $thisq->push( time ); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 9 | 0 | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 3 | ▶ | sub clear { $_[0]->[QUEUE] = undef; 1 } | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub expire { | ||||||||||||||||||||||||||||
| 78 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expire: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | return unless $self->is_expired; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | $self->clear | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub is_expired { | ||||||||||||||||||||||||||||
| 84 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_expired: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 4 | ▶ | my $thisq = $self->_queue || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_queue || (return)
MC/DC0%0/1
$self->_queue || (return)
$self->_queue
Truth table50%1/2
A
A= $self->_queue
| |||||||||||||||||||||||||||||
| 86 | 4 | ▶ | my $latest = $thisq->get(-1) || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
$thisq->get(-1) || (return)
MC/DC0%0/1
$thisq->get(-1) || (return)
$thisq->get(-1)
Truth table50%1/2
A
A= $thisq->get(-1)
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | time - $latest > $self->seconds | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | ▶ | |||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | qq[<avenj> it's not\n], | ||||||||||||||||||||||||||||
| 93 | qq[<JCW> What's not what?\n], | ||||||||||||||||||||||||||||
| 94 | qq[<Capn_Refsmmat> I always thought that\n], | ||||||||||||||||||||||||||||
| 95 | qq[<JCW> Thought what? :o\n], | ||||||||||||||||||||||||||||
| 96 | qq[<Capn_Refsmmat> well, I've always had this vague feeling of\n], | ||||||||||||||||||||||||||||
| 97 | qq[<JCW> Heh, you sound like seuss.\n], | ||||||||||||||||||||||||||||
| 98 | qq[<Capn_Refsmmat> I'm very by your remark\n] | ||||||||||||||||||||||||||||
| 99 | 2 | unless caller; 1; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =pod | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =for Pod::Coverage EVENTS QUEUE SECS | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 NAME | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Object::RateLimiter - A flood control (rate limiter) object | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | use Object::RateLimiter; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | my $ctrl = Object::RateLimiter->new( | ||||||||||||||||||||||||||||
| 114 | events => 3, | ||||||||||||||||||||||||||||
| 115 | seconds => 5 | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Run some subs, as a contrived example; | ||||||||||||||||||||||||||||
| 119 | # no more than 3 in 5 seconds, per our constructor above: | ||||||||||||||||||||||||||||
| 120 | my @work = ( | ||||||||||||||||||||||||||||
| 121 | sub { "foo" }, sub { "bar" }, | ||||||||||||||||||||||||||||
| 122 | sub { "baz" }, sub { "cake" }, | ||||||||||||||||||||||||||||
| 123 | # ... | ||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | while (my $some_item = shift @work) { | ||||||||||||||||||||||||||||
| 127 | if (my $delay = $ctrl->delay) { | ||||||||||||||||||||||||||||
| 128 | # Delayed $delay (fractional) seconds. | ||||||||||||||||||||||||||||
| 129 | # (You might want Time::HiRes::sleep, or yield to event loop, etc) | ||||||||||||||||||||||||||||
| 130 | sleep $delay | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | print $some_item->() | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # Clear the event history if it's stale: | ||||||||||||||||||||||||||||
| 136 | $ctrl->expire; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # Clear the event history unconditionally: | ||||||||||||||||||||||||||||
| 139 | $ctrl->clear; | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Same as calling ->delay: | ||||||||||||||||||||||||||||
| 142 | my $delayed = $ctrl->(); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | This is a generic rate-limiter object, implementing the math described in | ||||||||||||||||||||||||||||
| 147 | L<http://www.perl.com/pub/2004/11/11/floodcontrol.html> via light-weight | ||||||||||||||||||||||||||||
| 148 | array-type objects. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | The algorithm is fairly simple; the article linked above contains an in-depth | ||||||||||||||||||||||||||||
| 151 | discussion by Vladi Belperchinov-Shabanski (CPAN: | ||||||||||||||||||||||||||||
| 152 | L<http://www.metacpan.org/author/CADE>): | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | $delay = | ||||||||||||||||||||||||||||
| 155 | ( | ||||||||||||||||||||||||||||
| 156 | $oldest_timestamp + | ||||||||||||||||||||||||||||
| 157 | ( $seen_events * $limit_secs / $event_limit ) | ||||||||||||||||||||||||||||
| 158 | ) | ||||||||||||||||||||||||||||
| 159 | - time() | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | This module uses L<Time::HiRes> to provide support for fractional seconds. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | See L<Algorithm::FloodControl> for a similar module with a functional | ||||||||||||||||||||||||||||
| 164 | interface & persistent on-disk storage features (for use with CGI | ||||||||||||||||||||||||||||
| 165 | applications). | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 new | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | my $ctrl = Object::RateLimiter->new( | ||||||||||||||||||||||||||||
| 170 | events => 3, | ||||||||||||||||||||||||||||
| 171 | seconds => 5 | ||||||||||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Constructs a new rate-limiter with a clean event history. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 clear | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | $ctrl->clear; | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Clear the event history. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 clone | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | my $new_ctrl = $ctrl->clone( events => 4 ); | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Clones an existing rate-limiter; new options can be provided, overriding | ||||||||||||||||||||||||||||
| 187 | previous settings. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | The new limiter contains a clone of the event history; the old rate-limiter is | ||||||||||||||||||||||||||||
| 190 | left untouched. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head2 export | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | my $opts = $ctrl->export; | ||||||||||||||||||||||||||||
| 195 | # ... later, perhaps after storing/retrieving ... | ||||||||||||||||||||||||||||
| 196 | my $recreated = Object::RateLimiter->new(%$opts); | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Exports the current state of the rate-limiter as a reference to a hash; the | ||||||||||||||||||||||||||||
| 199 | exported hash can be fed to L</new> to recreate the rate-limiter. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | This is useful if the rate-limiter's state must be stored persistently. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 delay | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | if (my $delay = $ctrl->delay) { | ||||||||||||||||||||||||||||
| 206 | sleep $delay; # ... or do something else | ||||||||||||||||||||||||||||
| 207 | } else { | ||||||||||||||||||||||||||||
| 208 | # Not delayed. | ||||||||||||||||||||||||||||
| 209 | do_work; | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # Same as calling ->delay: | ||||||||||||||||||||||||||||
| 213 | my $delay = $ctrl->(); | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | The C<delay()> method determines if some work can be done now, or should wait. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | When called, event timestamps are considered; if we have exceeded our limit, | ||||||||||||||||||||||||||||
| 218 | the delay in (possibly fractional) seconds until the event would be | ||||||||||||||||||||||||||||
| 219 | allowed is returned. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | A return value of 0 indicates that the event does not need to wait. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head2 events | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Returns the B<events> limit the object was constructed with. | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head2 expire | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | $ctrl->expire; | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Clears the event history if L</is_expired> is true. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Returns true if L</clear> was called. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | (You're not required to call C<expire()>, but it can be useful to save a | ||||||||||||||||||||||||||||
| 236 | little memory.) | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =head2 is_expired | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | Returns true if the last seen event is outside of our time window (in other | ||||||||||||||||||||||||||||
| 241 | words, the event history is stale) or there is no event history. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Also see L</expire> | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head2 seconds | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Returns the B<seconds> limit the object was constructed with. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Jon Portnoy <avenj@cobaltirc.org> | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Based on the math from L<Algorithm::FloodControl> as described in an article | ||||||||||||||||||||||||||||
| 254 | written by the author: | ||||||||||||||||||||||||||||
| 255 | L<http://www.perl.com/pub/2004/11/11/floodcontrol.html> | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Licensed under the same terms as Perl. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =cut | ||||||||||||||||||||||||||||