blib/lib/Redis/RateLimit.pm

filter: Statementstmt 42.6% 61 / 143 Branchbran 11.5% 3 / 26 Conditioncond 10.0% 1 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 43.9% 18 / 41 Podpod 100% 9 / 9 totaltotal 39.1% 92 / 235 CC 24 SCAR 18.0
CC
24
Cov
36%
__ANON__
20.0
_script_args
12.0
whitelist
12.0
CRAP 6.1
↑ Summary
1 package Redis::RateLimit;
2 # ABSTRACT: Sliding window rate limiting with Redis
3 2 $Redis::RateLimit::VERSION = '1.0002';
4 2 ▶use 5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Digest::SHA1 qw/sha1_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Share qw/dist_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Slurp::Tiny qw/read_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw/any max min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #pod =attr redis
17 #pod
18 #pod Redis client. If none is provided, a default is constructed for 127.0.0.1:6379.
19 #pod
20 #pod =cut
21  
22 has redis => (
23 is => 'ro',
24 lazy => 1,
25 0 ▶ default => sub { Redis->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 2 handles => { map +( "redis_$_" => $_ ), qw/
27 eval evalsha hget keys sadd srem
28 / },
29 );
30  
31 #pod =attr prefix
32 #pod
33 #pod A prefix to be included on each redis key. This prevents collisions with
34 #pod multiple applications using the same Redis DB. Defaults to 'ratelimit'.
35 #pod
36 #pod =cut
37  
38 has prefix => (
39 is => 'ro',
40 lazy => 1,
41 1 ▶ default => sub { 'ratelimit' },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 2 );
43  
44 #pod =attr client_prefix
45 #pod
46 #pod Set this to a true value if using a Redis client that supports transparent
47 #pod prefixing. Defaults to 0.
48 #pod
49 #pod =cut
50  
51 has client_prefix => (
52 is => 'ro',
53 1 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 2 );
55  
56 #pod =attr rules
57 #pod
58 #pod An arrayref of rules, each of which is a hashref with C<interval>, C<limit>,
59 #pod and optionally C<precision> values.
60 #pod
61 #pod =cut
62  
63 2 has rules => (
64 is => 'ro',
65 required => 1,
66 );
67  
68 3 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
69 3 my ( $next, $self ) = ( shift, shift );
70  
71 3 my $args = $self->$next(@_);
72 3 my $rules = delete $args->{rules};
73 $args->{rules} = [
74 map {
75 3 my $rule = $_;
76 5 ▶ defined $rule->{$_} || croak "$_ undefined" for qw/interval limit/;
Branch: 1 branch
BranchTrueFalseTotal
defined $rule->{$_} or croak("$_ undefined") 7 2 9
77 [
78 map 0+$_, # numify for later JSON encoding
79 3 @{$rule}{qw/interval limit/},
80 grep defined, $rule->{precision}
81 3 ];
82 } @$rules
83 ];
84  
85 1 return $args;
86 2 };
87  
88 2 has _script_cache => (
89 is => 'lazy',
90 );
91  
92 0 ▶sub _build__script_cache {
Subroutine _build__script_cache: not called CC=2 SCAR=17.9
93 0 my $self = shift;
94  
95 # cache scripts: { name => [ hash, script ], ... }
96 0 my %cache =(
97 check_rate_limit => [ $self->_check_limit_script ],
98 check_limit_incr => [ $self->_check_limit_incr_script ],
99 );
100 0 unshift @$_, sha1_hex($$_[0]) for values %cache;
101  
102 0 return \%cache;
103 }
104  
105 # Note: 1 is returned for a normal rate limited action, 2 is returned for a
106 # blacklisted action. Must sync with return codes in lua/check_limit.lua
107 0 ▶sub _DENIED_NUMS { (1, 2) }
Subroutine _DENIED_NUMS: not called CC=1 SCAR=6.9
108  
109 0 ▶sub _read_lua {
Subroutine _read_lua: not called CC=1 SCAR=6.9
110 0 my ( $self, $filename ) = @_;
111  
112 0 my $path = dist_file('Redis-RateLimit', "$filename.lua");
113 0 read_file($path, binmode => ':utf8');
114 }
115  
116 0 ▶sub _check_limit_script {
Subroutine _check_limit_script: not called CC=1 SCAR=6.9
117 0 my $self = shift;
118  
119 0 join("\n", map(
120 $self->_read_lua($_), qw/
121 unpack_args
122 check_whitelist_blacklist
123 check_limit
124 /),
125 'return 0'
126 );
127 }
128  
129 0 ▶sub _check_limit_incr_script {
Subroutine _check_limit_incr_script: not called CC=1 SCAR=6.9
130 0 my $self = shift;
131  
132 0 join("\n", map(
133 $self->_read_lua($_), qw/
134 unpack_args
135 check_whitelist_blacklist
136 check_limit
137 check_incr_limit
138 /),
139 );
140 }
141  
142 0 ▶sub _exec {
Subroutine _exec: not called CC=1 SCAR=6.9
143 0 my ( $self, $name, @params ) = @_;
144  
145 0 my ( $hash, $script ) = @{ $self->_script_cache->{$name} };
146 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 $self->redis_evalsha($hash, @params);
148 }
149 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
150 0 ▶ croak $_ unless /NOSCRIPT/;
Branch: 1 branch
BranchTrueFalseTotal
unless /NOSCRIPT/u 0 0 0
151 0 $self->redis_eval($script, @params);
152 0 };
153 }
154  
155 has _json_encoder => (
156 is => 'ro',
157 1 ▶ default => sub { JSON::MaybeXS->new(utf8 => 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
158 2 handles => {
159 json_encode => 'encode',
160 },
161 );
162  
163 has _whitelist_key => (
164 is => 'ro',
165 1 ▶ default => sub { shift->_prefix_key(whitelist => 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
166 2 );
167  
168 has _blacklist_key => (
169 is => 'ro',
170 1 ▶ default => sub { shift->_prefix_key(blacklist => 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
171 2 );
172  
173 2 ▶sub _prefix_key {
Subroutine _prefix_key: called CC=3 SCAR=12.0
174 2 my ( $self, $key, $force ) = @_;
175  
176 2 my @parts = $key;
177  
178 # Support prefixing with an optional `force` argument, but omit prefix by
179 # default if the client library supports transparent prefixing.
180 2 ▶ unshift @parts, $self->prefix if $force || !$self->client_prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $force or not $self->client_prefix 2 0 2
Condition33%1/3
$force or not $self->client_prefix
l!l&&r!l&&!r
200
MC/DC0%0/2
$force or not $self->client_prefix
$force not $self->client_prefix
Truth table33%1/3
A or not(B)
A= $forceB= not $self->client_prefix
ABresult
1X1
011
000
181  
182 # The compact handles a falsy prefix
183 #_.compact(parts).join ':'
184 2 join ':', @parts;
185 }
186  
187 0 ▶sub _script_args {
Subroutine _script_args: not called CC=3 SCAR=24.8
188 0 my ( $self, $keys, $weight ) = @_;
189 0 ▶ $weight //= 1;
Condition0%0/2
$weight //= 1
l!l
00
MC/DC0%0/1
$weight //= 1
$weight
Truth table0%0/2
A
A= $weight
Aresult
11
00
190  
191 0 my @adjusted_keys = map $self->_prefix_key($_), grep length, @$keys;
192 0 ▶ croak "Bad keys: @$keys" unless @adjusted_keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @adjusted_keys 0 0 0
193  
194 0 my $rules = $self->json_encode($self->rules);
195 0 $weight = max($weight, 1);
196 return (
197 0 0+@adjusted_keys, @adjusted_keys,
198 $rules, time, $weight, $self->_whitelist_key, $self->_blacklist_key,
199 );
200 }
201  
202 #pod =method check($key | \@keys)
203 #pod
204 #pod Returns true if any of the keys are rate limited.
205 #pod
206 #pod =cut
207  
208 0 ▶sub check {
Subroutine check: not called CC=2 SCAR=17.9
Pod documented
209 0 my $self = shift;
210 0 ▶ my $keys = ref $_[0] ? shift : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
211  
212 0 my $result = $self->_exec(
213 check_rate_limit => $self->_script_args($keys)
214 );
215 0 ▶ return any { $result == $_ } _DENIED_NUMS;
Subroutine __ANON__: not called CC=1 SCAR=6.9
216 }
217  
218 #pod =method incr($key | \@keys [, $weight ])
219 #pod
220 #pod Returns true if any of the keys are rate limited, otherwise, it increments
221 #pod counts and returns false.
222 #pod
223 #pod =cut
224  
225 0 ▶sub incr {
Subroutine incr: not called CC=2 SCAR=17.9
Pod documented
226 0 my ( $self, $keys, $weight ) = @_;
227 0 ▶ $keys = [ $keys ] unless ref $keys;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keys 0 0 0
228  
229 0 my $result = $self->_exec(
230 check_limit_incr => $self->_script_args($keys, $weight)
231 );
232 0 ▶ return any { $result == $_ } _DENIED_NUMS;
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 }
234  
235 #pod =method keys
236 #pod
237 #pod Returns all of the rate limiter's with prefixes removed.
238 #pod
239 #pod =cut
240  
241 0 ▶sub keys {
Subroutine keys: not called CC=1 SCAR=6.9
Pod documented
242 0 my $self = shift;
243  
244 0 my @results = $self->redis_keys($self->_prefix_key('*'));
245 0 my $re = $self->_prefix_key('(.+)');
246 0 map /^$re/, @results;
247 }
248  
249 #pod =method violated_rules($key | \@keys)
250 #pod
251 #pod Returns a list of rate limit rules violated for any of the keys, or an empty
252 #pod list.
253 #pod
254 #pod =cut
255  
256 0 ▶sub violated_rules {
Subroutine violated_rules: not called CC=2 SCAR=17.9
Pod documented
257 0 my $self = shift;
258 0 ▶ my $keys = ref $_[0] ? shift : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
259  
260 0 ▶ my $check_key = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 my $key = shift;
262  
263 0 ▶ my $check_rule = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
264 0 my $rule = shift;
265 # Note: this mirrors precision computation in `check_limit.lua`
266 # on lines 7 and 8 and count key construction on line 16
267 0 my ( $interval, $limit, $precision ) = @$rule;
268 0 ▶ $precision = min($precision // $interval, $interval);
Condition0%0/3
$precision // $interval
l!l&&r!l&&!r
000
MC/DC0%0/2
$precision // $interval
$precision $interval
Truth table0%0/3
A // B
A= $precisionB= $interval
ABresult
1X1
011
000
269 0 my $count_key = "$interval:$precision:";
270  
271 0 my $count = $self->redis_hget($self->_prefix_key($key), $count_key);
272 0 ▶ $count //= -1;
Condition0%0/2
$count //= -1
l!l
00
MC/DC0%0/1
$count //= -1
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
273 0 ▶ return unless $count >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless $count >= $limit 0 0 0
274  
275 0 return { interval => $interval, limit => $limit };
276 0 };
277  
278 0 map $check_rule->($_), @{ $self->rules };
279 0 };
280  
281 0 return map $check_key->($_), @$keys;
282 }
283  
284 #pod =method limited_keys($key | \@keys)
285 #pod
286 #pod Returns a list of limited keys.
287 #pod
288 #pod =cut
289  
290 0 ▶sub limited_keys {
Subroutine limited_keys: not called CC=2 SCAR=17.9
Pod documented
291 0 my $self = shift;
292 0 ▶ my $keys = ref $_[0] ? shift : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
293  
294 0 grep $self->check($_), @$keys;
295 }
296  
297 #pod =method whitelist($key | \@keys)
298 #pod
299 #pod Adds the keys to the whitelist so they are never rate limited.
300 #pod
301 #pod =cut
302  
303 0 ▶sub whitelist {
Subroutine whitelist: not called CC=3 SCAR=24.8
Pod documented
304 0 my $self = shift;
305 0 ▶ my $keys = ref $_[0] ? shift : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
306  
307 0 for ( @$keys ) {
308 0 my $key = $self->_prefix_key($_);
309 0 $self->redis_srem($self->_blacklist_key, $key);
310 0 $self->redis_sadd($self->_whitelist_key, $key);
311 }
312 }
313  
314 #pod =method unwhitelist($key | \@keys)
315 #pod
316 #pod Removes the keys from the whitelist.
317 #pod
318 #pod =cut
319  
320 0 ▶sub unwhitelist {
Subroutine unwhitelist: not called CC=3 SCAR=24.8
Pod documented
321 0 my $self = shift;
322 0 ▶ my $keys = ref $_[0] ? shift : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
323  
324 0 for ( @$keys ) {
325 0 my $key = $self->_prefix_key($_);
326 0 $self->redis_srem($self->_whitelist_key, $key);
327 }
328 }
329  
330 #pod =method blacklist($key | \@keys)
331 #pod
332 #pod Adds the keys to the blacklist so they are always rate limited.
333 #pod
334 #pod =cut
335  
336 0 ▶sub blacklist {
Subroutine blacklist: not called CC=3 SCAR=24.8
Pod documented
337 0 my $self = shift;
338 0 ▶ my $keys = ref $_[0] ? shift : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
339  
340 0 for ( @$keys ) {
341 0 my $key = $self->_prefix_key($_);
342 0 $self->redis_srem($self->_whitelist_key, $key);
343 0 $self->redis_sadd($self->_blacklist_key, $key);
344 }
345 }
346  
347 #pod =method unblacklist($key | \@keys)
348 #pod
349 #pod Removes the keys from the blacklist.
350 #pod
351 #pod =cut
352  
353 0 ▶sub unblacklist {
Subroutine unblacklist: not called CC=3 SCAR=24.8
Pod documented
354 0 my $self = shift;
355 0 ▶ my $keys = ref $_[0] ? shift : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
356  
357 0 for ( @$keys ) {
358 0 my $key = $self->_prefix_key($_);
359 0 $self->redis_srem($self->_blacklist_key, $key);
360 }
361 }
362  
363 2 1;
364  
365 __END__
↑ Summary