@@ -1541,6 +1541,7 @@ pub struct Connector {
1541
1541
session : Option < SessionInfo > ,
1542
1542
readonly : bool ,
1543
1543
detached : bool ,
1544
+ fail_eagerly : bool ,
1544
1545
server_version : Version ,
1545
1546
session_timeout : Duration ,
1546
1547
connection_timeout : Duration ,
@@ -1555,6 +1556,7 @@ impl Connector {
1555
1556
session : None ,
1556
1557
readonly : false ,
1557
1558
detached : false ,
1559
+ fail_eagerly : false ,
1558
1560
server_version : Version ( u32:: MAX , u32:: MAX , u32:: MAX ) ,
1559
1561
session_timeout : Duration :: ZERO ,
1560
1562
connection_timeout : Duration :: ZERO ,
@@ -1621,6 +1623,15 @@ impl Connector {
1621
1623
self
1622
1624
}
1623
1625
1626
+ /// Fail session establishment eagerly with [Error::NoHosts] when all hosts has been tried.
1627
+ ///
1628
+ /// This permits fail-fast without wait up to [Self::session_timeout] in [Self::connect]. This
1629
+ /// is not suitable for situations where ZooKeeper cluster is accessible via a single virtual IP.
1630
+ pub fn fail_eagerly ( & mut self ) -> & mut Self {
1631
+ self . fail_eagerly = true ;
1632
+ self
1633
+ }
1634
+
1624
1635
async fn connect_internally ( & mut self , secure : bool , cluster : & str ) -> Result < Client > {
1625
1636
let ( endpoints, chroot) = endpoint:: parse_connect_string ( cluster, secure) ?;
1626
1637
if let Some ( session) = self . session . as_ref ( ) {
@@ -1647,6 +1658,9 @@ impl Connector {
1647
1658
self . connection_timeout ,
1648
1659
) ;
1649
1660
let mut endpoints = IterableEndpoints :: from ( endpoints. as_slice ( ) ) ;
1661
+ if !self . fail_eagerly {
1662
+ endpoints. cycle ( ) ;
1663
+ }
1650
1664
let mut buf = Vec :: with_capacity ( 4096 ) ;
1651
1665
let mut connecting_depot = Depot :: for_connecting ( ) ;
1652
1666
let conn = session. start ( & mut endpoints, & mut buf, & mut connecting_depot) . await ?;
@@ -1677,8 +1691,9 @@ impl Connector {
1677
1691
/// plaintext protocol, while `server3` uses tls encrypted protocol.
1678
1692
///
1679
1693
/// # Notable errors
1680
- /// * [Error::NoHosts] if no host is available
1694
+ /// * [Error::NoHosts] if no host is available and [Self::fail_eagerly] is turn on
1681
1695
/// * [Error::SessionExpired] if specified session expired
1696
+ /// * [Error::Timeout] if no session established with in approximate [Self::session_timeout]
1682
1697
///
1683
1698
/// # Notable behaviors
1684
1699
/// The state of this connector is undefined after connection attempt no matter whether it is
0 commit comments