Maintain listener list within Cluster

This commit is contained in:
Tyler Hobbs
2013-10-17 12:27:46 -05:00
parent 203f7720da
commit 4834683a43

View File

@@ -231,6 +231,9 @@ class Cluster(object):
_is_setup = False
_prepared_statements = None
_listeners = None
_listener_lock = None
def __init__(self,
contact_points=("127.0.0.1",),
port=9042,
@@ -290,6 +293,9 @@ class Cluster(object):
self.cql_version = cql_version
self.max_schema_agreement_wait = max_schema_agreement_wait
self._listeners = set()
self._listener_lock = Lock()
# let Session objects be GC'ed (and shutdown) when the user no longer
# holds a reference. Normally the cycle detector would handle this,
# but implementing __del__ prevents that.
@@ -521,6 +527,25 @@ class Cluster(object):
for session in self.sessions:
session.on_remove(host)
def register_listener(self, listener):
"""
Adds a :class:`cassandra.policies.HostStateListener` subclass instance to
the list of listeners to be notified when a host is added, removed,
marked up, or marked down.
"""
with self._listener_lock:
self._listeners.add(listener)
def unregister_listener(self, listener):
""" Removes a registered listener. """
with self._listener_lock:
self._listeners.remove(listener)
@property
def listeners(self):
with self._listener_lock:
return self._listeners.copy()
def ensure_core_connections(self):
"""
If any host has fewer than the configured number of core connections