File tree Expand file tree Collapse file tree 3 files changed +22
-0
lines changed Expand file tree Collapse file tree 3 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -489,6 +489,12 @@ impl Client {
489
489
simple_query:: batch_execute ( self . inner ( ) , query) . await
490
490
}
491
491
492
+ /// Check the connection is alive and wait for the confirmation.
493
+ pub async fn check_connection ( & self ) -> Result < ( ) , Error > {
494
+ // sync is a very quick message to test the connection health.
495
+ query:: sync ( self . inner ( ) ) . await
496
+ }
497
+
492
498
/// Begins a new database transaction.
493
499
///
494
500
/// The transaction will roll back by default - use the `commit` method to commit it.
Original file line number Diff line number Diff line change @@ -242,3 +242,13 @@ impl RowStream {
242
242
self . rows_affected
243
243
}
244
244
}
245
+
246
+ pub async fn sync ( client : & InnerClient ) -> Result < ( ) , Error > {
247
+ let buf = Bytes :: from_static ( b"S\0 \0 \0 \x04 " ) ;
248
+ let mut responses = client. send ( RequestMessages :: Single ( FrontendMessage :: Raw ( buf) ) ) ?;
249
+
250
+ match responses. next ( ) . await ? {
251
+ Message :: ReadyForQuery ( _) => Ok ( ( ) ) ,
252
+ _ => Err ( Error :: unexpected_message ( ) ) ,
253
+ }
254
+ }
Original file line number Diff line number Diff line change @@ -149,6 +149,12 @@ async fn scram_password_ok() {
149
149
connect ( "user=scram_user password=password dbname=postgres" ) . await ;
150
150
}
151
151
152
+ #[ tokio:: test]
153
+ async fn sync ( ) {
154
+ let client = connect ( "user=postgres" ) . await ;
155
+ client. check_connection ( ) . await . unwrap ( ) ;
156
+ }
157
+
152
158
#[ tokio:: test]
153
159
async fn pipelined_prepare ( ) {
154
160
let client = connect ( "user=postgres" ) . await ;
You can’t perform that action at this time.
0 commit comments