healthchecks!

This commit is contained in:
Lev Kokotov
2022-02-03 17:32:04 -08:00
parent 89043ef12f
commit 2114cb2a97
3 changed files with 23 additions and 4 deletions

View File

@@ -43,8 +43,20 @@ impl ManageConnection for ServerPool {
}
/// Determines if the connection is still connected to the database.
async fn is_valid(&self, _conn: &mut PooledConnection<'_, Self>) -> Result<(), Self::Error> {
Ok(())
async fn is_valid(&self, conn: &mut PooledConnection<'_, Self>) -> Result<(), Self::Error> {
let server = &mut *conn;
// If this fails, the connection will be closed and another will be grabbed from the pool quietly :-).
// Failover, step 1, complete.
match tokio::time::timeout(
tokio::time::Duration::from_millis(1000),
server.query("SELECT 1"),
)
.await
{
Ok(_) => Ok(()),
Err(_err) => Err(Error::ServerTimeout),
}
}
/// Synchronously determine if the connection is no longer usable, if possible.