Skip to content

Commit 6469dbc

Browse files
authored
Support database poolers: clean up LISTEN/NOTIFY state when opening a connection (#4367)
2 parents d310f48 + 7a11f21 commit 6469dbc

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

crates/cli/src/util.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use mas_storage::RepositoryAccess;
2323
use mas_storage_pg::PgRepository;
2424
use mas_templates::{SiteConfigExt, TemplateLoadingError, Templates};
2525
use sqlx::{
26-
ConnectOptions, PgConnection, PgPool,
26+
ConnectOptions, Executor, PgConnection, PgPool,
2727
postgres::{PgConnectOptions, PgPoolOptions},
2828
};
2929
use tokio_util::{sync::CancellationToken, task::TaskTracker};
@@ -339,6 +339,15 @@ pub async fn database_pool_from_config(config: &DatabaseConfig) -> Result<PgPool
339339
.acquire_timeout(config.connect_timeout)
340340
.idle_timeout(config.idle_timeout)
341341
.max_lifetime(config.max_lifetime)
342+
.after_connect(|conn, _meta| {
343+
Box::pin(async move {
344+
// Unlisten from all channels, as we might be connected via a connection pooler
345+
// that doesn't clean up LISTEN/NOTIFY state when reusing connections.
346+
conn.execute("UNLISTEN *;").await?;
347+
348+
Ok(())
349+
})
350+
})
342351
.connect_with(options)
343352
.await
344353
.context("could not connect to the database")

0 commit comments

Comments
 (0)