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

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

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

impl RouterFactory {
18
    /// Create a router instance from application context
19
    pub async fn create_router(ctx: &Arc<AppContext>) -> Result<Box<dyn RouterTrait>, String> {
20
        match ctx.router_config.connection_mode {
21
22
23
24
25
26
27
28
29
30
31
32
33
34
            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
35
                }
36
37
                RoutingMode::OpenAI { .. } => {
                    Err("OpenAI mode requires HTTP connection_mode".to_string())
38
                }
39
40
41
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
                }
                RoutingMode::OpenAI { worker_urls, .. } => {
                    Self::create_openai_router(worker_urls.clone(), ctx).await
                }
            },
59
60
61
        }
    }

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

        Ok(Box::new(router))
    }

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

83
84
85
        ctx.policy_registry.set_prefill_policy(prefill_policy);
        ctx.policy_registry.set_decode_policy(decode_policy);

86
        let router = PDRouter::new(ctx).await?;
87
88
89

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

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

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

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

110
111
112
        ctx.policy_registry.set_prefill_policy(prefill_policy);
        ctx.policy_registry.set_decode_policy(decode_policy);
        let router = GrpcPDRouter::new(ctx).await?;
113
114

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

117
118
119
120
121
122
123
124
125
126
    /// Create an OpenAI router
    async fn create_openai_router(
        worker_urls: Vec<String>,
        ctx: &Arc<AppContext>,
    ) -> Result<Box<dyn RouterTrait>, String> {
        let base_url = worker_urls
            .first()
            .cloned()
            .ok_or_else(|| "OpenAI mode requires at least one worker URL".to_string())?;

127
128
129
130
131
132
        let router = OpenAIRouter::new(
            base_url,
            Some(ctx.router_config.circuit_breaker.clone()),
            ctx.response_storage.clone(),
        )
        .await?;
133
134
135

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