@@ -364,13 +364,23 @@ def _should_recycle_connection(self, conn):
364
364
return False
365
365
366
366
def _maybe_connect (self , node_id ):
367
- """Idempotent non-blocking connection attempt to the given node id."""
367
+ """Idempotent non-blocking connection attempt to the given node id.
368
+
369
+ Returns True if connection object exists and is connected / connecting
370
+ """
368
371
with self ._lock :
369
372
conn = self ._conns .get (node_id )
370
373
374
+ # Check if existing connection should be recreated because host/port changed
375
+ if conn is not None and self ._should_recycle_connection (conn ):
376
+ self ._conns .pop (node_id ).close ()
377
+ conn = None
378
+
371
379
if conn is None :
372
380
broker = self .cluster .broker_metadata (node_id )
373
- assert broker , 'Broker id %s not in current metadata' % (node_id ,)
381
+ if broker is None :
382
+ log .debug ('Broker id %s not in current metadata' , node_id )
383
+ return False
374
384
375
385
log .debug ("Initiating connection to node %s at %s:%s" ,
376
386
node_id , broker .host , broker .port )
@@ -382,16 +392,11 @@ def _maybe_connect(self, node_id):
382
392
** self .config )
383
393
self ._conns [node_id ] = conn
384
394
385
- # Check if existing connection should be recreated because host/port changed
386
- elif self ._should_recycle_connection (conn ):
387
- self ._conns .pop (node_id )
388
- return False
389
-
390
395
elif conn .connected ():
391
396
return True
392
397
393
398
conn .connect ()
394
- return conn .connected ()
399
+ return not conn .disconnected ()
395
400
396
401
def ready (self , node_id , metadata_priority = True ):
397
402
"""Check whether a node is connected and ok to send more requests.
@@ -580,7 +585,10 @@ def poll(self, timeout_ms=None, future=None):
580
585
581
586
# Attempt to complete pending connections
582
587
for node_id in list (self ._connecting ):
583
- self ._maybe_connect (node_id )
588
+ # False return means no more connection progress is possible
589
+ # Connected nodes will update _connecting via state_change callback
590
+ if not self ._maybe_connect (node_id ):
591
+ self ._connecting .remove (node_id )
584
592
585
593
# If we got a future that is already done, don't block in _poll
586
594
if future is not None and future .is_done :
@@ -919,7 +927,12 @@ def check_version(self, node_id=None, timeout=None, strict=False):
919
927
if try_node is None :
920
928
self ._lock .release ()
921
929
raise Errors .NoBrokersAvailable ()
922
- self ._maybe_connect (try_node )
930
+ if not self ._maybe_connect (try_node ):
931
+ if try_node == node_id :
932
+ raise Errors .NodeNotReadyError ("Connection failed to %s" % node_id )
933
+ else :
934
+ continue
935
+
923
936
conn = self ._conns [try_node ]
924
937
925
938
# We will intentionally cause socket failures
0 commit comments