Commit 7b608b8f authored by Vincent Pelletier's avatar Vincent Pelletier

client.pool: Optimise iterateForObject for most likely cases.

Nodes are likely to be running, so filtering before sort is unlikely to
save time. Caller is likely to stop iterating after first yield connection
("load" case), so move filtering inside loop.

Also, document non-straightforward code.
parent efd7f6f1
...@@ -122,16 +122,21 @@ class ConnectionPool(object): ...@@ -122,16 +122,21 @@ class ConnectionPool(object):
getConnForNode = self.getConnForNode getConnForNode = self.getConnForNode
while cell_list: while cell_list:
new_cell_list = [] new_cell_list = []
cell_list = [c for c in cell_list if c.getNode().isRunning()] # Shuffle to randomise node to access...
shuffle(cell_list) shuffle(cell_list)
# ...and sort with non-unique keys, to prioritise ranges of
# randomised entries.
cell_list.sort(key=self.getCellSortKey) cell_list.sort(key=self.getCellSortKey)
for cell in cell_list: for cell in cell_list:
node = cell.getNode() node = cell.getNode()
conn = getConnForNode(node) if node.isRunning():
if conn is not None: conn = getConnForNode(node)
yield (node, conn) if conn is not None:
elif node.isRunning(): yield (node, conn)
new_cell_list.append(cell) # Re-check if node is running, as our knowledge of its
# state can have changed during connection attempt.
elif node.isRunning():
new_cell_list.append(cell)
cell_list = new_cell_list cell_list = new_cell_list
if new_cell_list: if new_cell_list:
# wait a bit to avoid a busy loop # wait a bit to avoid a busy loop
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment