lib.rs 2.65 KB
Newer Older
1
2
use pyo3::prelude::*;
pub mod router;
3
pub mod server;
4
pub mod tree;
5

6
7
8
9
10
#[pyclass(eq)]
#[derive(Clone, PartialEq)]
pub enum PolicyType {
    Random,
    RoundRobin,
11
    CacheAware,
12
13
}

14
15
16
17
18
#[pyclass]
struct Router {
    host: String,
    port: u16,
    worker_urls: Vec<String>,
19
    policy: PolicyType,
20
    cache_threshold: f32,
21
22
    balance_abs_threshold: usize,
    balance_rel_threshold: f32,
23
24
    eviction_interval_secs: u64,
    max_tree_size: usize,
25
    verbose: bool,
26
27
28
29
30
}

#[pymethods]
impl Router {
    #[new]
31
32
33
34
35
    #[pyo3(signature = (
        worker_urls,
        policy = PolicyType::RoundRobin,
        host = String::from("127.0.0.1"),
        port = 3001,
36
        cache_threshold = 0.50,
37
38
        balance_abs_threshold = 32,
        balance_rel_threshold = 1.0001,
39
        eviction_interval_secs = 60,
40
41
        max_tree_size = 2usize.pow(24),
        verbose = false
42
43
44
45
46
47
    ))]
    fn new(
        worker_urls: Vec<String>,
        policy: PolicyType,
        host: String,
        port: u16,
48
        cache_threshold: f32,
49
50
        balance_abs_threshold: usize,
        balance_rel_threshold: f32,
51
52
        eviction_interval_secs: u64,
        max_tree_size: usize,
53
        verbose: bool,
54
55
    ) -> PyResult<Self> {
        Ok(Router {
56
57
58
            host,
            port,
            worker_urls,
59
            policy,
60
            cache_threshold,
61
62
            balance_abs_threshold,
            balance_rel_threshold,
63
64
            eviction_interval_secs,
            max_tree_size,
65
            verbose,
66
        })
67
68
69
    }

    fn start(&self) -> PyResult<()> {
70
71
72
        let policy_config = match &self.policy {
            PolicyType::Random => router::PolicyConfig::RandomConfig,
            PolicyType::RoundRobin => router::PolicyConfig::RoundRobinConfig,
73
74
            PolicyType::CacheAware => router::PolicyConfig::CacheAwareConfig {
                cache_threshold: self.cache_threshold,
75
76
                balance_abs_threshold: self.balance_abs_threshold,
                balance_rel_threshold: self.balance_rel_threshold,
77
78
                eviction_interval_secs: self.eviction_interval_secs,
                max_tree_size: self.max_tree_size,
79
80
            },
        };
81
82

        actix_web::rt::System::new().block_on(async move {
83
84
85
86
87
88
89
90
91
            server::startup(server::ServerConfig {
                host: self.host.clone(),
                port: self.port,
                worker_urls: self.worker_urls.clone(),
                policy_config,
                verbose: self.verbose,
            })
            .await
            .unwrap();
92
93
94
95
96
97
98
        });

        Ok(())
    }
}

#[pymodule]
99
fn sglang_router_rs(m: &Bound<'_, PyModule>) -> PyResult<()> {
100
    m.add_class::<PolicyType>()?;
101
102
    m.add_class::<Router>()?;
    Ok(())
103
}