Swan Common
π Languages: English | δΈζ
Swan Common is the core component of the Swan HTTP library, providing shared type definitions, interceptor interfaces, retry mechanisms, and other foundational features.
π Core Features
- HTTP Type Definitions: Unified HTTP method, content type, and other type definitions
- Interceptor Interface: High-performance zero-copy interceptor trait definitions
- Retry Mechanism: Complete exponential backoff retry strategy implementation
- Parameter Parsing: Macro parameter parsing and validation logic
- State Management: Type support for application state injection
π¦ Installation
Add the following to your Cargo.toml:
[]
= "0.3"
= "0.1"
= "1.0"
π§ Main Components
HTTP Types
use ;
let method = Get;
let content_type = Json;
Interceptor Interface
use async_trait;
use SwanInterceptor;
use Cow;
;
Retry Strategy
use ;
use LitStr;
// Create exponential retry strategy
let policy = exponential; // 3 retries, base delay 100ms
// Parse retry configuration from string
let config_str: LitStr = parse_quote!;
let retry_config = parse?;
π Retry Mechanism Features
- Exponential Backoff Algorithm: Intelligent delay growth to avoid server overload
- Random Jitter: Prevent thundering herd effect by spreading retry times
- Idempotency Protection: Automatically detect safe retry conditions
- Flexible Configuration: Support both simplified and detailed configuration syntax
Supported Retry Configuration Formats
// Simplified format
"exponential(3, 100ms)" // 3 retries, base delay 100ms
"fixed(max_attempts=4, delay=1s)" // 4 retries, fixed delay 1 second
// Detailed format
"exponential(
max_attempts=5,
base_delay=200ms,
max_delay=30s,
exponential_base=2.0,
jitter_ratio=0.1,
idempotent_only=true
)"
β‘ Performance Features
- Zero-Copy Interceptors: Use
Cow<[u8]>to avoid unnecessary memory copying - Compile-Time Optimization: Retry strategies determined at compile time with zero runtime overhead
- Lightweight Structures:
RetryPolicymemory footprint β€ 64 bytes
π§ͺ Testing
Run tests:
π Documentation
Detailed API documentation:
π€ Use with Swan Macro
Swan Common is typically used with Swan Macro:
[]
= "0.3"
= "0.3"
π License
This project is licensed under the GPL-3.0 License. See the LICENSE file for details.