Failsafe¶
erpc.failsafe ¶
eRPC failsafe policy configuration.
Provides dataclasses for timeout, retry, hedge, and circuit breaker policies that map directly to eRPC's failsafe configuration surface. Includes preset configurations for common use cases.
Examples:
>>> from erpc.failsafe import FailsafeConfig, TimeoutPolicy, RetryPolicy
>>> config = FailsafeConfig(
... timeout=TimeoutPolicy(duration="30s"),
... retry=RetryPolicy(max_attempts=3),
... )
>>> config.to_dict()
{'timeout': {'duration': '30s'}, 'retry': {'maxAttempts': 3, ...}}
TimeoutPolicy
dataclass
¶
Timeout policy for eRPC requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duration
|
str
|
Timeout duration string (e.g. "30s", "100ms", "1m"). |
required |
Examples:
Source code in erpc/failsafe.py
to_dict ¶
Serialize to eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with camelCase keys matching eRPC config schema. |
RetryPolicy
dataclass
¶
Retry policy with exponential backoff and empty response handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_attempts
|
int
|
Maximum number of retry attempts. |
3
|
delay
|
str
|
Initial delay between retries (e.g. "1s"). |
'1s'
|
backoff_max_delay
|
str
|
Maximum backoff delay cap (e.g. "10s"). |
'10s'
|
backoff_factor
|
float
|
Multiplier for exponential backoff. |
2.0
|
jitter
|
str
|
Random jitter added to delay (e.g. "500ms"). |
'500ms'
|
empty_result_accept
|
bool | None
|
Whether to accept empty results as valid. |
None
|
empty_result_confidence
|
float | None
|
Confidence threshold for empty result detection. |
None
|
empty_result_max_attempts
|
int | None
|
Max retries specifically for empty results. |
None
|
Examples:
Source code in erpc/failsafe.py
to_dict ¶
Serialize to eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with camelCase keys. Empty result fields are omitted when None. |
Source code in erpc/failsafe.py
HedgePolicy
dataclass
¶
Hedge policy for parallel speculative requests.
Sends additional requests after a delay to reduce tail latency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delay
|
str
|
Time to wait before sending hedge request (e.g. "500ms"). |
required |
max_count
|
int
|
Maximum number of hedge requests to send. |
required |
Examples:
Source code in erpc/failsafe.py
to_dict ¶
Serialize to eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with camelCase keys matching eRPC config schema. |
CircuitBreakerPolicy
dataclass
¶
Circuit breaker policy to prevent cascading failures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
failure_threshold
|
int
|
Number of failures before opening the circuit. |
5
|
half_open_after
|
str
|
Duration before transitioning to half-open state. |
'60s'
|
success_threshold
|
int
|
Successes needed in half-open to close the circuit. |
3
|
Examples:
>>> CircuitBreakerPolicy().to_dict()
{'failureThreshold': 5, 'halfOpenAfter': '60s', 'successThreshold': 3}
Source code in erpc/failsafe.py
to_dict ¶
Serialize to eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with camelCase keys matching eRPC config schema. |
Source code in erpc/failsafe.py
FailsafeConfig
dataclass
¶
Composite failsafe configuration combining multiple policies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
TimeoutPolicy | None
|
Timeout policy, or None to disable. |
None
|
retry
|
RetryPolicy | None
|
Retry policy, or None to disable. |
None
|
hedge
|
HedgePolicy | None
|
Hedge policy, or None to disable. |
None
|
circuit_breaker
|
CircuitBreakerPolicy | None
|
Circuit breaker policy, or None to disable. |
None
|
Examples:
>>> config = FailsafeConfig(
... timeout=TimeoutPolicy(duration="30s"),
... retry=RetryPolicy(),
... )
>>> sorted(config.to_dict().keys())
['retry', 'timeout']
Source code in erpc/failsafe.py
to_dict ¶
Serialize to eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with only non-None policies included. |
Source code in erpc/failsafe.py
MethodFailsafeConfig
dataclass
¶
Per-method failsafe policy override.
Allows different failsafe settings for specific RPC methods or finality states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
match_method
|
str
|
Method name or glob pattern (e.g. "eth_*", "eth_getLogs"). |
required |
match_finality
|
str | None
|
Finality state filter (finalized, unfinalized, realtime, unknown). |
None
|
failsafe
|
FailsafeConfig
|
Failsafe configuration to apply for matching requests. |
FailsafeConfig()
|
Examples:
>>> cfg = MethodFailsafeConfig(
... match_method="eth_getLogs",
... match_finality="finalized",
... failsafe=FailsafeConfig(timeout=TimeoutPolicy(duration="60s")),
... )
>>> cfg.to_dict()["matchMethod"]
'eth_getLogs'
Source code in erpc/failsafe.py
to_dict ¶
Serialize to eRPC-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with matchMethod, optional matchFinality, and failsafe config. |
Source code in erpc/failsafe.py
FailsafePresets ¶
Pre-built failsafe configurations for common use cases.
Examples:
Source code in erpc/failsafe.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
high_performance_defi
staticmethod
¶
Failsafe preset optimized for DeFi applications.
Low timeouts, aggressive hedging, and fast retries for latency-sensitive trading and DeFi operations.
Returns:
| Type | Description |
|---|---|
FailsafeConfig
|
FailsafeConfig tuned for low-latency DeFi workloads. |
Source code in erpc/failsafe.py
indexer
staticmethod
¶
Failsafe preset optimized for blockchain indexing.
Generous timeouts and retries for reliability over large batch queries. No hedging to avoid unnecessary duplicate load.
Returns:
| Type | Description |
|---|---|
FailsafeConfig
|
FailsafeConfig tuned for reliable indexing workloads. |
Source code in erpc/failsafe.py
finality_based
staticmethod
¶
Failsafe presets that vary by block finality state.
Returns different policies for finalized vs unfinalized data, reflecting that finalized data is immutable and can be cached/retried more aggressively.
Returns:
| Type | Description |
|---|---|
list[MethodFailsafeConfig]
|
List of MethodFailsafeConfig entries for different finality states. |