baseplate.lib.thrift_pool

A Thrift client connection pool.

Note

See baseplate.clients.thrift.ThriftContextFactory for a convenient way to integrate the pool with your application.

The pool lazily creates connections and maintains them in a pool. Individual connections have a maximum lifetime, after which they will be recycled.

A basic example of usage:

pool = thrift_pool_from_config(app_config, "example_service.")
with pool.connection() as protocol:
    client = ExampleService.Client(protocol)
    client.do_example_thing()

Configuration Parsing

baseplate.lib.thrift_pool.thrift_pool_from_config(app_config, prefix, **kwargs)[source]

Make a ThriftConnectionPool from a configuration dictionary.

The keys useful to thrift_pool_from_config() should be prefixed, e.g. example_service.endpoint etc. The prefix argument specifies the prefix used to filter keys. Each key is mapped to a corresponding keyword argument on the ThriftConnectionPool constructor. Any keyword arguments given to this function will be also be passed through to the constructor. Keyword arguments take precedence over the configuration file.

Supported keys: :rtype: ThriftConnectionPool

  • endpoint (required): A host:port pair, e.g. localhost:2014,

    where the Thrift server can be found.

  • fifo_queue: True will enable use of a FIFO queue instead of the default LIFO queue.

  • size: The size of the connection pool.

  • max_age: The oldest a connection can be before it’s recycled and

    replaced with a new one. Written as a Timespan() e.g. 1 minute.

  • timeout: The maximum amount of time a connection attempt or RPC call

    can take before a TimeoutError is raised. (Timespan())

  • max_connection_attempts: The maximum number of times the pool will attempt to

    open a connection.

Changed in version 1.2: max_retries was renamed max_connection_attempts.

Classes

class baseplate.lib.thrift_pool.ThriftConnectionPool(endpoint, size=10, max_age=120, timeout=1, max_connection_attempts=3, protocol_factory=<thrift.protocol.THeaderProtocol.THeaderProtocolFactory object>, queue_cls=<class 'queue.LifoQueue'>)[source]

A pool that maintains a queue of open Thrift connections.

Parameters:
  • endpoint (EndpointConfiguration) – The remote address of the Thrift service.

  • size (int) – The maximum number of connections that can be open before new attempts to open block.

  • max_age (int) – The maximum number of seconds a connection should be kept alive. Connections older than this will be reaped.

  • timeout (float) – The maximum number of seconds a connection attempt or RPC call can take before a TimeoutError is raised.

  • max_connection_attempts (int) – The maximum number of times the pool will attempt to open a connection.

  • protocol_factory (TProtocolFactory) – The factory to use for creating protocols from transports. This is useful for talking to services that don’t support THeaderProtocol.

  • queue_cls (Queue) – A stdlib compatible queue class.

All exceptions raised by this class derive from TTransportException.

Changed in version 1.2: max_retries was renamed max_connection_attempts.

connection()[source]

Acquire a connection from the pool.

This method is to be used with a context manager. It returns a connection from the pool, or blocks up to timeout seconds waiting for one if the pool is full and all connections are in use.

When the context is exited, the connection is returned to the pool. However, if it was exited via an unexpected Thrift exception, the connection is closed instead because the state of the connection is unknown.

Return type:

Generator[TProtocolBase, None, None]