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

3
4
use std::sync::Arc;

5
use super::{
6
    grpc::{pd_router::GrpcPDRouter, router::GrpcRouter},
7
8
    http::{pd_router::PDRouter, router::Router},
    openai::OpenAIRouter,
9
10
    RouterTrait,
};
11
use crate::{
12
13
    config::{PolicyConfig, RoutingMode},
    core::ConnectionMode,
14
15
16
    policies::PolicyFactory,
    server::AppContext,
};
17
18
19
20
21

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

impl RouterFactory {
22
    /// Create a router instance from application context
23
    pub async fn create_router(ctx: &Arc<AppContext>) -> Result<Box<dyn RouterTrait>, String> {
24
        match ctx.router_config.connection_mode {
25
            ConnectionMode::Grpc { .. } => match &ctx.router_config.mode {
26
27
28
29
30
31
32
33
34
35
36
37
38
                RoutingMode::Regular { .. } => Self::create_grpc_router(ctx).await,
                RoutingMode::PrefillDecode {
                    prefill_policy,
                    decode_policy,
                    ..
                } => {
                    Self::create_grpc_pd_router(
                        prefill_policy.as_ref(),
                        decode_policy.as_ref(),
                        &ctx.router_config.policy,
                        ctx,
                    )
                    .await
39
                }
40
41
                RoutingMode::OpenAI { .. } => {
                    Err("OpenAI mode requires HTTP connection_mode".to_string())
42
                }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
            },
            ConnectionMode::Http => match &ctx.router_config.mode {
                RoutingMode::Regular { .. } => Self::create_regular_router(ctx).await,
                RoutingMode::PrefillDecode {
                    prefill_policy,
                    decode_policy,
                    ..
                } => {
                    Self::create_pd_router(
                        prefill_policy.as_ref(),
                        decode_policy.as_ref(),
                        &ctx.router_config.policy,
                        ctx,
                    )
                    .await
                }
59
                RoutingMode::OpenAI { worker_urls } => {
60
61
62
                    Self::create_openai_router(worker_urls.clone(), ctx).await
                }
            },
63
64
65
        }
    }

66
67
    /// Create a regular router
    pub async fn create_regular_router(
68
        ctx: &Arc<AppContext>,
69
    ) -> Result<Box<dyn RouterTrait>, String> {
70
        let router = Router::new(ctx).await?;
71
72
73
74
75

        Ok(Box::new(router))
    }

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

87
88
89
        ctx.policy_registry.set_prefill_policy(prefill_policy);
        ctx.policy_registry.set_decode_policy(decode_policy);

90
        let router = PDRouter::new(ctx).await?;
91
92
93

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

95
    /// Create a gRPC router with injected policy
96
97
    pub async fn create_grpc_router(ctx: &Arc<AppContext>) -> Result<Box<dyn RouterTrait>, String> {
        let router = GrpcRouter::new(ctx).await?;
98
99

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

102
    /// Create a gRPC PD router with tokenizer and worker configuration
103
    pub async fn create_grpc_pd_router(
104
105
106
107
        prefill_policy_config: Option<&PolicyConfig>,
        decode_policy_config: Option<&PolicyConfig>,
        main_policy_config: &PolicyConfig,
        ctx: &Arc<AppContext>,
108
    ) -> Result<Box<dyn RouterTrait>, String> {
109
110
111
112
113
        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));

114
115
116
        ctx.policy_registry.set_prefill_policy(prefill_policy);
        ctx.policy_registry.set_decode_policy(decode_policy);
        let router = GrpcPDRouter::new(ctx).await?;
117
118

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

121
122
123
124
125
    /// Create an OpenAI router
    async fn create_openai_router(
        worker_urls: Vec<String>,
        ctx: &Arc<AppContext>,
    ) -> Result<Box<dyn RouterTrait>, String> {
126
127
128
        if worker_urls.is_empty() {
            return Err("OpenAI mode requires at least one worker URL".to_string());
        }
129

130
        let router = OpenAIRouter::new(
131
            worker_urls,
132
133
            Some(ctx.router_config.circuit_breaker.clone()),
            ctx.response_storage.clone(),
134
            ctx.conversation_storage.clone(),
135
            ctx.conversation_item_storage.clone(),
136
137
        )
        .await?;
138
139
140

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