aidale-layer
Built-in middleware layers for Aidale (logging, retry, caching, etc.).
Overview
aidale-layer provides composable middleware layers following the AOP (Aspect-Oriented Programming) pattern:
- LoggingLayer: Request/response logging with timing
- RetryLayer: Exponential backoff retry with jitter
- More layers coming soon (caching, rate limiting, etc.)
Available Layers
LoggingLayer
Logs all requests and responses with timing information:
use LoggingLayer;
let executor = builder
.layer
.finish;
Output example:
[AI Request] model=gpt-3.5-turbo messages=2
[AI Response] duration=1.2s tokens=150
RetryLayer
Automatic retry with exponential backoff:
use RetryLayer;
use Duration;
let executor = builder
.layer
.finish;
Features:
- Configurable max retries
- Exponential backoff with jitter
- Configurable delay bounds
- Only retries on transient errors (5xx, network errors)
Composition
Layers are composed in order from outermost to innermost:
let executor = builder
.layer // Executes first (outer)
.layer
.finish;
Execution flow:
Request → LoggingLayer → RetryLayer → Provider
Response ← LoggingLayer ← RetryLayer ← Provider
Zero-Cost Abstraction
Layers use compile-time composition with static dispatch:
- No virtual dispatch (no
dyn Trait) - No heap allocation for layer chain
- All composition resolved at compile time
- Type-level recursion for layer nesting
This means zero runtime overhead compared to manual implementation!
Usage
Via the main aidale crate:
[]
= { = "0.1", = ["layers"] }
Directly:
[]
= "0.1"
= "0.1"
Custom Layers
Implement the Layer trait from aidale-core:
use ;
use async_trait;
Planned Layers
- CachingLayer: Response caching with TTL
- RateLimitLayer: Request rate limiting
- CircuitBreakerLayer: Circuit breaker pattern
- MetricsLayer: Prometheus metrics export
- TracingLayer: OpenTelemetry distributed tracing
Related Crates
aidale-core- Core traits and runtimeaidale-provider- Provider implementationsaidale-plugin- Plugin system
License
MIT OR Apache-2.0