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,14 +122,19 @@ class ConnectionPool(object): ...@@ -122,14 +122,19 @@ 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()
if node.isRunning():
conn = getConnForNode(node) conn = getConnForNode(node)
if conn is not None: if conn is not None:
yield (node, conn) yield (node, conn)
# Re-check if node is running, as our knowledge of its
# state can have changed during connection attempt.
elif node.isRunning(): elif node.isRunning():
new_cell_list.append(cell) new_cell_list.append(cell)
cell_list = new_cell_list cell_list = new_cell_list
......
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