Rate limiters¶
erpc.rate_limiters ¶
eRPC rate limiter configuration.
Provides dataclasses for configuring eRPC rate limiting budgets, rules, auto-tuning, and storage backends (memory or Redis).
Examples:
>>> from erpc.rate_limiters import (
... MemoryStore,
... RateLimiterConfig,
... RateLimitBudget,
... RateLimitRule,
... )
>>> budget = RateLimitBudget(
... id="global",
... rules=[RateLimitRule(method="*", max_count=1000, period="minute")],
... )
>>> config = RateLimiterConfig(store=MemoryStore(), budgets=[budget])
>>> config.to_dict()["store"]["driver"]
'memory'
RateLimitRule
dataclass
¶
A single rate limiting rule within a budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
RPC method name or |
required |
max_count
|
int
|
Maximum number of requests allowed in the period. |
required |
period
|
str
|
Time window — one of |
required |
per_ip
|
bool
|
Apply the limit per source IP address. |
False
|
per_user
|
bool
|
Apply the limit per authenticated user. |
False
|
per_network
|
bool
|
Apply the limit per network/chain. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> rule = RateLimitRule(method="eth_call", max_count=100, period="second")
>>> rule.to_dict()["maxCount"]
100
Source code in erpc/rate_limiters.py
__post_init__ ¶
Validate period value.
Source code in erpc/rate_limiters.py
to_dict ¶
Serialize to an eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with camelCase keys matching eRPC YAML schema. |
Source code in erpc/rate_limiters.py
AutoTuneConfig
dataclass
¶
Auto-tuner configuration for dynamic rate limit adjustment.
The auto-tuner monitors error rates and adjusts budget limits automatically within the configured bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
Whether auto-tuning is active. |
True
|
adjustment_period
|
str
|
How often to evaluate and adjust (e.g. |
'1m'
|
error_rate_threshold
|
float
|
Error rate above which limits are decreased (0.0-1.0). |
0.1
|
increase_factor
|
float
|
Multiplier when increasing budget (> 1.0). |
1.1
|
decrease_factor
|
float
|
Multiplier when decreasing budget (< 1.0). |
0.9
|
min_budget
|
int
|
Floor for auto-tuned budget value. |
0
|
max_budget
|
int
|
Ceiling for auto-tuned budget value. |
10000
|
Examples:
Source code in erpc/rate_limiters.py
to_dict ¶
Serialize to an eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with camelCase keys matching eRPC YAML schema. |
Source code in erpc/rate_limiters.py
RateLimitBudget
dataclass
¶
A named rate limit budget containing one or more rules.
Budgets are referenced by ID in eRPC project and network configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Unique budget identifier used for referencing in config. |
required |
rules
|
list[RateLimitRule]
|
List of rate limiting rules applied in this budget. |
required |
auto_tune
|
AutoTuneConfig | None
|
Optional auto-tuner configuration for dynamic adjustment. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> budget = RateLimitBudget(
... id="global",
... rules=[RateLimitRule(method="*", max_count=100, period="second")],
... )
>>> budget.to_dict()["id"]
'global'
Source code in erpc/rate_limiters.py
__post_init__ ¶
to_dict ¶
Serialize to an eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with camelCase keys matching eRPC YAML schema. |
Source code in erpc/rate_limiters.py
MemoryStore
dataclass
¶
In-memory rate limit store backend.
Uses the eRPC process memory for rate limit tracking. Simple and requires no external dependencies, but state is lost on restart.
Examples:
Source code in erpc/rate_limiters.py
to_dict ¶
Serialize to an eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with |
RedisStore
dataclass
¶
Redis-backed rate limit store.
Persists rate limit counters in Redis, enabling shared state across multiple eRPC instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Redis connection URI (e.g. |
required |
tls
|
bool
|
Enable TLS for the Redis connection. |
False
|
pool_size
|
int
|
Connection pool size. |
10
|
near_limit_ratio
|
float
|
Ratio (0.0-1.0) at which to flag approaching limits. |
0.8
|
cache_key_prefix
|
str
|
Prefix for all Redis keys used by the rate limiter. |
'erpc_rl_'
|
Examples:
Source code in erpc/rate_limiters.py
to_dict ¶
Serialize to an eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with |
Source code in erpc/rate_limiters.py
RateLimiterConfig
dataclass
¶
Top-level rate limiter configuration for eRPC.
Combines a storage backend with one or more named rate limit budgets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
RateLimitStore
|
Storage backend for rate limit counters. |
required |
budgets
|
list[RateLimitBudget]
|
List of named rate limit budgets. |
list()
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If duplicate budget IDs are found. |
Examples:
>>> config = RateLimiterConfig(
... store=MemoryStore(),
... budgets=[
... RateLimitBudget(
... id="default",
... rules=[RateLimitRule(method="*", max_count=100, period="second")],
... )
... ],
... )
>>> config.get_budget("default") is not None
True
Source code in erpc/rate_limiters.py
__post_init__ ¶
get_budget ¶
Look up a budget by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
budget_id
|
str
|
The unique budget identifier to search for. |
required |
Returns:
| Type | Description |
|---|---|
RateLimitBudget | None
|
The matching budget, or |
Source code in erpc/rate_limiters.py
to_dict ¶
Serialize to an eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with store and budgets configuration. |