@@ -478,10 +478,31 @@ def zk_loop(self):
478478 self .client ._session_callback (KeeperState .CLOSED )
479479 self .logger .log (BLATHER , 'Connection stopped' )
480480
481+ def _expand_client_hosts (self ):
482+ # Expand the entire list in advance so we can randomize it if needed
483+ host_ports = []
484+ for host , port in self .client .hosts :
485+ try :
486+ for rhost in socket .getaddrinfo (host .strip (), port , 0 , 0 , socket .IPPROTO_TCP ):
487+ host_ports .append ((rhost [4 ][0 ], rhost [4 ][1 ]))
488+ except socket .gaierror as e :
489+ # Skip hosts that don't resolve
490+ self .logger .warning ("Cannot resolve %s: %s" , host .strip (), e )
491+ pass
492+ if self .client .randomize_hosts :
493+ random .shuffle (host_ports )
494+ return host_ports
495+
481496 def _connect_loop (self , retry ):
482497 # Iterate through the hosts a full cycle before starting over
483498 status = None
484- for host , port in self .client .hosts :
499+ host_ports = self ._expand_client_hosts ()
500+
501+ # Check for an empty hostlist, indicating none resolved
502+ if len (host_ports ) == 0 :
503+ return STOP_CONNECTING
504+
505+ for host , port in host_ports :
485506 if self .client ._stopped .is_set ():
486507 status = STOP_CONNECTING
487508 break
0 commit comments