File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ use crate::{
4
4
ToStatement , Transaction , TransactionBuilder ,
5
5
} ;
6
6
use std:: task:: Poll ;
7
+ use std:: time:: Duration ;
7
8
use tokio_postgres:: tls:: { MakeTlsConnect , TlsConnect } ;
8
9
use tokio_postgres:: types:: { BorrowToSql , ToSql , Type } ;
9
10
use tokio_postgres:: { Error , Row , SimpleQueryMessage , Socket } ;
@@ -413,6 +414,18 @@ impl Client {
413
414
self . connection . block_on ( self . client . simple_query ( query) )
414
415
}
415
416
417
+ /// Validates connection, timing out after specified duration.
418
+ pub fn is_valid ( & mut self , timeout : Duration ) -> Result < ( ) , Error > {
419
+ let inner_client = & self . client ;
420
+ self . connection . block_on ( async {
421
+ let trivial_query = inner_client. simple_query ( "" ) ;
422
+ tokio:: time:: timeout ( timeout, trivial_query)
423
+ . await
424
+ . map_err ( |_| Error :: timeout ( ) ) ?
425
+ . map ( |_| ( ) )
426
+ } )
427
+ }
428
+
416
429
/// Executes a sequence of SQL statements using the simple query protocol.
417
430
///
418
431
/// Statements should be separated by semicolons. If an error occurs, execution of the sequence will stop at that
Original file line number Diff line number Diff line change @@ -354,6 +354,7 @@ enum Kind {
354
354
RowCount ,
355
355
#[ cfg( feature = "runtime" ) ]
356
356
Connect ,
357
+ Timeout ,
357
358
}
358
359
359
360
struct ErrorInner {
@@ -392,6 +393,7 @@ impl fmt::Display for Error {
392
393
Kind :: RowCount => fmt. write_str ( "query returned an unexpected number of rows" ) ?,
393
394
#[ cfg( feature = "runtime" ) ]
394
395
Kind :: Connect => fmt. write_str ( "error connecting to server" ) ?,
396
+ Kind :: Timeout => fmt. write_str ( "timeout waiting for server" ) ?,
395
397
} ;
396
398
if let Some ( ref cause) = self . 0 . cause {
397
399
write ! ( fmt, ": {}" , cause) ?;
@@ -491,4 +493,9 @@ impl Error {
491
493
pub ( crate ) fn connect ( e : io:: Error ) -> Error {
492
494
Error :: new ( Kind :: Connect , Some ( Box :: new ( e) ) )
493
495
}
496
+
497
+ #[ doc( hidden) ]
498
+ pub fn timeout ( ) -> Error {
499
+ Error :: new ( Kind :: Timeout , None )
500
+ }
494
501
}
You can’t perform that action at this time.
0 commit comments