factory.rs 2.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Factory for creating router instances

use super::{pd_router::PDRouter, router::Router, RouterTrait};
use crate::config::{PolicyConfig, RouterConfig, RoutingMode};
use crate::policies::PolicyFactory;

/// Factory for creating router instances based on configuration
pub struct RouterFactory;

impl RouterFactory {
    /// Create a router instance from configuration
    pub fn create_router(config: &RouterConfig) -> Result<Box<dyn RouterTrait>, String> {
        match &config.mode {
            RoutingMode::Regular { worker_urls } => {
                Self::create_regular_router(worker_urls, &config.policy, config)
            }
            RoutingMode::PrefillDecode {
                prefill_urls,
                decode_urls,
            } => Self::create_pd_router(prefill_urls, decode_urls, &config.policy, config),
        }
    }

    /// Create a regular router with injected policy
    fn create_regular_router(
        worker_urls: &[String],
        policy_config: &PolicyConfig,
        router_config: &RouterConfig,
    ) -> Result<Box<dyn RouterTrait>, String> {
        // Create policy
        let policy = PolicyFactory::create_from_config(policy_config);

        // Create regular router with injected policy
        let router = Router::new(
            worker_urls.to_vec(),
            policy,
            router_config.worker_startup_timeout_secs,
            router_config.worker_startup_check_interval_secs,
        )?;

        Ok(Box::new(router))
    }

    /// Create a PD router with injected policy
    fn create_pd_router(
        prefill_urls: &[(String, Option<u16>)],
        decode_urls: &[String],
        policy_config: &PolicyConfig,
        router_config: &RouterConfig,
    ) -> Result<Box<dyn RouterTrait>, String> {
        // Create policy directly from PolicyConfig
        // All policies now support PD mode through the select_worker_pair method
        let policy = PolicyFactory::create_from_config(policy_config);

        // Create PD router with injected policy
        let router = PDRouter::new(
            prefill_urls.to_vec(),
            decode_urls.to_vec(),
            policy,
            router_config.worker_startup_timeout_secs,
            router_config.worker_startup_check_interval_secs,
        )?;

        Ok(Box::new(router))
    }
}