2025-07-18 14:24:24 -07:00
|
|
|
//! Factory for creating router instances
|
|
|
|
|
|
|
|
|
|
use super::{pd_router::PDRouter, router::Router, RouterTrait};
|
2025-08-02 19:16:47 -07:00
|
|
|
use crate::config::{PolicyConfig, RoutingMode};
|
2025-07-18 14:24:24 -07:00
|
|
|
use crate::policies::PolicyFactory;
|
2025-08-02 19:16:47 -07:00
|
|
|
use crate::server::AppContext;
|
|
|
|
|
use std::sync::Arc;
|
2025-07-18 14:24:24 -07:00
|
|
|
|
|
|
|
|
/// Factory for creating router instances based on configuration
|
|
|
|
|
pub struct RouterFactory;
|
|
|
|
|
|
|
|
|
|
impl RouterFactory {
|
2025-08-02 19:16:47 -07:00
|
|
|
/// Create a router instance from application context
|
|
|
|
|
pub fn create_router(ctx: &Arc<AppContext>) -> Result<Box<dyn RouterTrait>, String> {
|
|
|
|
|
match &ctx.router_config.mode {
|
2025-07-18 14:24:24 -07:00
|
|
|
RoutingMode::Regular { worker_urls } => {
|
2025-08-02 19:16:47 -07:00
|
|
|
Self::create_regular_router(worker_urls, &ctx.router_config.policy, ctx)
|
2025-07-18 14:24:24 -07:00
|
|
|
}
|
|
|
|
|
RoutingMode::PrefillDecode {
|
|
|
|
|
prefill_urls,
|
|
|
|
|
decode_urls,
|
2025-07-27 00:39:20 -07:00
|
|
|
prefill_policy,
|
|
|
|
|
decode_policy,
|
|
|
|
|
} => Self::create_pd_router(
|
|
|
|
|
prefill_urls,
|
|
|
|
|
decode_urls,
|
|
|
|
|
prefill_policy.as_ref(),
|
|
|
|
|
decode_policy.as_ref(),
|
2025-08-02 19:16:47 -07:00
|
|
|
&ctx.router_config.policy,
|
|
|
|
|
ctx,
|
2025-07-27 00:39:20 -07:00
|
|
|
),
|
2025-07-18 14:24:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a regular router with injected policy
|
|
|
|
|
fn create_regular_router(
|
|
|
|
|
worker_urls: &[String],
|
|
|
|
|
policy_config: &PolicyConfig,
|
2025-08-02 19:16:47 -07:00
|
|
|
ctx: &Arc<AppContext>,
|
2025-07-18 14:24:24 -07:00
|
|
|
) -> Result<Box<dyn RouterTrait>, String> {
|
|
|
|
|
// Create policy
|
|
|
|
|
let policy = PolicyFactory::create_from_config(policy_config);
|
|
|
|
|
|
2025-08-02 19:16:47 -07:00
|
|
|
// Create regular router with injected policy and client
|
2025-07-18 14:24:24 -07:00
|
|
|
let router = Router::new(
|
|
|
|
|
worker_urls.to_vec(),
|
|
|
|
|
policy,
|
2025-08-02 19:16:47 -07:00
|
|
|
ctx.client.clone(),
|
|
|
|
|
ctx.router_config.worker_startup_timeout_secs,
|
|
|
|
|
ctx.router_config.worker_startup_check_interval_secs,
|
|
|
|
|
ctx.router_config.dp_aware,
|
|
|
|
|
ctx.router_config.api_key.clone(),
|
2025-07-18 14:24:24 -07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
Ok(Box::new(router))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a PD router with injected policy
|
|
|
|
|
fn create_pd_router(
|
|
|
|
|
prefill_urls: &[(String, Option<u16>)],
|
|
|
|
|
decode_urls: &[String],
|
2025-07-27 00:39:20 -07:00
|
|
|
prefill_policy_config: Option<&PolicyConfig>,
|
|
|
|
|
decode_policy_config: Option<&PolicyConfig>,
|
|
|
|
|
main_policy_config: &PolicyConfig,
|
2025-08-02 19:16:47 -07:00
|
|
|
ctx: &Arc<AppContext>,
|
2025-07-18 14:24:24 -07:00
|
|
|
) -> Result<Box<dyn RouterTrait>, String> {
|
2025-07-27 00:39:20 -07:00
|
|
|
// Create policies - use specific policies if provided, otherwise fall back to main policy
|
|
|
|
|
let prefill_policy =
|
|
|
|
|
PolicyFactory::create_from_config(prefill_policy_config.unwrap_or(main_policy_config));
|
|
|
|
|
let decode_policy =
|
|
|
|
|
PolicyFactory::create_from_config(decode_policy_config.unwrap_or(main_policy_config));
|
2025-07-18 14:24:24 -07:00
|
|
|
|
2025-08-02 19:16:47 -07:00
|
|
|
// Create PD router with separate policies and client
|
2025-07-18 14:24:24 -07:00
|
|
|
let router = PDRouter::new(
|
|
|
|
|
prefill_urls.to_vec(),
|
|
|
|
|
decode_urls.to_vec(),
|
2025-07-27 00:39:20 -07:00
|
|
|
prefill_policy,
|
|
|
|
|
decode_policy,
|
2025-08-02 19:16:47 -07:00
|
|
|
ctx.client.clone(),
|
|
|
|
|
ctx.router_config.worker_startup_timeout_secs,
|
|
|
|
|
ctx.router_config.worker_startup_check_interval_secs,
|
2025-07-18 14:24:24 -07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
Ok(Box::new(router))
|
|
|
|
|
}
|
|
|
|
|
}
|