pub struct Router { /* private fields */ }Expand description
Router for intelligent provider selection.
This struct implements the routing logic based on the configured strategy and maintains provider performance statistics for informed decision making.
Implementations§
Source§impl Router
impl Router
Sourcepub fn new(strategy: RoutingStrategy) -> Self
pub fn new(strategy: RoutingStrategy) -> Self
Sourcepub fn select_provider(
&self,
providers: &[String],
context: &RoutingContext,
) -> Option<ProviderSelection>
pub fn select_provider( &self, providers: &[String], context: &RoutingContext, ) -> Option<ProviderSelection>
Select a provider based on the current routing strategy.
§Arguments
providers- List of available provider identifierscontext- Routing context with request information
§Returns
Returns a provider selection if one is found, or None if no provider
can be selected based on the current strategy and context.
§Examples
use ultrafast_models_sdk::routing::{Router, RoutingStrategy, RoutingContext};
use std::collections::HashMap;
let router = Router::new(RoutingStrategy::Single);
let providers = vec!["openai".to_string(), "anthropic".to_string()];
let context = RoutingContext {
model: Some("gpt-4".to_string()),
user_region: None,
request_size: 1000,
estimated_tokens: 500,
user_id: None,
metadata: HashMap::new(),
};
if let Some(selection) = router.select_provider(&providers, &context) {
println!("Selected: {}", selection.provider_id);
}Sourcepub fn update_stats(
&mut self,
provider_id: &str,
success: bool,
latency_ms: u64,
)
pub fn update_stats( &mut self, provider_id: &str, success: bool, latency_ms: u64, )
Update provider statistics after a request.
§Arguments
provider_id- Identifier of the providersuccess- Whether the request was successfullatency_ms- Response latency in milliseconds
§Examples
use ultrafast_models_sdk::routing::Router;
let mut router = Router::new(RoutingStrategy::Single);
// Update stats after a successful request
router.update_stats("openai", true, 150);
// Update stats after a failed request
router.update_stats("anthropic", false, 500);Auto Trait Implementations§
impl Freeze for Router
impl RefUnwindSafe for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl UnwindSafe for Router
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more