Limiters
provide.foundation.logger.ratelimit.limiters
¶
TODO: Add module docstring.
Classes¶
AsyncRateLimiter
¶
Asynchronous token bucket rate limiter. Uses asyncio.Lock for thread safety in async contexts.
Initialize the async rate limiter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
float
|
Maximum number of tokens (burst capacity) |
required |
refill_rate
|
float
|
Tokens refilled per second |
required |
Source code in provide/foundation/logger/ratelimit/limiters.py
Functions¶
get_stats
async
¶
Get rate limiter statistics.
Source code in provide/foundation/logger/ratelimit/limiters.py
is_allowed
async
¶
Check if a log message is allowed based on available tokens.
Returns:
| Type | Description |
|---|---|
bool
|
True if the log should be allowed, False if rate limited |
Source code in provide/foundation/logger/ratelimit/limiters.py
GlobalRateLimiter
¶
Global rate limiter singleton for Foundation's logging system. Manages per-logger and global rate limits.
Source code in provide/foundation/logger/ratelimit/limiters.py
Functions¶
configure
¶
configure(
global_rate: float | None = None,
global_capacity: float | None = None,
per_logger_rates: (
dict[str, tuple[float, float]] | None
) = None,
use_buffered: bool = False,
max_queue_size: int = 1000,
max_memory_mb: float | None = None,
overflow_policy: str = "drop_oldest",
) -> None
Configure the global rate limiter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_rate
|
float | None
|
Global logs per second limit |
None
|
global_capacity
|
float | None
|
Global burst capacity |
None
|
per_logger_rates
|
dict[str, tuple[float, float]] | None
|
Dict of logger_name -> (rate, capacity) tuples |
None
|
use_buffered
|
bool
|
Use buffered rate limiter with tracking |
False
|
max_queue_size
|
int
|
Maximum queue size for buffered limiter |
1000
|
max_memory_mb
|
float | None
|
Maximum memory for buffered limiter |
None
|
overflow_policy
|
str
|
What to do when queue is full |
'drop_oldest'
|
Source code in provide/foundation/logger/ratelimit/limiters.py
get_stats
¶
Get comprehensive rate limiting statistics.
Source code in provide/foundation/logger/ratelimit/limiters.py
is_allowed
¶
Check if a log from a specific logger is allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger_name
|
str
|
Name of the logger |
required |
item
|
Any | None
|
Optional item for buffered tracking |
None
|
Returns:
| Type | Description |
|---|---|
tuple[bool, str | None]
|
Tuple of (allowed, reason) where reason is set if denied |
Source code in provide/foundation/logger/ratelimit/limiters.py
SyncRateLimiter
¶
Synchronous token bucket rate limiter for controlling log output rates. Thread-safe implementation suitable for synchronous logging operations.
Initialize the rate limiter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
float
|
Maximum number of tokens (burst capacity) |
required |
refill_rate
|
float
|
Tokens refilled per second |
required |
Source code in provide/foundation/logger/ratelimit/limiters.py
Functions¶
get_stats
¶
Get rate limiter statistics.
Source code in provide/foundation/logger/ratelimit/limiters.py
is_allowed
¶
Check if a log message is allowed based on available tokens.
Returns:
| Type | Description |
|---|---|
bool
|
True if the log should be allowed, False if rate limited |