API Reference
Cap Class
fastapicap.Cap
Singleton-style Redis connection manager for Cap.
This class provides a shared, async Redis connection for all rate limiter
instances. It is not meant to be instantiated; use the classmethod
init_app
to initialize the connection.
Attributes:
Name | Type | Description |
---|---|---|
redis |
Optional[Redis]
|
The shared aioredis Redis connection instance. |
Example
Cap.init_app("redis://localhost:6379/0")
Now Cap.redis can be used by all limiters.
Prevent instantiation of Cap.
Raises:
Type | Description |
---|---|
RuntimeError
|
Always, to enforce singleton usage. |
Source code in fastapicap/connection.py
init_app
classmethod
Initialize the shared Redis connection for Cap.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
redis_url
|
str
|
The Redis connection URL. |
required |
Example
Cap.init_app("redis://localhost:6379/0")
Source code in fastapicap/connection.py
Strategies Class
fastapicap.RateLimiter
RateLimiter(limit, seconds=0, minutes=0, hours=0, days=0, key_func=None, on_limit=None, prefix='cap')
Bases: BaseLimiter
Implements a Fixed Window rate limiting algorithm.
This limiter restricts the number of requests within a fixed time window. When a new window starts, the counter resets to zero. All requests within the same window consume from the same counter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
limit
|
int
|
The maximum number of requests allowed within the defined window. Must be a positive integer. |
required |
seconds
|
int
|
The number of seconds defining the window size. Can be combined with minutes, hours, or days. Defaults to 0. |
0
|
minutes
|
int
|
The number of minutes defining the window size. Defaults to 0. |
0
|
hours
|
int
|
The number of hours defining the window size. Defaults to 0. |
0
|
days
|
int
|
The number of days defining the window size. Defaults to 0. |
0
|
key_func
|
Optional[Callable[[Request], str]]
|
An asynchronous or synchronous function to extract a unique key from the request. Defaults to client IP and path. |
None
|
on_limit
|
Optional[Callable[[Request, Response, int], None]]
|
An asynchronous or synchronous function called when the rate limit is exceeded. Defaults to raising HTTP 429. |
None
|
prefix
|
str
|
Redis key prefix for all limiter keys. Defaults to "cap". |
'cap'
|
Attributes:
Name | Type | Description |
---|---|---|
limit |
int
|
The maximum requests allowed per window. |
window_ms |
int
|
The calculated window size in milliseconds. |
lua_script |
str
|
The Lua script used for fixed window logic in Redis. |
Raises:
Type | Description |
---|---|
ValueError
|
If the |
Source code in fastapicap/strategy/fixed_window.py
__call__
async
Apply the rate limiting logic to the incoming request. It interacts with Redis to increment a counter within the current time window and checks if the limit has been exceeded.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The incoming FastAPI request object. |
required |
response
|
Response
|
The FastAPI response object. This can be
modified by the |
required |
Raises:
Type | Description |
---|---|
HTTPException
|
By default, if the rate limit is exceeded,
|
Source code in fastapicap/strategy/fixed_window.py
fastapicap.SlidingWindowRateLimiter
SlidingWindowRateLimiter(limit, seconds=0, minutes=0, hours=0, days=0, key_func=None, on_limit=None, prefix='cap')
Bases: BaseLimiter
Implements an Approximated Sliding Window rate limiting algorithm.
This algorithm provides a more accurate and smoother rate limiting experience than a simple Fixed Window, while being more memory-efficient than a pure log-based sliding window. It works by maintaining counters for the current fixed window and the immediately preceding fixed window. The effective count for the sliding window is then calculated as a weighted sum of the requests in the previous window and the current window, based on how much of the previous window still "slides" into the current view.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
limit
|
int
|
The maximum number of requests allowed within the defined sliding window. Must be a positive integer. |
required |
seconds
|
int
|
The number of seconds defining the size of the
individual fixed window segments that make up the sliding window.
This value, combined with others, determines the |
0
|
minutes
|
int
|
The number of minutes defining the window segment size. Defaults to 0. |
0
|
hours
|
int
|
The number of hours defining the window segment size. Defaults to 0. |
0
|
days
|
int
|
The number of days defining the window segment size. Defaults to 0. |
0
|
key_func
|
Optional[Callable[[Request], str]]
|
An asynchronous or
synchronous function to extract a unique key from the request.
Defaults to client IP and path. The function should accept
a |
None
|
on_limit
|
Optional[Callable[[Request, Response, int], None]]
|
An
asynchronous or synchronous function called when the rate limit is exceeded.
Defaults to raising HTTP 429. The function should accept a
|
None
|
prefix
|
str
|
Redis key prefix for all limiter keys. Defaults to "cap". |
'cap'
|
Attributes:
Name | Type | Description |
---|---|---|
limit |
int
|
The maximum requests allowed within the sliding window. |
window_ms |
int
|
The calculated size of a single fixed window segment
in milliseconds (e.g., if you set |
lua_script |
str
|
The Lua script used for the approximated sliding window logic in Redis. |
Raises:
Type | Description |
---|---|
ValueError
|
If the |
Note
This implementation relies on a Redis Lua script to atomically manage
and count requests within the current and previous fixed window segments.
The retry_after
value provided by the Lua script indicates the
approximate time in seconds until the next request might be allowed.
Source code in fastapicap/strategy/sliding_window.py
__call__
async
Applies the approximated sliding window rate limiting logic to the incoming request.
This method is the core of the rate limiter. It interacts with Redis to increment counters for the current and previous window segments and checks if the estimated count within the sliding window exceeds the limit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The incoming FastAPI request object. |
required |
response
|
Response
|
The FastAPI response object. This can be
modified by the |
required |
Raises:
Type | Description |
---|---|
HTTPException
|
By default, if the rate limit is exceeded,
|
Source code in fastapicap/strategy/sliding_window.py
fastapicap.TokenBucketRateLimiter
TokenBucketRateLimiter(capacity, tokens_per_second=0, tokens_per_minute=0, tokens_per_hour=0, tokens_per_day=0, key_func=None, on_limit=None, prefix='cap')
Bases: BaseLimiter
Implements the Token Bucket rate limiting algorithm.
The Token Bucket algorithm works like a bucket that tokens are continuously
added to at a fixed refill_rate
. Each request consumes one token.
If a request arrives and there are tokens available in the bucket,
the request is processed, and a token is removed. If the bucket is empty,
the request is denied (or queued). The capacity
defines the maximum
number of tokens the bucket can hold, allowing for bursts of traffic
up to that capacity. This algorithm is excellent for controlling the
average rate of requests while permitting bursts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
capacity
|
int
|
The maximum number of tokens the bucket can hold. This determines the maximum burst size allowed. Must be a positive integer. |
required |
tokens_per_second
|
float
|
The rate at which tokens are added to the
bucket, in tokens per second. If combined with other |
0
|
tokens_per_minute
|
float
|
The token refill rate in tokens per minute. Defaults to 0. |
0
|
tokens_per_hour
|
float
|
The token refill rate in tokens per hour. Defaults to 0. |
0
|
tokens_per_day
|
float
|
The token refill rate in tokens per day. Defaults to 0. |
0
|
key_func
|
Optional[Callable[[Request], str]]
|
An asynchronous or
synchronous function to extract a unique key from the request.
Defaults to client IP and path. The function should accept
a |
None
|
on_limit
|
Optional[Callable[[Request, Response, int], None]]
|
An
asynchronous or synchronous function called when the rate limit is exceeded.
Defaults to raising HTTP 429. The function should accept a
|
None
|
prefix
|
str
|
Redis key prefix for all limiter keys. Defaults to "cap". |
'cap'
|
Attributes:
Name | Type | Description |
---|---|---|
capacity |
int
|
The configured maximum bucket capacity. |
refill_rate |
float
|
The total calculated token refill rate in tokens per millisecond. |
lua_script |
str
|
The Lua script used for token bucket logic in Redis. |
Raises:
Type | Description |
---|---|
ValueError
|
If the |
Source code in fastapicap/strategy/token_bucket.py
__call__
async
Applies the Token Bucket rate limiting logic to the incoming request.
This method is the core of the rate limiter. It interacts with Redis to simulate token consumption and bucket refill, determining if the request is allowed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The incoming FastAPI request object. |
required |
response
|
Response
|
The FastAPI response object. This can be
modified by the |
required |
Raises:
Type | Description |
---|---|
HTTPException
|
By default, if the rate limit is exceeded,
|
Source code in fastapicap/strategy/token_bucket.py
fastapicap.LeakyBucketRateLimiter
LeakyBucketRateLimiter(capacity, leaks_per_second=0, leaks_per_minute=0, leaks_per_hour=0, leaks_per_day=0, key_func=None, on_limit=None, prefix='cap')
Bases: BaseLimiter
Implements the Leaky Bucket rate limiting algorithm.
The Leaky Bucket algorithm models traffic flow like water in a bucket. Requests are "drops" added to the bucket. If the bucket overflows (exceeds capacity), new requests are rejected. "Water" (requests) leaks out of the bucket at a constant rate, making space for new requests. This algorithm is known for producing a smooth, constant output rate of requests, which helps in preventing bursts from overwhelming downstream services.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
capacity
|
int
|
The maximum capacity of the bucket. This represents the maximum number of requests that can be held in the bucket before new requests are rejected. Must be a positive integer. |
required |
leaks_per_second
|
float
|
The rate at which requests "leak" (are processed)
from the bucket, in requests per second. If combined with other
|
0
|
leaks_per_minute
|
float
|
The leak rate in requests per minute. Defaults to 0. |
0
|
leaks_per_hour
|
float
|
The leak rate in requests per hour. Defaults to 0. |
0
|
leaks_per_day
|
float
|
The leak rate in requests per day. Defaults to 0. |
0
|
key_func
|
Optional[Callable[[Request], str]]
|
An asynchronous or
synchronous function to extract a unique key from the request.
Defaults to client IP and path. The function should accept
a |
None
|
on_limit
|
Optional[Callable[[Request, Response, int], None]]
|
An
asynchronous or synchronous function called when the rate limit is exceeded.
Defaults to raising HTTP 429. The function should accept a
|
None
|
prefix
|
str
|
Redis key prefix for all limiter keys. Defaults to "cap". |
'cap'
|
Attributes:
Name | Type | Description |
---|---|---|
capacity |
int
|
The configured maximum bucket capacity. |
leak_rate |
float
|
The total calculated leak rate in requests per millisecond. |
lua_script |
str
|
The Lua script used for leaky bucket logic in Redis. |
Raises:
Type | Description |
---|---|
ValueError
|
If the |
Source code in fastapicap/strategy/leaky_bucket.py
__call__
async
Applies the leaky bucket rate limiting logic to the incoming request.
This method is the core of the rate limiter. It interacts with Redis to simulate adding a "drop" to the bucket and checks if it overflows.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The incoming FastAPI request object. |
required |
response
|
Response
|
The FastAPI response object. This can be
modified by the |
required |
Raises:
Type | Description |
---|---|
HTTPException
|
By default, if the rate limit is exceeded,
|
Source code in fastapicap/strategy/leaky_bucket.py
fastapicap.GCRARateLimiter
GCRARateLimiter(burst, tokens_per_second=0, tokens_per_minute=0, tokens_per_hour=0, tokens_per_day=0, key_func=None, on_limit=None, prefix='cap')
Bases: BaseLimiter
Implements the Generic Cell Rate Algorithm (GCRA) for rate limiting.
GCRA is a popular algorithm that controls the rate of events by tracking the "Theoretical Arrival Time" (TAT) of the next allowed event. It's often used for API rate limiting as it provides a smooth, burstable rate.
This limiter allows for a burst of requests up to burst
capacity,
and then enforces a steady rate defined by tokens_per_second
,
tokens_per_minute
, tokens_per_hour
, or tokens_per_day
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
burst
|
int
|
The maximum number of additional requests that can be served instantly (i.e., the "burst" capacity beyond the steady rate). This defines how many requests can be handled without delay if the system has been idle. |
required |
tokens_per_second
|
float
|
The steady rate of tokens allowed per second.
If combined with other |
0
|
tokens_per_minute
|
float
|
The steady rate of tokens allowed per minute. Defaults to 0. |
0
|
tokens_per_hour
|
float
|
The steady rate of tokens allowed per hour. Defaults to 0. |
0
|
tokens_per_day
|
float
|
The steady rate of tokens allowed per day. Defaults to 0. |
0
|
key_func
|
Optional[Callable[[Request], str]]
|
An asynchronous function
to extract a unique key from the request. This key is used to
identify the subject being rate-limited (e.g., client IP, user ID).
If |
None
|
on_limit
|
Optional[Callable[[Request, Response, int], None]]
|
An
asynchronous function called when the rate limit is exceeded.
It receives the |
None
|
prefix
|
str
|
A string prefix for all Redis keys used by this limiter. Defaults to "cap". |
'cap'
|
Attributes:
Name | Type | Description |
---|---|---|
burst |
int
|
The configured burst capacity. |
tokens_per_second |
float
|
The total calculated steady rate in tokens per second. |
period |
float
|
The calculated time period (in milliseconds) between allowed tokens. |
lua_script |
str
|
The Lua script used for GCRA logic in Redis. |
Raises:
Type | Description |
---|---|
ValueError
|
If the total calculated |
Note
The GCRA_LUA
script handles the core rate-limiting logic in Redis,
ensuring atomic operations. The retry_after
value returned by the
Lua script (if a limit is hit) indicates the number of milliseconds
until the next request would be allowed.
Source code in fastapicap/strategy/gcra.py
__call__
async
Executes the GCRA rate-limiting logic for the incoming request.
This method is designed to be used as a FastAPI dependency or decorator.
It interacts with Redis to check if the request is allowed based on
the configured GCRA parameters. If the limit is exceeded, it calls
the on_limit
handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The incoming FastAPI request object. |
required |
response
|
Response
|
The FastAPI response object. This can be
modified by the |
required |
Raises:
Type | Description |
---|---|
HTTPException
|
By default, if the rate limit is exceeded,
|
Source code in fastapicap/strategy/gcra.py
fastapicap.SlidingWindowLogRateLimiter
SlidingWindowLogRateLimiter(limit, window_seconds=0, window_minutes=0, window_hours=0, window_days=0, key_func=None, on_limit=None, prefix='cap')
Bases: BaseLimiter
Implements a Sliding Window (Log-based) rate limiting algorithm.
This is the most accurate form of the sliding window algorithm. It works by
storing a timestamp for every request made by a client within a Redis sorted set.
When a new request comes in, the algorithm first removes all timestamps that
fall outside the current sliding window. Then, it counts the number of remaining
timestamps within the window. If this count is below the limit
, the request
is allowed, and its timestamp is added to the set. This method ensures
precise rate limiting as the window truly "slides" over time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
limit
|
int
|
The maximum number of requests allowed within the defined sliding window. Must be a positive integer. |
required |
window_seconds
|
int
|
The number of seconds defining the size of the sliding window. Can be combined with minutes, hours, or days. Defaults to 0. |
0
|
window_minutes
|
int
|
The number of minutes defining the window size. Defaults to 0. |
0
|
window_hours
|
int
|
The number of hours defining the window size. Defaults to 0. |
0
|
window_days
|
int
|
The number of days defining the window size. Defaults to 0. |
0
|
key_func
|
Optional[Callable[[Request], str]]
|
An asynchronous or
synchronous function to extract a unique key from the request.
Defaults to client IP and path. The function should accept
a |
None
|
on_limit
|
Optional[Callable[[Request, Response, int], None]]
|
An
asynchronous or synchronous function called when the rate limit is exceeded.
Defaults to raising HTTP 429. The function should accept a
|
None
|
prefix
|
str
|
Redis key prefix for all limiter keys. Defaults to "cap". |
'cap'
|
Attributes:
Name | Type | Description |
---|---|---|
limit |
int
|
The maximum requests allowed within the sliding window. |
window_seconds |
int
|
The total calculated window size in seconds. |
lua_script |
str
|
The Lua script used for the log-based sliding window logic in Redis. |
Raises:
Type | Description |
---|---|
ValueError
|
If the |
Note
This implementation uses Redis sorted sets (ZADD
, ZREMRANGEBYSCORE
, ZCARD
)
to store and manage request timestamps, ensuring atomic operations
for accurate rate limiting.
Source code in fastapicap/strategy/sliding_window_log.py
__call__
async
Applies the log-based sliding window rate limiting logic to the incoming request.
This method is the core of the rate limiter. It interacts with Redis to manage request timestamps within a sorted set and checks if the total count within the current sliding window exceeds the configured limit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The incoming FastAPI request object. |
required |
response
|
Response
|
The FastAPI response object. This can be
modified by the |
required |
Raises:
Type | Description |
---|---|
HTTPException
|
By default, if the rate limit is exceeded,
|