Skip to content

Commit 788a79b

Browse files
author
Glauber Costa
committed
allow more connections when connecting to the database
Our external cloud database doesn't seem to be able to make do with just one. It does seem that there is a sqlx bug making this worse: launchbadge/sqlx#622 But not a lot of options for now, and a large connection pool is good anyway.
1 parent c441cc6 commit 788a79b

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

server/src/query/dbconn.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ pub(crate) struct DbConnection {
4141
}
4242

4343
impl DbConnection {
44-
pub(crate) async fn connect(uri: &str) -> Result<Self> {
44+
pub(crate) async fn connect(uri: &str, nr_conn: usize) -> Result<Self> {
4545
let opts = AnyConnectOptions::from_str(uri).map_err(QueryError::ConnectionFailed)?;
4646
let pool = AnyPoolOptions::new()
47+
.max_connections(nr_conn as _)
4748
.connect(uri)
4849
.await
4950
.map_err(QueryError::ConnectionFailed)?;
@@ -56,9 +57,9 @@ impl DbConnection {
5657
})
5758
}
5859

59-
pub(crate) async fn local_connection(&self) -> Result<Self> {
60+
pub(crate) async fn local_connection(&self, nr_conn: usize) -> Result<Self> {
6061
match self.kind {
61-
Kind::Postgres => Self::connect(&self.conn_uri).await,
62+
Kind::Postgres => Self::connect(&self.conn_uri, nr_conn).await,
6263
Kind::Sqlite => Ok(self.clone()),
6364
}
6465
}

server/src/query/engine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ impl QueryEngine {
163163
Self { kind, pool }
164164
}
165165

166-
pub(crate) async fn local_connection(conn: &DbConnection) -> Result<Self> {
167-
let local = conn.local_connection().await?;
166+
pub(crate) async fn local_connection(conn: &DbConnection, nr_conn: usize) -> Result<Self> {
167+
let local = conn.local_connection(nr_conn).await?;
168168
Ok(Self::new(local.kind, local.pool))
169169
}
170170

server/src/query/meta/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ impl MetaService {
161161
Self { kind, pool }
162162
}
163163

164-
pub(crate) async fn local_connection(conn: &DbConnection) -> anyhow::Result<Self> {
165-
let local = conn.local_connection().await?;
164+
pub(crate) async fn local_connection(
165+
conn: &DbConnection,
166+
nr_conn: usize,
167+
) -> anyhow::Result<Self> {
168+
let local = conn.local_connection(nr_conn).await?;
166169
Ok(Self::new(local.kind, local.pool))
167170
}
168171

server/src/server.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ pub struct Opt {
5050
/// Should we wait for a debugger before executing any JS?
5151
#[structopt(long)]
5252
inspect_brk: bool,
53+
/// size of database connection pool.
54+
#[structopt(short, long, default_value = "1000")]
55+
nr_connections: usize,
5356
/// How many executor threads to create
5457
#[structopt(short, long, default_value = "1")]
5558
executor_threads: usize,
@@ -113,6 +116,7 @@ pub struct SharedState {
113116
data_db: DbConnection,
114117
metadata_db: DbConnection,
115118
materialize: ModulesDirectory,
119+
nr_connections: usize,
116120
}
117121

118122
impl SharedState {
@@ -136,7 +140,8 @@ impl SharedTasks {
136140
async fn run(state: SharedState, mut cmd: ExecutorChannel) -> Result<()> {
137141
init_deno(&state.materialize.path(), state.inspect_brk).await?;
138142

139-
let meta = Rc::new(MetaService::local_connection(&state.metadata_db).await?);
143+
let meta =
144+
Rc::new(MetaService::local_connection(&state.metadata_db, state.nr_connections).await?);
140145
let ts = meta.load_type_system().await?;
141146

142147
let routes = meta.load_endpoints().await?;
@@ -148,7 +153,8 @@ async fn run(state: SharedState, mut cmd: ExecutorChannel) -> Result<()> {
148153
Ok(Type::Object(t)) => t,
149154
_ => anyhow::bail!("Internal error: type {} not found", OAUTHUSER_TYPE_NAME),
150155
};
151-
let query_engine = Arc::new(QueryEngine::local_connection(&state.data_db).await?);
156+
let query_engine =
157+
Arc::new(QueryEngine::local_connection(&state.data_db, state.nr_connections).await?);
152158
let mut transaction = query_engine.start_transaction().await?;
153159
query_engine
154160
.create_table(&mut transaction, &oauth_user_type)
@@ -239,11 +245,11 @@ pub async fn run_shared_state(
239245
let file = format!("{}/chisel.ts", materialize.path().display());
240246
fs::write(file, include_bytes!("../../api/src/chisel.ts")).await?;
241247

242-
let meta_conn = DbConnection::connect(&opt.metadata_db_uri).await?;
243-
let data_db = DbConnection::connect(&opt.data_db_uri).await?;
248+
let meta_conn = DbConnection::connect(&opt.metadata_db_uri, opt.nr_connections).await?;
249+
let data_db = DbConnection::connect(&opt.data_db_uri, opt.nr_connections).await?;
244250

245-
let meta = MetaService::local_connection(&meta_conn).await?;
246-
let query_engine = QueryEngine::local_connection(&data_db).await?;
251+
let meta = MetaService::local_connection(&meta_conn, opt.nr_connections).await?;
252+
let query_engine = QueryEngine::local_connection(&data_db, opt.nr_connections).await?;
247253

248254
meta.create_schema().await?;
249255

@@ -383,6 +389,7 @@ pub async fn run_shared_state(
383389
data_db,
384390
metadata_db: meta_conn,
385391
materialize: materialize.clone(),
392+
nr_connections: opt.nr_connections,
386393
};
387394

388395
let tasks = SharedTasks { rpc_task, sig_task };

0 commit comments

Comments
 (0)