| 1 | # $Id: Throttle.pm,v 1.4 2010/03/12 22:10:11 dk Exp $ | ||||||||||||||||||||||||||||
| 2 | package IO::Lambda::Throttle; | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use IO::Lambda qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use IO::Lambda::Mutex qw(mutex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | our $DEBUG = $IO::Lambda::DEBUG{throttle} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$IO::Lambda::DEBUG{'throttle'} || 0
MC/DC0%0/1
$IO::Lambda::DEBUG{'throttle'} || 0
$IO::Lambda::DEBUG{'throttle'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'throttle'}
| |||||||||||||||||||||||||||||
| 11 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 12 | 1 | our @EXPORT_OK = qw(throttle); | |||||||||||||||||||||||||||
| 13 | 1 | our %EXPORT_TAGS = ( all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub new | ||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||
| 17 | 0 | ▶ | my ($class, $rate, $strict) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | my $self = bless { | ||||||||||||||||||||||||||
|
Condition0%0/2
$strict || 0
MC/DC0%0/1
$strict || 0
$strict
Truth table0%0/2
A
A= $strict
| |||||||||||||||||||||||||||||
| 19 | mutex => IO::Lambda::Mutex-> new, | ||||||||||||||||||||||||||||
| 20 | last => 0, | ||||||||||||||||||||||||||||
| 21 | low => 0, | ||||||||||||||||||||||||||||
| 22 | high => 0, | ||||||||||||||||||||||||||||
| 23 | strict => $strict || 0, | ||||||||||||||||||||||||||||
| 24 | }, $class; | ||||||||||||||||||||||||||||
| 25 | 0 | $self-> rate($rate); | |||||||||||||||||||||||||||
| 26 | 0 | return $self; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub rate | ||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | return $_[0]-> {rate} unless $#_; | ||||||||||||||||||||||||||
|
Subroutine
rate: not called CC=4 SCAR=30.0
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | my ( $self, $rate) = @_; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | die "negative rate" if defined($rate) and $rate < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $rate and $rate < 0
MC/DC0%0/2
defined $rate and $rate < 0
defined $rate
$rate < 0
Truth table0%0/3
A and B
A= defined $rateB= $rate < 0
| |||||||||||||||||||||||||||||
| 34 | 0 | $self-> {rate} = $rate; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub strict { $#_ ? $_[0]-> {strict} = $_[1] : $_[0]-> {strict} } | ||||||||||||||||||||||||||
|
Subroutine
strict: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # warning: when called, changes internal state of an object | ||||||||||||||||||||||||||||
| 40 | # returns 0 if rate limitter thinks it's ok to run now, | ||||||||||||||||||||||||||||
| 41 | # otherwise returns number of seconds needed to sleep | ||||||||||||||||||||||||||||
| 42 | sub next_timeout | ||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
next_timeout: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | unless ( $self-> {rate}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | # special case | ||||||||||||||||||||||||||||
| 47 | 0 | return 0; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | my $ts = time; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ( $ts < $self-> {last}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | # warn "negative time detected\n"; | ||||||||||||||||||||||||||||
| 53 | 0 | my $delta = $self-> {last} - $ts; | |||||||||||||||||||||||||||
| 54 | 0 | $self-> {low} -= $delta; | |||||||||||||||||||||||||||
| 55 | 0 | $self-> {high} -= $delta; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 0 | $self-> {last} = $ts; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | warn "$ts: $self->{low}/$self->{high}\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( $self-> {low} < $self-> {high}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | 0 | $self-> {low} += 1 / $self-> {rate}; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | warn "case1\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | return 0; | |||||||||||||||||||||||||||
| 64 | } elsif ( $self-> {low} < $ts) { | ||||||||||||||||||||||||||||
| 65 | 0 | $self-> {low} = $ts + 1 / $self-> {rate}; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | $self-> {high} = $ts + ($self->{strict} ? 1 / $self-> {rate} : 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | warn "case2\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | return 0; | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | warn "wait ", $self->{low}-$ts, "\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | return $self-> {low} - $ts; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Returns a lambda that finishes until rate-limitter allows further run. | ||||||||||||||||||||||||||||
| 76 | sub lock | ||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
lock: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 0 | weaken $self; | |||||||||||||||||||||||||||
| 80 | return $self-> {mutex}-> pipeline( | ||||||||||||||||||||||||||||
| 81 | lambda { | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $timeout = $self-> next_timeout; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return unless $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | context $timeout; | |||||||||||||||||||||||||||
| 85 | timeout { | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | die "something wrong, non-zero timeout" | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | if $self-> next_timeout; | ||||||||||||||||||||||||||||
| 88 | 0 | return; | |||||||||||||||||||||||||||
| 89 | 0 | }; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # returns a lambda that is finished when all lambdas, one by one, | ||||||||||||||||||||||||||||
| 95 | # are passed through a rate limitter | ||||||||||||||||||||||||||||
| 96 | sub ratelimit | ||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
ratelimit: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 0 | my @ret; | |||||||||||||||||||||||||||
| 100 | return lambda { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my @lambdas = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return unless @lambdas; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | context $self-> lock; | |||||||||||||||||||||||||||
| 104 | tail { | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | context shift @lambdas; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | tail { | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | push @ret, @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( @lambdas) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | this-> call(@lambdas)-> start; | |||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 0 | my @r = @ret; | |||||||||||||||||||||||||||
| 112 | 0 | @ret = (); | |||||||||||||||||||||||||||
| 113 | 0 | return @r; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | }} | |||||||||||||||||||||||||||
| 116 | 0 | }; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub throttle { __PACKAGE__-> new(@_)-> ratelimit } | ||||||||||||||||||||||||||
|
Subroutine
throttle: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =pod | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 NAME | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | IO::Lambda::Throttle - rate-limiting facility | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Provides several interfaces for throttling control flow by imposing rate limit. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 SYNPOSIS | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | use IO::Lambda qw(:lambda); | ||||||||||||||||||||||||||||
| 137 | use IO::Lambda::Throttle qw(throttle); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # execute 2 lambdas a sec - blocking | ||||||||||||||||||||||||||||
| 140 | throttle(2)-> wait(@lambdas); | ||||||||||||||||||||||||||||
| 141 | # non-blocking | ||||||||||||||||||||||||||||
| 142 | lambda { | ||||||||||||||||||||||||||||
| 143 | context throttle(2), @lambdas; | ||||||||||||||||||||||||||||
| 144 | tail {}; | ||||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # share a rate-limiter between two sets of lambdas running in parallel | ||||||||||||||||||||||||||||
| 148 | # strictly 1 lambda in 10 seconds will be executed | ||||||||||||||||||||||||||||
| 149 | my $t = IO::Lambda::Throttle-> new(0.1); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # throttle lambdas sequentially | ||||||||||||||||||||||||||||
| 152 | sub track | ||||||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||||||
| 154 | my @lambdas = @_; | ||||||||||||||||||||||||||||
| 155 | return lambda { | ||||||||||||||||||||||||||||
| 156 | context $t-> ratelimit, @lambdas; | ||||||||||||||||||||||||||||
| 157 | tail {}; | ||||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # parallel two tracks - execution order will be | ||||||||||||||||||||||||||||
| 162 | # $a[0], $b[0], $a[1], $b[1], etc | ||||||||||||||||||||||||||||
| 163 | lambda { | ||||||||||||||||||||||||||||
| 164 | context track(@a), track(@b); | ||||||||||||||||||||||||||||
| 165 | tails {} | ||||||||||||||||||||||||||||
| 166 | }-> wait; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 API | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =over | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item new($rate = 0, $strict = 0) | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | The constructor creates a new rate-limiter object. The object methods (see | ||||||||||||||||||||||||||||
| 175 | below) generate lambdas that allow to execute lambdas with a given rate and | ||||||||||||||||||||||||||||
| 176 | algorithm. See L<rate> and C<strict> for description. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =item rate INT | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | C<$rate> is given in lambda/seconds, and means infinity if is 0. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =item strict BOOL | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | C<$strict> selects between fair and aggressive throttling . For example, if | ||||||||||||||||||||||||||||
| 185 | rate is 5 l/s, and first 5 lambdas all come within first 0.1 sec. With | ||||||||||||||||||||||||||||
| 186 | C<$strict> 0, all af them will be scheduled to execution immediately, but the | ||||||||||||||||||||||||||||
| 187 | 6th lambda will be delayed to 1.2 sec. With C<$strict> 1, all lambdas will be | ||||||||||||||||||||||||||||
| 188 | scheduled to be executed evenly with 0.2 sec delay. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =item next_timeout :: TIMEOUT | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Internal function, called when code needs to determine whether lambda is | ||||||||||||||||||||||||||||
| 193 | allowed to run immediately (function returns 0) or after a timeout (returns | ||||||||||||||||||||||||||||
| 194 | non-zero value). If a non-zero value is returned, it is guaranteed that after | ||||||||||||||||||||||||||||
| 195 | sleeping this time, next invocation of the function will return 0. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Override the function for your own implementation of rate-limiting function. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =item lock | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Returns a lambda that will execute when rate-limiter allows next execution: | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | context $throttle-> lock; | ||||||||||||||||||||||||||||
| 204 | tail { | ||||||||||||||||||||||||||||
| 205 | ... do something ... | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | The lambda can be reused. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =item ratelimit :: @lambdas -> @ret | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Returns a lambda that finishes when all passed @lambdas are finished. | ||||||||||||||||||||||||||||
| 213 | Executes them one by one, imposing a rate limit. Returns results of lambdas | ||||||||||||||||||||||||||||
| 214 | accumulated in a list. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =item throttle($rate,$strict) | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | Condition version of C<< new($rate,$strict)-> ratelimit >> | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =back | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Dmitry Karasik, E<lt>dmitry@karasik.eu.orgE<gt>. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||||||||