File tree Expand file tree Collapse file tree 3 files changed +20
-0
lines changed Expand file tree Collapse file tree 3 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -413,6 +413,11 @@ impl Client {
413
413
self . connection . block_on ( self . client . simple_query ( query) )
414
414
}
415
415
416
+ /// Validates connection, timing out after specified duration.
417
+ pub fn is_valid ( & mut self , timeout : std:: time:: Duration ) -> Result < ( ) , Error > {
418
+ self . connection . block_on ( self . client . is_valid ( timeout) )
419
+ }
420
+
416
421
/// Executes a sequence of SQL statements using the simple query protocol.
417
422
///
418
423
/// 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 @@ -450,6 +450,15 @@ impl Client {
450
450
self . simple_query_raw ( query) . await ?. try_collect ( ) . await
451
451
}
452
452
453
+ /// Validates connection, timing out after specified duration.
454
+ pub async fn is_valid ( & self , timeout : Duration ) -> Result < ( ) , Error > {
455
+ type SqmResult = Result < Vec < SimpleQueryMessage > , Error > ;
456
+ type SqmTimeout = Result < SqmResult , tokio:: time:: error:: Elapsed > ;
457
+ let sqm_future = self . simple_query_raw ( "" ) . await ?. try_collect ( ) ;
458
+ let sqm_timeout: SqmTimeout = tokio:: time:: timeout ( timeout, sqm_future) . await ;
459
+ sqm_timeout. map_err ( |_| Error :: timeout ( ) ) ?. map ( |_| ( ) )
460
+ }
461
+
453
462
pub ( crate ) async fn simple_query_raw ( & self , query : & str ) -> Result < SimpleQueryStream , Error > {
454
463
simple_query:: simple_query ( self . inner ( ) , query) . await
455
464
}
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,8 @@ 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
+ pub ( crate ) fn timeout ( ) -> Error {
498
+ Error :: new ( Kind :: Timeout , None )
499
+ }
494
500
}
You can’t perform that action at this time.
0 commit comments