FastAPI/RateLimit/Driver.pm

filter: Statementstmt 83.3% 15 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 3 / 6 Podpod 100% 3 / 3 totaltotal 77.7% 21 / 27 CC 1 SCAR 4.1
CC
1
Cov
83%
increment_async
2.0
get_async
2.0
reset_async
2.0
CRAP 1.5
« FastAPI/Queue.pm ↑ Summary FastAPI/BotProtection.pm »
1 package PAGI::FastAPI::RateLimit::Driver;
2  
3 24 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use experimental 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 our $VERSION = qv('v1.7.3');
8 24 our $AUTHORITY = 'cpan:MANWAR';
9  
10 24 class PAGI::FastAPI::RateLimit::Driver {
11  
12 0 ▶ method increment_async ($key, $ttl) {
Subroutine increment_async: not called CC=1 SCAR=6.9
Pod documented
13 0 die "Driver subclass must implement 'increment_async'";
14 24 }
15  
16 0 ▶ method get_async ($key) {
Subroutine get_async: not called CC=1 SCAR=6.9
Pod documented
17 0 die "Driver subclass must implement 'get_async'";
18 23 }
19  
20 0 ▶ method reset_async ($key) {
Subroutine reset_async: not called CC=1 SCAR=6.9
Pod documented
21 0 die "Driver subclass must implement 'reset_async'";
22 23 }
23 }
24  
25 =encoding utf-8
26  
27 =head1 NAME
28  
29 PAGI::FastAPI::RateLimit::Driver - Abstract Base Class for Rate Limiting Storage Drivers
30  
31 =head1 VERSION
32  
33 Version v1.7.3
34  
35 =head1 SYNOPSIS
36  
37 package PAGI::FastAPI::RateLimit::Driver::Custom;
38  
39 use v5.38;
40 use experimental 'class';
41 use Future;
42  
43 class PAGI::FastAPI::RateLimit::Driver::Custom :isa(PAGI::FastAPI::RateLimit::Driver) {
44  
45 method increment_async ($key, $ttl) {
46 # Increment hit count for $key asynchronously...
47 return Future->done($new_count, $expires_at);
48 }
49  
50 method get_async ($key) {
51 # Fetch current hit count asynchronously...
52 return Future->done($current_count);
53 }
54  
55 method reset_async ($key) {
56 # Clear stored hit count for $key...
57 return Future->done(1);
58 }
59 }
60  
61 =head1 DESCRIPTION
62  
63 C<PAGI::FastAPI::RateLimit::Driver> defines the abstract async interface for
64 all rate-limiting storage backends used by L<PAGI::FastAPI::Middleware::RateLimit>.
65  
66 Custom storage drivers (e.g., Redis, Memcached, DynamoDB) must inherit from
67 this class and implement its asynchronous methods, ensuring they return
68 L<Future> instances to maintain non-blocking behavior inside the PAGI event
69 loop.
70  
71 As of version C<v1.0.0>, we have built-in support for in memory rate limit by
72 L<PAGI::FastAPI::RateLimit::Driver::Memory>.
73  
74 We even have demo app created for in memory rate limit: C<eg/rate_limit_demo.pl>
75  
76 If you are looking for more specialised options then you have the following
77 choices as separate companion packages: L<PAGI::FastAPI::RateLimit::Driver::CHI>
78 and L<PAGI::FastAPI::RateLimit::Driver::Redis>.
79  
80 =head1 REQUIRED METHODS
81  
82 Subclasses B<must> override the following methods. Calling any of these
83 directly on the base class will throw an exception.
84  
85 =head2 C<increment_async($key, $ttl)>
86  
87 my $future = $driver->increment_async($key, $ttl);
88 my ($count, $expires_at) = $future->get;
89  
90 Increments the request hit counter for the specified C<$key> by 1 and sets
91 or updates its Time-To-Live (C<$ttl>) in seconds.
92  
93 =over 4
94  
95 =item * C<$key> - Scalar string uniquely identifying the client bucket (e.g., IP address, API key, user ID).
96  
97 =item * C<$ttl> - Integer window duration in seconds.
98  
99 =back
100  
101 Returns a L<Future> resolving to a two-element list C<($count, $expires_at)>:
102  
103 =over 4
104  
105 =item * C<$count> - Integer representing the updated hit count for the key.
106  
107 =item * C<$expires_at> - Unix epoch timestamp at which the current window
108 resets. Should remain stable across calls within the same window (i.e. set
109 once on the first hit, not recalculated on every increment).
110  
111 =back
112  
113 B<Note:> L<PAGI::FastAPI::Middleware::RateLimit> reads both values via list
114 assignment (C<< my ($count, $reset_at) = await $driver->increment_async(...) >>)
115 and uses C<$expires_at> to populate the C<x-ratelimit-reset> response header
116 and to compute C<retry-after> on a C<429>. A driver that resolves with only
117 a single value will leave C<$expires_at> C<undef>, silently disabling the
118 C<x-ratelimit-reset> header and making C<retry-after> fall back to the full
119 window length rather than the time actually remaining in it.
120  
121 =head2 C<get_async($key)>
122  
123 my $future = $driver->get_async($key);
124  
125 Retrieves the current hit count for the specified C<$key> without altering
126 its expiration.
127  
128 Returns a L<Future> resolving to an integer (C<0> if the key does not exist
129 or has expired).
130  
131 =head2 C<reset_async($key)>
132  
133 my $future = $driver->reset_async($key);
134  
135 Clears or expires the tracking record for C<$key> immediately.
136  
137 Returns a L<Future> resolving to a boolean true value on success.
138  
139 =head1 SEE ALSO
140  
141 L<PAGI::FastAPI::RateLimit::Driver::Memory>, L<PAGI::FastAPI::Middleware::RateLimit>
142  
143 =head1 AUTHOR
144  
145 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
146  
147 =head1 REPOSITORY
148  
149 L<https://github.com/manwar/PAGI-FastAPI>
150  
151 =head1 BUGS
152  
153 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>.
154 I will be notified and then you'll automatically be notified of progress on your
155 bug as I make changes.
156  
157 =head1 SUPPORT
158  
159 You can find documentation for this module with the perldoc command.
160  
161 perldoc PAGI::FastAPI::RateLimit::Driver
162  
163 You can also look for information at:
164  
165 =over 4
166  
167 =item * BUG Report
168  
169 L<https://github.com/manwar/PAGI-FastAPI/issues>
170  
171 =item * Search MetaCPAN
172  
173 L<https://metacpan.org/dist/PAGI-FastAPI/>
174  
175 =back
176  
177 =head1 LICENSE AND COPYRIGHT
178  
179 Copyright (C) 2026 Mohammad Sajid Anwar.
180  
181 This program is free software; you can redistribute it and/or modify it under
182 the terms of the Artistic License (2.0).
183  
184 =cut
185  
186 23 1; # End of PAGI::FastAPI::RateLimit::Driver
« FastAPI/Queue.pm ↑ Summary FastAPI/BotProtection.pm »