Skip to content

Commit

Permalink
compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Sep 13, 2023
1 parent c64c359 commit faa88e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/bin/cratesfyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,11 @@ impl Context for BinContext {
Ok(self
.pool
.get_or_try_init::<_, Error>(|| {
Ok(Pool::new(&*self.config()?, self.instance_metrics()?)?)
Ok(Pool::new(
&*self.config()?,
&*self.runtime()?,
self.instance_metrics()?,
)?)
})?
.clone())
}
Expand Down
14 changes: 11 additions & 3 deletions src/db/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use r2d2_postgres::PostgresConnectionManager;
use sqlx::postgres::PgPoolOptions;
use sqlx::Executor;
use std::sync::Arc;
use tokio::runtime::Runtime;
use tracing::debug;

pub type PoolClient = r2d2::PooledConnection<PostgresConnectionManager<NoTls>>;
Expand All @@ -24,24 +25,30 @@ pub struct Pool {
}

impl Pool {
pub fn new(config: &Config, metrics: Arc<InstanceMetrics>) -> Result<Pool, PoolError> {
pub fn new(
config: &Config,
runtime: &Runtime,
metrics: Arc<InstanceMetrics>,
) -> Result<Pool, PoolError> {
debug!(
"creating database pool (if this hangs, consider running `docker-compose up -d db s3`)"
);
Self::new_inner(config, metrics, DEFAULT_SCHEMA)
Self::new_inner(config, runtime, metrics, DEFAULT_SCHEMA)
}

#[cfg(test)]
pub(crate) fn new_with_schema(
config: &Config,
runtime: &Runtime,
metrics: Arc<InstanceMetrics>,
schema: &str,
) -> Result<Pool, PoolError> {
Self::new_inner(config, metrics, schema)
Self::new_inner(config, runtime, metrics, schema)
}

fn new_inner(
config: &Config,
runtime: &Runtime,
metrics: Arc<InstanceMetrics>,
schema: &str,
) -> Result<Pool, PoolError> {
Expand All @@ -57,6 +64,7 @@ impl Pool {
.build(manager)
.map_err(PoolError::PoolCreationFailed)?;

let _guard = runtime.enter();
let async_pool = PgPoolOptions::new()
.max_connections(config.max_pool_size)
.min_connections(config.min_pool_idle)
Expand Down

0 comments on commit faa88e1

Please sign in to comment.