mod.rs 3.15 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
12
use reqwest::Client;
use std::fmt::Debug;

13
14
use crate::openai_api_types::{ChatCompletionRequest, CompletionRequest, GenerateRequest};

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

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

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

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

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

    /// Get model information
61
    async fn get_model_info(&self, client: &Client, req: Request<Body>) -> Response;
62
63
64
65
66

    /// Route a generate request
    async fn route_generate(
        &self,
        client: &Client,
67
68
69
        headers: Option<&HeaderMap>,
        body: &GenerateRequest,
    ) -> Response;
70
71
72
73
74

    /// Route a chat completion request
    async fn route_chat(
        &self,
        client: &Client,
75
76
77
        headers: Option<&HeaderMap>,
        body: &ChatCompletionRequest,
    ) -> Response;
78
79
80
81
82

    /// Route a completion request
    async fn route_completion(
        &self,
        client: &Client,
83
84
85
        headers: Option<&HeaderMap>,
        body: &CompletionRequest,
    ) -> Response;
86
87

    /// Flush cache on all workers
88
    async fn flush_cache(&self, client: &Client) -> Response;
89
90

    /// Get worker loads (for monitoring)
91
    async fn get_worker_loads(&self, client: &Client) -> Response;
92
93
94
95
96
97
98
99
100
101

    /// 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
102
    fn liveness(&self) -> Response {
103
        // Simple liveness check - if we can respond, we're alive
104
        (StatusCode::OK, "OK").into_response()
105
106
107
    }

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