connection_pool

class opensearchpy.ConnectionPool(connections, dead_timeout=60, timeout_cutoff=5, selector_class=<class 'opensearchpy.connection_pool.RoundRobinSelector'>, randomize_hosts=True, **kwargs)[source]

Bases: object

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

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

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

Upon each request the Transport will ask for a Connection via the get_connection method. If the connection fails (its 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).

Parameters:
  • connections (Any) – list of tuples containing the Connection instance and its options

  • dead_timeout (float) – number of seconds a connection should be retired for after a failure, increases on consecutive failures

  • timeout_cutoff (int) – number of consecutive failures after which the timeout doesn’t increase

  • selector_class (Type[ConnectionSelector]) – ConnectionSelector subclass to use if more than one connection is live

  • randomize_hosts (bool) – shuffle the list of connections upon arrival to avoid dog piling effect across processes

  • connections

  • dead_timeout

  • timeout_cutoff

  • selector_class

  • randomize_hosts

  • kwargs (Any) –

__repr__()[source]

Return repr(self).

Return type:

str

close()[source]

Explicitly closes connections

Return type:

Any

get_connection()[source]

Return a connection from the pool using the ConnectionSelector instance.

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

Returns a connection instance and its current fail count.

Return type:

Any

mark_dead(connection, now=None)[source]

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

Parameters:
  • connection (Any) – the failed instance

  • connection

  • now (float | None) –

Return type:

None

mark_live(connection)[source]

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

Parameters:
  • connection (Any) – the connection to redeem

  • connection

Return type:

None

resurrect(force=False)[source]

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

Parameters:
  • force (bool) – resurrect a connection even if there is none eligible (used when we have no live connections). If force is specified resurrect always returns a connection.

  • force

Return type:

Any

class opensearchpy.ConnectionSelector(opts)[source]

Bases: object

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

The options dictionary is the one that has been passed to Transport as hosts param and the same that is used to construct the Connection object itself. When the Connection was created from information retrieved from the cluster via the sniffing process it will be the dictionary returned by the host_info_callback.

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

Parameters:
select(connections)[source]

Select a connection from the given list.

Parameters:
  • connections (Sequence[Connection]) – list of live connections to choose from

  • connections

Return type:

None

class opensearchpy.RoundRobinSelector(opts)[source]

Bases: ConnectionSelector

Selector using round-robin.

Parameters:
select(connections)[source]

Select a connection from the given list.

Parameters:
  • connections (Sequence[Connection]) – list of live connections to choose from

  • connections

Return type:

Any