Skip to content

Commit

Permalink
Allow specifying db path and pool size
Browse files Browse the repository at this point in the history
  • Loading branch information
akolov committed Oct 23, 2024
1 parent 49e4d68 commit bdddd4d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
91 changes: 87 additions & 4 deletions packages/duckdb-server-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/duckdb-server-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async-trait = "0.1.83"
axum = { version = "0.7", features = ["http1", "http2", "ws", "json", "tokio", "tracing", "macros"] }
axum-server = { version = "0.7", features = ["tls-rustls"] }
axum-server-dual-protocol = "0.7"
clap = { version = "4.5.20", features = ["derive"] }
duckdb = { version = "1", features = ["bundled", "csv", "json", "parquet", "url", "r2d2"] }
futures = "0.3"
listenfd = "1.0"
Expand Down
6 changes: 4 additions & 2 deletions packages/duckdb-server-rust/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ async fn handle_post(
query::handle(&state, params).await
}

pub fn app() -> Result<Router> {
pub fn app(db_path: &str, pool_size: u32) -> Result<Router> {
let effective_pool_size = if db_path == ":memory:" { 1 } else { pool_size };

// Database and state setup
let db = ConnectionPool::new(":memory:", 1)?; // TODO: we can only use one connection since temp tables are scoped per connection
let db = ConnectionPool::new(db_path, effective_pool_size)?;
let cache = lru::LruCache::new(1000.try_into()?);

let state = Arc::new(AppState {
Expand Down
19 changes: 18 additions & 1 deletion packages/duckdb-server-rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use axum_server::tls_rustls::RustlsConfig;
use clap::Parser;
use listenfd::ListenFd;
use std::net::TcpListener;
use std::{net::Ipv4Addr, net::SocketAddr, path::PathBuf};
Expand All @@ -14,8 +15,24 @@ mod interfaces;
mod query;
mod websocket;

// Define command-line arguments using clap
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Args {
/// Path to the DuckDB database file (default: ":memory:")
#[arg(short, long)]
db_path: Option<String>,

/// Size of the connection pool (default: 1)
#[arg(short, long, default_value_t = 1)]
pool_size: u32,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let db_path = args.db_path.unwrap_or_else(|| ":memory:".to_string());

// Tracing setup
tracing_subscriber::registry()
.with(
Expand All @@ -27,7 +44,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.init();

// App setup
let app = app::app()?;
let app = app::app(&db_path, args.pool_size)?;

// TLS configuration
let mut config = RustlsConfig::from_pem_file(
Expand Down

0 comments on commit bdddd4d

Please sign in to comment.