Node Pool

class elastic_transport.NodePool(node_configs, node_class, dead_node_backoff_factor=1.0, max_dead_node_backoff=30.0, node_selector_class=<class 'elastic_transport.RoundRobinSelector'>, randomize_nodes=True)

Container holding the BaseNode instances, managing the selection process (via a NodeSelector) and dead connections.

It’s only interactions are with the Transport class that drives all the actions within NodePool.

Initially nodes are stored on the class as a list and, along with the connection options, get passed to the NodeSelector instance for future reference.

Upon each request the Transport will ask for a BaseNode via the get_node method. If the connection fails (it’s perform_request raises a ConnectionError) it will be marked as dead (via mark_dead) and put on a timeout (if it fails N times in a row the timeout is exponentially longer - the formula is default_timeout * 2 ** (fail_count - 1)). When the timeout is over the connection will be resurrected and returned to the live pool. A connection that has been previously marked as dead and succeeds will be marked as live (its fail count will be deleted).

get()

Return a node from the pool using the NodeSelector instance.

It tries to resurrect eligible nodes, forces a resurrection when no nodes are available and passes the list of live nodes to the selector instance to choose from.

Return type:

BaseNode

mark_dead(node, _now=None)

Mark the node as dead (failed). Remove it from the live pool and put it on a timeout.

Parameters:

node (BaseNode) – The failed node.

Return type:

None

mark_live(node)

Mark node as healthy after a resurrection. Resets the fail counter for the node.

Parameters:

node (BaseNode) – The BaseNode instance to mark as alive.

Return type:

None

resurrect(force=False)

Attempt to resurrect a node from the dead queue. It will try to locate one (not all) eligible (it’s timeout is over) node to return to the live pool. Any resurrected node is also returned.

Parameters:

force (bool) – resurrect a node even if there is none eligible (used when we have no live nodes). If force is ‘True’’ resurrect always returns a node.

Return type:

Optional[BaseNode]

Node selectors

class elastic_transport.NodeSelector(node_configs)

Simple class used to select a node from a list of currently live node instances. In init time it is passed a dictionary containing all the nodes options which it can then use during the selection process. When the select() method is called it is given a list of currently live nodes to choose from.

The selector is initialized with the list of seed nodes that the NodePool was initialized with. This list of seed nodes can be used to make decisions within select()

Example of where this would be useful is a zone-aware selector that would only select connections from it’s own zones and only fall back to other connections where there would be none in its zones.

select(nodes)

Select a nodes from the given list.

Parameters:

nodes (Sequence[BaseNode]) – list of live nodes to choose from

Return type:

BaseNode

class elastic_transport.RandomSelector(node_configs)

Randomly select a node

class elastic_transport.RoundRobinSelector(node_configs)

Select a node using round-robin