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