'Choose the node with fewest outstanding requests, with fallbacks.
This method will prefer a node with an existing connection and no
in-flight-requests. If no such node is found, a node will be chosen
randomly from disconnected nodes that are not "blacked out" (i.e.,
are not subject to a reconnect backoff).
Returns:
node_id or None if no suitable node was found'

def least_loaded_node(self):

    nodes = [broker.nodeId for broker in self.cluster.brokers()]
    random.shuffle(nodes)
    inflight = float('inf')
    found = None
    for node_id in nodes:
        conn = self._conns.get(node_id)
        connected = ((conn is not None) and conn.connected())
        blacked_out = ((conn is not None) and conn.blacked_out())
        curr_inflight = (len(conn.in_flight_requests) if conn else 0)
        if (connected and (curr_inflight == 0)):
            return node_id
        elif ((not blacked_out) and (curr_inflight < inflight)):
            inflight = curr_inflight
            found = node_id
    if (found is not None):
        return found
    elif ('bootstrap' in self._conns):
        return 'bootstrap'
    return None
