pub trait RoutingStrategy: Send + Sync {
// Required method
fn select_provider<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
providers: &'life1 [ProviderConfig],
request: &'life2 CompletionRequest,
) -> Pin<Box<dyn Future<Output = Result<usize>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait;
// Provided methods
fn report_success<'life0, 'async_trait>(
&'life0 self,
provider_index: usize,
latency: Duration,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn report_failure<'life0, 'async_trait>(
&'life0 self,
provider_index: usize,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn name(&self) -> &str { ... }
}Expand description
Trait for routing strategies.
Implementations define how requests are routed across multiple providers:
- Round-robin: Distribute requests evenly
- Priority: Try providers in order
- Latency-based: Route to fastest provider
- Load-balancing: Consider provider load
- Cost-optimized: Route to cheapest provider
§Example Implementation
use simple_agent_type::router::RoutingStrategy;
use simple_agent_type::config::ProviderConfig;
use simple_agent_type::request::CompletionRequest;
use simple_agent_type::message::Message;
use simple_agent_type::error::{Result, SimpleAgentsError};
use async_trait::async_trait;
use std::sync::atomic::{AtomicUsize, Ordering};
struct RoundRobinStrategy {
counter: AtomicUsize,
}
#[async_trait]
impl RoutingStrategy for RoundRobinStrategy {
async fn select_provider(
&self,
providers: &[ProviderConfig],
_request: &CompletionRequest,
) -> Result<usize> {
if providers.is_empty() {
return Err(SimpleAgentsError::Routing("no providers".to_string()));
}
let index = self.counter.fetch_add(1, Ordering::Relaxed);
Ok(index % providers.len())
}
}
let strategy = RoundRobinStrategy {
counter: AtomicUsize::new(0),
};
let providers = vec![
ProviderConfig::new("p1", "http://example.com"),
ProviderConfig::new("p2", "http://example.com"),
];
let request = CompletionRequest::builder()
.model("gpt-4")
.message(Message::user("Hello!"))
.build()
.unwrap();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let index = strategy.select_provider(&providers, &request).await.unwrap();
assert!(index < providers.len());
});Required Methods§
Sourcefn select_provider<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
providers: &'life1 [ProviderConfig],
request: &'life2 CompletionRequest,
) -> Pin<Box<dyn Future<Output = Result<usize>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn select_provider<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
providers: &'life1 [ProviderConfig],
request: &'life2 CompletionRequest,
) -> Pin<Box<dyn Future<Output = Result<usize>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Provided Methods§
Sourcefn report_success<'life0, 'async_trait>(
&'life0 self,
provider_index: usize,
latency: Duration,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn report_success<'life0, 'async_trait>(
&'life0 self,
provider_index: usize,
latency: Duration,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Report successful request completion.
Used by latency-based and adaptive routing strategies to track provider performance.
§Arguments
provider_index: Index of the provider that succeededlatency: Request duration
Sourcefn report_failure<'life0, 'async_trait>(
&'life0 self,
provider_index: usize,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn report_failure<'life0, 'async_trait>(
&'life0 self,
provider_index: usize,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Report request failure.
Used by reliability-tracking routing strategies.
§Arguments
provider_index: Index of the provider that failed