andymccurdy/redis-py — pull request #340 by thruflo (2013-04-18 17:49:07+00:00)
Title: Add optional `redis.connection.BlockingConnectionPool` class.

Adds a thread-safe `BlockingConnectionPool` class to `redis.connection`.

This can be used the same way the default `ConnectionPool` is used, with
an additional `timeout` kwarg to limit the time it will wait for an available
connection before raising a ConnectionError.

I've based the implementation on the blocking connection pool in [urllib3][],
transposing the core algorithm to the existing `ConnectionPool`s api. Aside
from the `timeout` kwarg, the main api difference is that the
`max_connections` value is defaulted to `50` and raises a `ValueError` if
the value provided is not a positive integer. This is because the
connection pool queue we block on must be created with a `maxsize`.

Usage:

>>> from redis.client import Redis
>>> client = Redis(connection_pool=BlockingConnectionPool())

Use ``max_connections`` to increase / decrease the pool size:

>>> pool = BlockingConnectionPool(max_connections=10)

Use ``timeout`` to tell it either how many seconds to wait for a connection
to become available, or to block forever:

# Block forever.
>>> pool = BlockingConnectionPool(timeout=None)

# Raise a ``ConnectionError`` after five seconds if a connection is
# not available.
>>> pool = BlockingConnectionPool(timeout=5)

[urllib3]: https://code.google.com/p/urllib3/source/browse/urllib3/connectionpool.py#128