Add the ability to get notified of connection loss
When joining a group it is common that the entity joining that group would like to know if it has somehow lost that group membership if certain conditions such as disconnection occur, to accomodate this allow for the driver to have a list of callbacks which will be automatically called if the connection is lost. Change-Id: Iab6acbc6b2934998e51f0618ec66962d77a6ce1c
This commit is contained in:
parent
069c3d308d
commit
0cdb974dd2
@ -17,6 +17,7 @@
|
||||
from kazoo import client
|
||||
from kazoo import exceptions
|
||||
from kazoo.protocol import paths
|
||||
from kazoo.protocol import states
|
||||
from zake import fake_client
|
||||
|
||||
from tooz import coordination
|
||||
@ -25,10 +26,25 @@ _TOOZ_NAMESPACE = "tooz"
|
||||
|
||||
|
||||
class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
||||
def __init__(self):
|
||||
self.lost_callbacks = []
|
||||
self._registered_listener = False
|
||||
|
||||
def _on_state_change(self, state):
|
||||
if state == states.KazooState.LOST:
|
||||
# These callbacks should be quick, and not block, so that the
|
||||
# kazoo event loop does not block... If we want this to be able
|
||||
# to handle blocking, we need to use the alternate kazoo thread
|
||||
# and schedule this loop there.
|
||||
for cb in self.lost_callbacks:
|
||||
cb(self)
|
||||
|
||||
def start(self, timeout=10):
|
||||
try:
|
||||
self._coord.start(timeout=timeout)
|
||||
if not self._registered_listener:
|
||||
self._coord.add_listener(self._on_state_change)
|
||||
self._registered_listener = True
|
||||
except self._coord.handler.timeout_exception as e:
|
||||
raise coordination.ToozConnectionError("operation error: %s" % (e))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user