factory.rs 4.82 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
12
13
14
15
use crate::{
    config::{ConnectionMode, PolicyConfig, RoutingMode},
    policies::PolicyFactory,
    server::AppContext,
};
16
17
18
19
20

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

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

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

        Ok(Box::new(router))
    }

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

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

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

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

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

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

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

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

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

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

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

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