mod.rs 2.92 KB
Newer Older
1
2
3
//! Router implementations

use async_trait::async_trait;
4
5
6
7
8
9
use axum::{
    body::Body,
    extract::Request,
    http::{HeaderMap, StatusCode},
    response::{IntoResponse, Response},
};
10
11
use std::fmt::Debug;

12
use crate::protocols::spec::{ChatCompletionRequest, CompletionRequest, GenerateRequest};
13

14
pub mod factory;
15
pub mod header_utils;
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
pub mod pd_router;
pub mod pd_types;
pub mod router;

pub use factory::RouterFactory;

/// Worker management trait for administrative operations
///
/// This trait is separate from RouterTrait to allow Send futures
/// for use in service discovery and other background tasks
#[async_trait]
pub trait WorkerManagement: Send + Sync {
    /// Add a worker to the router
    async fn add_worker(&self, worker_url: &str) -> Result<String, String>;

    /// Remove a worker from the router
    fn remove_worker(&self, worker_url: &str);

    /// Get all worker URLs
    fn get_worker_urls(&self) -> Vec<String>;
}

/// Core trait for all router implementations
///
/// This trait provides a unified interface for routing requests,
/// regardless of whether it's a regular router or PD router.
42
#[async_trait]
43
44
45
pub trait RouterTrait: Send + Sync + Debug + WorkerManagement {
    /// Get a reference to self as Any for downcasting
    fn as_any(&self) -> &dyn std::any::Any;
46

47
    /// Route a health check request
48
    async fn health(&self, req: Request<Body>) -> Response;
49
50

    /// Route a health generate request
51
    async fn health_generate(&self, req: Request<Body>) -> Response;
52
53

    /// Get server information
54
    async fn get_server_info(&self, req: Request<Body>) -> Response;
55
56

    /// Get available models
57
    async fn get_models(&self, req: Request<Body>) -> Response;
58
59

    /// Get model information
60
    async fn get_model_info(&self, req: Request<Body>) -> Response;
61
62

    /// Route a generate request
63
64
    async fn route_generate(&self, headers: Option<&HeaderMap>, body: &GenerateRequest)
        -> Response;
65
66
67
68

    /// Route a chat completion request
    async fn route_chat(
        &self,
69
70
71
        headers: Option<&HeaderMap>,
        body: &ChatCompletionRequest,
    ) -> Response;
72
73
74
75

    /// Route a completion request
    async fn route_completion(
        &self,
76
77
78
        headers: Option<&HeaderMap>,
        body: &CompletionRequest,
    ) -> Response;
79
80

    /// Flush cache on all workers
81
    async fn flush_cache(&self) -> Response;
82
83

    /// Get worker loads (for monitoring)
84
    async fn get_worker_loads(&self) -> Response;
85
86
87
88
89
90
91
92
93
94

    /// Get router type name
    fn router_type(&self) -> &'static str;

    /// Check if this is a PD router
    fn is_pd_mode(&self) -> bool {
        self.router_type() == "pd"
    }

    /// Server liveness check - is the server process running
95
    fn liveness(&self) -> Response {
96
        // Simple liveness check - if we can respond, we're alive
97
        (StatusCode::OK, "OK").into_response()
98
99
100
    }

    /// Server readiness check - is the server ready to handle requests
101
    fn readiness(&self) -> Response;
102
}