A Thrift client connection pool.


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)

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:

  • endpoint (required): A host:port pair, e.g. localhost:2014,
    where the Thrift server can be found.
  • 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_retries: The maximum number of times the pool will attempt to
    open a connection.
Return type:ThriftConnectionPool


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

A pool that maintains a queue of open Thrift connections.

  • 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 (int) – The maximum number of seconds a connection attempt or RPC call can take before a TimeoutError is raised.
  • max_retries (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.

All exceptions raised by this class derive from TTransportException.


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]