| 1 | package PAGI::FastAPI::RateLimit::Driver::Memory; | ||||||||||||||||||||||||||||
| 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 | ▶ | use PAGI::FastAPI::RateLimit::Driver; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 24 | class PAGI::FastAPI::RateLimit::Driver::Memory :isa(PAGI::FastAPI::RateLimit::Driver) { | |||||||||||||||||||||||||||
| 13 | 24 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 24 | field $max_keys :param = 10000; | |||||||||||||||||||||||||||
| 16 | 24 | field %_storage; # $key => { count => Int, expires_at => EpochInt } | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1039 | ▶ | method _purge_expired ($key) { | ||||||||||||||||||||||||||
|
Subroutine
_purge_expired: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 19 | 1039 | my $now = time(); | |||||||||||||||||||||||||||
| 20 | 1039 | ▶ | if (exists $_storage{$key} && $_storage{$key}{expires_at} <= $now) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $_storage{$key} and $_storage{$key}{"expires_at"} <= $now
MC/DC0%0/2
exists $_storage{$key} and $_storage{$key}{"expires_at"} <= $now
exists $_storage{$key}
$_storage{$key}{"expires_at"} <= $now
Truth table66%2/3
A and B
A= exists $_storage{$key}B= $_storage{$key}{"expires_at"} <= $now
| |||||||||||||||||||||||||||||
| 21 | 0 | delete $_storage{$key}; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Probabilistic global sweep (~1% of operations) to clean unvisited keys | ||||||||||||||||||||||||||||
| 25 | 1039 | ▶ | if (rand() < 0.01) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 13 | $self->_sweep_all_expired($now); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 24 | } | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 913 | ▶ | method _sweep_all_expired ($now = time()) { | ||||||||||||||||||||||||||
|
Subroutine
_sweep_all_expired: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 31 | 913 | ▶ | while (my ($k, $v) = each %_storage) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 91201 | ▶ | delete $_storage{$k} if $v->{expires_at} <= $now; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 23 | } | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1017 | ▶ | method _enforce_max_keys () { | ||||||||||||||||||||||||||
|
Subroutine
_enforce_max_keys: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 37 | 1017 | ▶ | return if keys %_storage < $max_keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Evict expired keys first | ||||||||||||||||||||||||||||
| 40 | 900 | $self->_sweep_all_expired(); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # If still exceeding capacity, evict the earliest-expiring key | ||||||||||||||||||||||||||||
| 43 | 900 | ▶ | if (keys %_storage >= $max_keys) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 900 | my ($oldest_key) = sort { $_storage{$a}{expires_at} <=> $_storage{$b}{expires_at} } keys %_storage; | |||||||||||||||||||||||||||
| 45 | 900 | ▶ | delete $_storage{$oldest_key} if defined $oldest_key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 23 | } | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1036 | ▶ | method increment_async ($key, $ttl) { | ||||||||||||||||||||||||||
|
Subroutine
increment_async: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 1036 | $self->_purge_expired($key); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1036 | my $now = time; | |||||||||||||||||||||||||||
| 53 | 1036 | ▶ | if (!exists $_storage{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1017 | $self->_enforce_max_keys(); | |||||||||||||||||||||||||||
| 55 | 1017 | $_storage{$key} = { | |||||||||||||||||||||||||||
| 56 | count => 1, | ||||||||||||||||||||||||||||
| 57 | expires_at => $now + $ttl, | ||||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||
| 60 | 19 | $_storage{$key}{count}++; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1036 | return Future->done($_storage{$key}{count}, $_storage{$key}{expires_at}); | |||||||||||||||||||||||||||
| 64 | 23 | } | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | method get_async ($key) { | ||||||||||||||||||||||||||
|
Subroutine
get_async: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 3 | $self->_purge_expired($key); | |||||||||||||||||||||||||||
| 68 | 3 | ▶ | my $count = $_storage{$key} ? $_storage{$key}{count} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 3 | return Future->done($count); | |||||||||||||||||||||||||||
| 70 | 23 | } | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | method reset_async ($key) { | ||||||||||||||||||||||||||
|
Subroutine
reset_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 3 | delete $_storage{$key}; | |||||||||||||||||||||||||||
| 74 | 3 | return Future->done(1); | |||||||||||||||||||||||||||
| 75 | 23 | } | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | method count () { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 1 | return scalar keys %_storage; | |||||||||||||||||||||||||||
| 79 | 23 | } | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 NAME | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | PAGI::FastAPI::RateLimit::Driver::Memory - Default In-Memory Storage Driver for Rate Limiting | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 VERSION | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Version v1.7.3 | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | use PAGI::FastAPI::RateLimit::Driver::Memory; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | my $driver = PAGI::FastAPI::RateLimit::Driver::Memory->new(); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Increment hit count for key 'client_127.0.0.1' with a 60-second window | ||||||||||||||||||||||||||||
| 99 | $driver->increment_async('client_127.0.0.1', 60)->then(sub ($count, $expires_at) { | ||||||||||||||||||||||||||||
| 100 | print "Current request count: $count (resets at $expires_at)\n"; | ||||||||||||||||||||||||||||
| 101 | }); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Retrieve current hits | ||||||||||||||||||||||||||||
| 104 | $driver->get_async('client_127.0.0.1')->then(sub ($count) { | ||||||||||||||||||||||||||||
| 105 | print "Hits: $count\n"; | ||||||||||||||||||||||||||||
| 106 | }); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Reset quota | ||||||||||||||||||||||||||||
| 109 | $driver->reset_async('client_127.0.0.1'); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | C<PAGI::FastAPI::RateLimit::Driver::Memory> is the default, in-memory | ||||||||||||||||||||||||||||
| 114 | implementation of L<PAGI::FastAPI::RateLimit::Driver>. It ships directly | ||||||||||||||||||||||||||||
| 115 | with the core L<PAGI::FastAPI> distribution. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | It tracks request hits and window expirations per key inside an in-process | ||||||||||||||||||||||||||||
| 118 | hash table. Expired keys are automatically purged on access. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | B<Note:> Storage is maintained entirely in process memory. Hit counters are | ||||||||||||||||||||||||||||
| 121 | B<not> shared across multi-process workers (e.g., Starman, Hypnotoad) or | ||||||||||||||||||||||||||||
| 122 | distributed server environments. For production clusters, install a | ||||||||||||||||||||||||||||
| 123 | distributed storage driver such as C<PAGI::FastAPI::RateLimit::Driver::Redis>. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 METHODS | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | Inherits all methods from L<PAGI::FastAPI::RateLimit::Driver>. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 C<new()> | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Instantiates a new in-memory rate limiting driver. Takes no required arguments. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =over 4 | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item * C<max_keys> - Optional integer. Maximum distinct active rate-limit keys to retain in memory before forced eviction. Default: C<10000>. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =back | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 C<increment_async($key, $ttl)> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Purges any expired record for C<$key>, increments its counter, sets the | ||||||||||||||||||||||||||||
| 142 | expiration timestamp if missing, and returns a L<Future> resolving to a | ||||||||||||||||||||||||||||
| 143 | two-element list C<($count, $expires_at)>: the updated integer hit count and | ||||||||||||||||||||||||||||
| 144 | the Unix epoch timestamp at which the window resets. C<$expires_at> is set | ||||||||||||||||||||||||||||
| 145 | once, on the first hit of a window, and held steady for the rest of that | ||||||||||||||||||||||||||||
| 146 | window. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 C<get_async($key)> | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Purges any expired record for C<$key> and returns a L<Future> resolving to | ||||||||||||||||||||||||||||
| 151 | its current integer hit count (or C<0> if absent/expired). | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 C<reset_async($key)> | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Removes C<$key> from internal storage and returns a L<Future> resolving to C<1>. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | L<PAGI::FastAPI::RateLimit::Driver>, L<PAGI::FastAPI::Middleware::RateLimit> | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | L<https://github.com/manwar/PAGI-FastAPI> | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 BUGS | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>. | ||||||||||||||||||||||||||||
| 172 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||
| 173 | bug as I make changes. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | perldoc PAGI::FastAPI::RateLimit::Driver::Memory | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | You can also look for information at: | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =over 4 | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item * BUG Report | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | L<https://github.com/manwar/PAGI-FastAPI/issues> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =item * Search MetaCPAN | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | L<https://metacpan.org/dist/PAGI-FastAPI/> | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =back | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 200 | the terms of the Artistic License (2.0). | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =cut | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 23 | 1; # End of PAGI::FastAPI::RateLimit::Driver::Memory | |||||||||||||||||||||||||||