mod.rs 2.9 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
13
use crate::openai_api_types::{ChatCompletionRequest, CompletionRequest, GenerateRequest};

14
15
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
pub mod factory;
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.
41
#[async_trait]
42
43
44
pub trait RouterTrait: Send + Sync + Debug + WorkerManagement {
    /// Get a reference to self as Any for downcasting
    fn as_any(&self) -> &dyn std::any::Any;
45

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

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

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

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

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

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

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

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

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

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

    /// 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
94
    fn liveness(&self) -> Response {
95
        // Simple liveness check - if we can respond, we're alive
96
        (StatusCode::OK, "OK").into_response()
97
98
99
    }

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