[router] add postgres databases data connector (#12218)

This commit is contained in:
rongfu.leng
2025-11-10 16:51:50 -08:00
committed by GitHub
parent 012bfc4fdc
commit 2fe4e69fca
12 changed files with 989 additions and 86 deletions
+44
View File
@@ -45,6 +45,7 @@ pub enum HistoryBackendType {
Memory,
None,
Oracle,
Postgres,
}
#[pyclass]
@@ -139,6 +140,34 @@ impl PyOracleConfig {
}
}
#[pyclass]
#[derive(Debug, Clone, PartialEq)]
pub struct PyPostgresConfig {
#[pyo3(get, set)]
pub db_url: Option<String>,
#[pyo3(get, set)]
pub pool_max: usize,
}
#[pymethods]
impl PyPostgresConfig {
#[new]
#[pyo3(signature = (db_url = None,pool_max = 16,))]
fn new(db_url: Option<String>, pool_max: usize) -> PyResult<Self> {
Ok(PyPostgresConfig { db_url, pool_max })
}
}
impl PyPostgresConfig {
fn to_config_postgres(&self) -> config::PostgresConfig {
config::PostgresConfig {
db_url: self.db_url.clone().unwrap_or_default(),
pool_max: self.pool_max,
}
}
}
#[pyclass]
#[derive(Debug, Clone, PartialEq)]
struct Router {
@@ -212,6 +241,7 @@ struct Router {
backend: BackendType,
history_backend: HistoryBackendType,
oracle_config: Option<PyOracleConfig>,
postgres_config: Option<PyPostgresConfig>,
client_cert_path: Option<String>,
client_key_path: Option<String>,
ca_cert_paths: Vec<String>,
@@ -305,6 +335,7 @@ impl Router {
HistoryBackendType::Memory => config::HistoryBackend::Memory,
HistoryBackendType::None => config::HistoryBackend::None,
HistoryBackendType::Oracle => config::HistoryBackend::Oracle,
HistoryBackendType::Postgres => config::HistoryBackend::Postgres,
};
let oracle = if matches!(self.history_backend, HistoryBackendType::Oracle) {
@@ -315,6 +346,14 @@ impl Router {
None
};
let postgres_config = if matches!(self.history_backend, HistoryBackendType::Postgres) {
self.postgres_config
.as_ref()
.map(|cfg| cfg.to_config_postgres())
} else {
None
};
config::RouterConfig::builder()
.mode(mode)
.policy(policy)
@@ -367,6 +406,7 @@ impl Router {
.maybe_tokenizer_path(self.tokenizer_path.as_ref())
.maybe_chat_template(self.chat_template.as_ref())
.maybe_oracle(oracle)
.maybe_postgres(postgres_config)
.maybe_reasoning_parser(self.reasoning_parser.as_ref())
.maybe_tool_call_parser(self.tool_call_parser.as_ref())
.maybe_mcp_config_path(self.mcp_config_path.as_ref())
@@ -455,6 +495,7 @@ impl Router {
backend = BackendType::Sglang,
history_backend = HistoryBackendType::Memory,
oracle_config = None,
postgres_config = None,
client_cert_path = None,
client_key_path = None,
ca_cert_paths = vec![],
@@ -529,6 +570,7 @@ impl Router {
backend: BackendType,
history_backend: HistoryBackendType,
oracle_config: Option<PyOracleConfig>,
postgres_config: Option<PyPostgresConfig>,
client_cert_path: Option<String>,
client_key_path: Option<String>,
ca_cert_paths: Vec<String>,
@@ -617,6 +659,7 @@ impl Router {
backend,
history_backend,
oracle_config,
postgres_config,
client_cert_path,
client_key_path,
ca_cert_paths,
@@ -687,6 +730,7 @@ fn sglang_router_rs(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<BackendType>()?;
m.add_class::<HistoryBackendType>()?;
m.add_class::<PyOracleConfig>()?;
m.add_class::<PyPostgresConfig>()?;
m.add_class::<Router>()?;
Ok(())
}