Skip to content

Commit cfdc2c6

Browse files
ping con before acquire
1 parent 68b2532 commit cfdc2c6

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/modules/db/sqlx/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use quickjs_runtime::reflection::get_proxy_instance_id;
1717
use quickjs_runtime::values::{JsValueFacade, TypedArrayType};
1818
use sqlx_lib::mysql::MySqlPoolOptions;
1919
use sqlx_lib::postgres::PgPoolOptions;
20-
use sqlx_lib::Executor;
20+
use sqlx_lib::{Connection, Executor};
2121
use sqlx_lib::{
2222
Column, MySql, MySqlExecutor, PgExecutor, Pool, Postgres, Row, Transaction, TypeInfo,
2323
};
@@ -721,6 +721,14 @@ impl SqlxConnection {
721721
.max_lifetime(Duration::from_secs(3600))
722722
.max_connections(64)
723723
.min_connections(2)
724+
.before_acquire(|conn, meta| Box::pin(async move {
725+
// if idle for more than a minute, ping before acquire
726+
if meta.idle_for.as_secs() > 60 {
727+
conn.ping().await?;
728+
}
729+
730+
Ok(true)
731+
}))
724732
.connect_lazy(con_str.as_str())
725733
.map_err(|e| JsError::new_string(format!("{e:?}")))?;
726734
Ok(SqlxConnection::MySqlConnection {
@@ -742,6 +750,14 @@ impl SqlxConnection {
742750
.max_lifetime(Duration::from_secs(3600))
743751
.max_connections(64)
744752
.min_connections(2)
753+
.before_acquire(|conn, meta| Box::pin(async move {
754+
// if idle for more than a minute, ping before acquire
755+
if meta.idle_for.as_secs() > 60 {
756+
conn.ping().await?;
757+
}
758+
759+
Ok(true)
760+
}))
745761
.connect_lazy(con_str.as_str())
746762
.map_err(|e| JsError::new_string(format!("{e:?}")))?;
747763
Ok(SqlxConnection::PostgresConnection {

0 commit comments

Comments
 (0)