factory.rs 4.93 KB
Newer Older
1
2
//! Factory for creating router instances

3
4
5
6
use super::{
    http::{pd_router::PDRouter, router::Router},
    RouterTrait,
};
7
use crate::config::{PolicyConfig, RoutingMode};
8
use crate::policies::PolicyFactory;
9
10
use crate::server::AppContext;
use std::sync::Arc;
11
12
13
14
15

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

impl RouterFactory {
16
    /// Create a router instance from application context
17
    pub async fn create_router(ctx: &Arc<AppContext>) -> Result<Box<dyn RouterTrait>, String> {
18
19
20
21
22
        // Check if IGW mode is enabled
        if ctx.router_config.enable_igw {
            return Self::create_igw_router(ctx).await;
        }

23
24
25
        // TODO: Add gRPC mode check here when implementing gRPC support

        // Default to HTTP proxy mode
26
        match &ctx.router_config.mode {
27
            RoutingMode::Regular { worker_urls } => {
28
                Self::create_regular_router(worker_urls, &ctx.router_config.policy, ctx).await
29
30
31
32
            }
            RoutingMode::PrefillDecode {
                prefill_urls,
                decode_urls,
33
34
                prefill_policy,
                decode_policy,
35
36
37
38
39
40
41
42
43
44
45
            } => {
                Self::create_pd_router(
                    prefill_urls,
                    decode_urls,
                    prefill_policy.as_ref(),
                    decode_policy.as_ref(),
                    &ctx.router_config.policy,
                    ctx,
                )
                .await
            }
46
47
48
49
        }
    }

    /// Create a regular router with injected policy
50
    async fn create_regular_router(
51
52
        worker_urls: &[String],
        policy_config: &PolicyConfig,
53
        ctx: &Arc<AppContext>,
54
55
56
57
    ) -> Result<Box<dyn RouterTrait>, String> {
        // Create policy
        let policy = PolicyFactory::create_from_config(policy_config);

58
        // Create regular router with injected policy and client
59
60
61
        let router = Router::new(
            worker_urls.to_vec(),
            policy,
62
63
64
65
66
            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(),
67
            ctx.router_config.retry.clone(),
68
            ctx.router_config.circuit_breaker.clone(),
69
            ctx.router_config.health_check.clone(),
70
71
        )
        .await?;
72
73
74
75
76

        Ok(Box::new(router))
    }

    /// Create a PD router with injected policy
77
    async fn create_pd_router(
78
79
        prefill_urls: &[(String, Option<u16>)],
        decode_urls: &[String],
80
81
82
        prefill_policy_config: Option<&PolicyConfig>,
        decode_policy_config: Option<&PolicyConfig>,
        main_policy_config: &PolicyConfig,
83
        ctx: &Arc<AppContext>,
84
    ) -> Result<Box<dyn RouterTrait>, String> {
85
86
87
88
89
        // 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));
90

91
        // Create PD router with separate policies and client
92
93
94
        let router = PDRouter::new(
            prefill_urls.to_vec(),
            decode_urls.to_vec(),
95
96
            prefill_policy,
            decode_policy,
97
98
99
            ctx.client.clone(),
            ctx.router_config.worker_startup_timeout_secs,
            ctx.router_config.worker_startup_check_interval_secs,
100
            ctx.router_config.retry.clone(),
101
            ctx.router_config.circuit_breaker.clone(),
102
            ctx.router_config.health_check.clone(),
103
104
        )
        .await?;
105
106
107

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

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    /// Create a gRPC router with injected policy
    pub async fn create_grpc_router(
        _worker_urls: &[String],
        _policy_config: &PolicyConfig,
        _ctx: &Arc<AppContext>,
    ) -> Result<Box<dyn RouterTrait>, String> {
        // For now, return an error as gRPC router is not yet implemented
        Err("gRPC router is not yet implemented".to_string())
    }

    /// Create a gRPC PD router (placeholder for now)
    pub async fn create_grpc_pd_router(
        _prefill_urls: &[(String, Option<u16>)],
        _decode_urls: &[String],
        _prefill_policy_config: Option<&PolicyConfig>,
        _decode_policy_config: Option<&PolicyConfig>,
        _main_policy_config: &PolicyConfig,
        _ctx: &Arc<AppContext>,
    ) -> Result<Box<dyn RouterTrait>, String> {
        // For now, return an error as gRPC PD router is not yet implemented
        Err("gRPC PD router is not yet implemented".to_string())
    }

132
133
134
135
136
    /// Create an IGW router (placeholder for future implementation)
    async fn create_igw_router(_ctx: &Arc<AppContext>) -> Result<Box<dyn RouterTrait>, String> {
        // For now, return an error indicating IGW is not yet implemented
        Err("IGW mode is not yet implemented".to_string())
    }
137
}