making connection consistency more configurable

This commit is contained in:
Blake Eggleston
2013-06-07 16:18:59 -07:00
parent d6707038df
commit f865465757
3 changed files with 22 additions and 6 deletions

View File

@@ -27,7 +27,7 @@ _max_connections = 10
# global connection pool # global connection pool
connection_pool = None connection_pool = None
def setup(hosts, username=None, password=None, max_connections=10, default_keyspace=None): def setup(hosts, username=None, password=None, max_connections=10, default_keyspace=None, consistency='ONE'):
""" """
Records the hosts and connects to one of them Records the hosts and connects to one of them
@@ -55,16 +55,17 @@ def setup(hosts, username=None, password=None, max_connections=10, default_keysp
if not _hosts: if not _hosts:
raise CQLConnectionError("At least one host required") raise CQLConnectionError("At least one host required")
connection_pool = ConnectionPool(_hosts, username, password) connection_pool = ConnectionPool(_hosts, username, password, consistency)
class ConnectionPool(object): class ConnectionPool(object):
"""Handles pooling of database connections.""" """Handles pooling of database connections."""
def __init__(self, hosts, username=None, password=None): def __init__(self, hosts, username=None, password=None, consistency=None):
self._hosts = hosts self._hosts = hosts
self._username = username self._username = username
self._password = password self._password = password
self._consistency = consistency
self._queue = Queue.Queue(maxsize=_max_connections) self._queue = Queue.Queue(maxsize=_max_connections)
@@ -120,7 +121,13 @@ class ConnectionPool(object):
for host in hosts: for host in hosts:
try: try:
new_conn = cql.connect(host.name, host.port, user=self._username, password=self._password) new_conn = cql.connect(
host.name,
host.port,
user=self._username,
password=self._password,
consistency_level=self._consistency
)
new_conn.set_cql_version('3.0.0') new_conn.set_cql_version('3.0.0')
return new_conn return new_conn
except Exception as e: except Exception as e:

View File

@@ -7,11 +7,20 @@ Connection
The setup function in `cqlengine.connection` records the Cassandra servers to connect to. The setup function in `cqlengine.connection` records the Cassandra servers to connect to.
If there is a problem with one of the servers, cqlengine will try to connect to each of the other connections before failing. If there is a problem with one of the servers, cqlengine will try to connect to each of the other connections before failing.
.. function:: setup(hosts [, username=None, password=None]) .. function:: setup(hosts [, username=None, password=None, consistency='ONE'])
:param hosts: list of hosts, strings in the <hostname>:<port>, or just <hostname> :param hosts: list of hosts, strings in the <hostname>:<port>, or just <hostname>
:type hosts: list :type hosts: list
:param username: a username, if required
:type username: str
:param password: a password, if required
:type password: str
:param consistency: the consistency level of the connection, defaults to 'ONE'
:type consistency: str
Records the hosts and connects to one of them Records the hosts and connects to one of them
See the example at :ref:`getting-started` See the example at :ref:`getting-started`