blib/lib/PAGI/FastAPI/RateLimit/Driver/Redis.pm

filter: Statementstmt 83.3% 35 / 42 Branchbran 30.0% 3 / 10 Conditioncond 40.0% 4 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 90.0% 9 / 10 Podpod 100% 3 / 3 totaltotal 67.9% 55 / 81 CC 10 SCAR 14.4
CC
10
Cov
68%
increment_async
6.8
__ANON__
6.0
get_async
5.6
CRAP 4.2
↑ Summary
1 package PAGI::FastAPI::RateLimit::Driver::Redis;
2  
3 2 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use experimental 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = qv('v0.1.0');
8 2 our $AUTHORITY = 'cpan:MANWAR';
9  
10 2 ▶use PAGI::FastAPI::RateLimit::Driver;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 class PAGI::FastAPI::RateLimit::Driver::Redis :isa(PAGI::FastAPI::RateLimit::Driver) {
14 2 field $redis :param; # Async/Sync Redis handle (e.g. Mojo::Redis, Redis, Redis::Fast)
15 2 field $key_prefix :param = 'pagi_rl:';
16  
17 5 ▶ method _wrap_future ($res) {
Subroutine _wrap_future: called CC=3 SCAR=14.2
18 5 ▶ if (ref($res) && $res->can('then')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res and $res->can("then")) 0 5 5
Condition33%1/3
ref $res and $res->can("then")
!ll&&!rl&&r
500
MC/DC0%0/2
ref $res and $res->can("then")
ref $res $res->can("then")
Truth table33%1/3
A and B
A= ref $resB= $res->can("then")
ABresult
0X0
100
111
19 0 return $res;
20 }
21 5 return Future->done($res);
22 }
23  
24 4 ▶ method increment_async ($key, $ttl) {
Subroutine increment_async: called CC=4 SCAR=19.2
Pod documented
25 4 my $redis_key = $key_prefix . $key;
26  
27 # Atomic INCR + EXPIRE via Lua script to prevent race conditions
28 4 my $lua = <<~'LUA';
29 local current = redis.call('INCR', KEYS[1])
30 if current == 1 then
31 redis.call('EXPIRE', KEYS[1], ARGV[1])
32 end
33 return current
34 LUA
35  
36 # Supports clients with eval() or call_p() methods
37 4 my $res;
38 4 ▶ if ($redis->can('eval')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($redis->can('eval')) { } 4 0 4
elsif ($redis->can('call_p')) { } 0 0 0
39 4 $res = $redis->eval($lua, 1, $redis_key, $ttl);
40 } elsif ($redis->can('call_p')) {
41 0 $res = $redis->call_p('EVAL', $lua, 1, $redis_key, $ttl);
42 } else {
43 # Fallback for basic drivers
44 0 my $count = $redis->incr($redis_key);
45 0 ▶ $redis->expire($redis_key, $ttl) if $count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 1 0 0 0
46 0 $res = $count;
47 }
48  
49 4 return $self->_wrap_future($res);
50 }
51  
52 2 ▶ method get_async ($key) {
Subroutine get_async: called CC=4 SCAR=17.3
Pod documented
53 2 my $redis_key = $key_prefix . $key;
54 2 my $res = $redis->get($redis_key);
55  
56 2 ▶ if (ref($res) && $res->can('then')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res and $res->can("then")) 0 2 2
Condition33%1/3
ref $res and $res->can("then")
!ll&&!rl&&r
200
MC/DC0%0/2
ref $res and $res->can("then")
ref $res $res->can("then")
Truth table33%1/3
A and B
A= ref $resB= $res->can("then")
ABresult
0X0
100
111
57 0 ▶ return $res->then(sub ($val) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
58 0 ▶ return Future->done($val // 0);
Condition0%0/2
$val // 0
l!l
00
MC/DC0%0/1
$val // 0
$val
Truth table0%0/2
A
A= $val
Aresult
11
00
59 0 });
60 }
61  
62 2 ▶ return Future->done($res // 0);
Condition100%2/2
$res // 0
l!l
11
MC/DC100%1/1
$res // 0
$res
Truth table100%2/2
A
A= $res
Aresult
11
00
63 }
64  
65 1 ▶ method reset_async ($key) {
Subroutine reset_async: called CC=1 SCAR=0.0
Pod documented
66 1 my $redis_key = $key_prefix . $key;
67 1 my $res = $redis->del($redis_key);
68  
69 1 return $self->_wrap_future($res);
70 }
71 }
72  
73 =encoding utf-8
74  
75 =head1 NAME
76  
77 PAGI::FastAPI::RateLimit::Driver::Redis - Redis Storage Driver for PAGI::FastAPI Rate Limiting
78  
79 =head1 VERSION
80  
81 Version v0.1.0
82  
83 =head1 SYNOPSIS
84  
85 use PAGI::FastAPI;
86 use PAGI::FastAPI::RateLimit::Driver::Redis;
87 use Redis;
88  
89 # Initialize Redis client instance
90 my $redis = Redis->new(server => '127.0.0.1:6379');
91  
92 # Instantiate rate limiting driver
93 my $driver = PAGI::FastAPI::RateLimit::Driver::Redis->new(
94 redis => $redis,
95 key_prefix => 'api_rl:', # Optional namespace prefix
96 );
97  
98 my $app = PAGI::FastAPI->new();
99  
100 # Register middleware with Redis driver
101 $app->add_rate_limit(
102 requests => 100,
103 window => 60,
104 driver => $driver,
105 );
106  
107 =head1 DESCRIPTION
108  
109 C<PAGI::FastAPI::RateLimit::Driver::Redis> is a high-performance, distributed storage driver for L<PAGI::FastAPI::Middleware::RateLimit>.
110  
111 It enables L<PAGI::FastAPI> web applications to maintain centralized, multi-worker hit counts across server clusters using an external Redis instance. Increments and TTL assignments are executed atomically via Lua scripts to prevent race conditions during heavy concurrent request bursts.
112  
113 =head1 CONSTRUCTOR
114  
115 =head2 C<new(%options)>
116  
117 Instantiates a new Redis rate-limiting driver.
118  
119 Accepts the following named parameters:
120  
121 =over 4
122  
123 =item * C<redis> (Required)
124  
125 An instantiated Redis client object (e.g., L<Redis>, L<Redis::Fast>, or L<Mojo::Redis>).
126  
127 =item * C<key_prefix> (Optional)
128  
129 A scalar string prepended to keys inside Redis to avoid key collisions. Defaults to C<'pagi_rl:'>.
130  
131 =back
132  
133 =head1 METHODS
134  
135 Inherits all methods from L<PAGI::FastAPI::RateLimit::Driver>.
136  
137 =head2 C<increment_async($key, $ttl)>
138  
139 my $future = $driver->increment_async($key, $ttl);
140  
141 Atomically increments the request counter for C<$key> and sets the TTL window if the bucket is new.
142  
143 Returns a L<Future> resolving to an integer containing the updated hit count.
144  
145 =head2 C<get_async($key)>
146  
147 my $future = $driver->get_async($key);
148  
149 Fetches the current request count for C<$key> without altering its TTL.
150  
151 Returns a L<Future> resolving to an integer hit count (or C<0> if the key does not exist or has expired).
152  
153 =head2 C<reset_async($key)>
154  
155 my $future = $driver->reset_async($key);
156  
157 Deletes the key immediately from Redis.
158  
159 Returns a L<Future> resolving to a true value on success.
160  
161 =head1 SEE ALSO
162  
163 L<PAGI::FastAPI::RateLimit::Driver>, L<PAGI::FastAPI::Middleware::RateLimit>, L<Redis>
164  
165 =head1 AUTHOR
166  
167 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
168  
169 =head1 REPOSITORY
170  
171 L<https://github.com/manwar/PAGI-FastAPI-RateLimit-Driver-Redis>
172  
173 =head1 BUGS
174  
175 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI-RateLimit-Driver-Redis/issues>.
176 I will be notified and then you'll automatically be notified of progress on your
177 bug as I make changes.
178  
179 =head1 SUPPORT
180  
181 You can find documentation for this module with the perldoc command.
182  
183 perldoc PAGI::FastAPI::RateLimit::Driver::Redis
184  
185 You can also look for information at:
186  
187 =over 4
188  
189 =item * BUG Report
190  
191 L<https://github.com/manwar/PAGI-FastAPI-RateLimit-Driver-Redis/issues>
192  
193 =item * CPAN Ratings
194  
195 L<http://cpanratings.perl.org/d/PAGI-FastAPI-RateLimit-Driver-Redis>
196  
197 =item * Search MetaCPAN
198  
199 L<https://metacpan.org/dist/PAGI-FastAPI-RateLimit-Driver-Redis/>
200  
201 =back
202  
203 =head1 LICENSE AND COPYRIGHT
204  
205 Copyright (C) 2026 Mohammad Sajid Anwar.
206  
207 This program is free software; you can redistribute it and/or modify it under
208 the terms of the Artistic License (2.0).
209  
210 =cut
211  
212 2 1; # End of PAGI::FastAPI::RateLimit::Driver::Redis
↑ Summary