@@ -402,10 +402,10 @@ def ready(self, node_id, metadata_priority=True):
402
402
403
403
def connected (self , node_id ):
404
404
"""Return True iff the node_id is connected."""
405
- with self ._lock :
406
- if node_id not in self . _conns :
407
- return False
408
- return self . _conns [ node_id ] .connected ()
405
+ conn = self ._conns . get ( node_id )
406
+ if conn is None :
407
+ return False
408
+ return conn .connected ()
409
409
410
410
def _close (self ):
411
411
if not self ._closed :
@@ -448,10 +448,10 @@ def is_disconnected(self, node_id):
448
448
Returns:
449
449
bool: True iff the node exists and is disconnected
450
450
"""
451
- with self ._lock :
452
- if node_id not in self . _conns :
453
- return False
454
- return self . _conns [ node_id ] .disconnected ()
451
+ conn = self ._conns . get ( node_id )
452
+ if conn is None :
453
+ return False
454
+ return conn .disconnected ()
455
455
456
456
def connection_delay (self , node_id ):
457
457
"""
@@ -467,10 +467,10 @@ def connection_delay(self, node_id):
467
467
Returns:
468
468
int: The number of milliseconds to wait.
469
469
"""
470
- with self ._lock :
471
- if node_id not in self . _conns :
472
- return 0
473
- return self . _conns [ node_id ] .connection_delay ()
470
+ conn = self ._conns . get ( node_id )
471
+ if conn is None :
472
+ return 0
473
+ return conn .connection_delay ()
474
474
475
475
def is_ready (self , node_id , metadata_priority = True ):
476
476
"""Check whether a node is ready to send more requests.
@@ -656,13 +656,14 @@ def in_flight_request_count(self, node_id=None):
656
656
Returns:
657
657
int: pending in-flight requests for the node, or all nodes if None
658
658
"""
659
- with self ._lock :
660
- if node_id is not None :
661
- if node_id not in self ._conns :
662
- return 0
663
- return len (self ._conns [node_id ].in_flight_requests )
664
- else :
665
- return sum ([len (conn .in_flight_requests ) for conn in self ._conns .values ()])
659
+ if node_id is not None :
660
+ conn = self ._conns .get (node_id )
661
+ if conn is None :
662
+ return 0
663
+ return len (conn .in_flight_requests )
664
+ else :
665
+ return sum ([len (conn .in_flight_requests )
666
+ for conn in list (self ._conns .values ())])
666
667
667
668
def _fire_pending_completed_requests (self ):
668
669
responses = []
@@ -689,38 +690,37 @@ def least_loaded_node(self):
689
690
Returns:
690
691
node_id or None if no suitable node was found
691
692
"""
692
- with self ._lock :
693
- nodes = [broker .nodeId for broker in self .cluster .brokers ()]
694
- random .shuffle (nodes )
695
-
696
- inflight = float ('inf' )
697
- found = None
698
- for node_id in nodes :
699
- conn = self ._conns .get (node_id )
700
- connected = conn is not None and conn .connected ()
701
- blacked_out = conn is not None and conn .blacked_out ()
702
- curr_inflight = len (conn .in_flight_requests ) if conn is not None else 0
703
- if connected and curr_inflight == 0 :
704
- # if we find an established connection
705
- # with no in-flight requests, we can stop right away
706
- return node_id
707
- elif not blacked_out and curr_inflight < inflight :
708
- # otherwise if this is the best we have found so far, record that
709
- inflight = curr_inflight
710
- found = node_id
711
-
712
- if found is not None :
713
- return found
714
-
715
- # some broker versions return an empty list of broker metadata
716
- # if there are no topics created yet. the bootstrap process
717
- # should detect this and keep a 'bootstrap' node alive until
718
- # a non-bootstrap node is connected and non-empty broker
719
- # metadata is available
720
- elif 'bootstrap' in self ._conns :
721
- return 'bootstrap'
693
+ nodes = [broker .nodeId for broker in self .cluster .brokers ()]
694
+ random .shuffle (nodes )
722
695
723
- return None
696
+ inflight = float ('inf' )
697
+ found = None
698
+ for node_id in nodes :
699
+ conn = self ._conns .get (node_id )
700
+ connected = conn is not None and conn .connected ()
701
+ blacked_out = conn is not None and conn .blacked_out ()
702
+ curr_inflight = len (conn .in_flight_requests ) if conn is not None else 0
703
+ if connected and curr_inflight == 0 :
704
+ # if we find an established connection
705
+ # with no in-flight requests, we can stop right away
706
+ return node_id
707
+ elif not blacked_out and curr_inflight < inflight :
708
+ # otherwise if this is the best we have found so far, record that
709
+ inflight = curr_inflight
710
+ found = node_id
711
+
712
+ if found is not None :
713
+ return found
714
+
715
+ # some broker versions return an empty list of broker metadata
716
+ # if there are no topics created yet. the bootstrap process
717
+ # should detect this and keep a 'bootstrap' node alive until
718
+ # a non-bootstrap node is connected and non-empty broker
719
+ # metadata is available
720
+ elif 'bootstrap' in self ._conns :
721
+ return 'bootstrap'
722
+
723
+ return None
724
724
725
725
def set_topics (self , topics ):
726
726
"""Set specific topics to track for metadata.
0 commit comments