My question relates to Kazoo/Zookeeper, but more generally it's about allocating a resource and then letting the caller of that resource no they should pause. From Kazoo docs:
Itβs highly recommended to add a state listener with add_listener() and watch for LOST and SUSPENDED state changes and re-act appropriately. In the event that a LOST state occurs, its certain that the lock and/or the lease has been lost.
I'm using a context manager to allocate locks:
@contextmanager
def lock(self, path, identifier):
lock = zk.Lock(path, identifier)
lock.acquire()
yield lock <--- how to communicate connection loss from client?
finally:
lock.release()
Used like this:
with lock('/some_op/') as lck:
# do something with lock, but pause in case of connection loss?
How do you allocate a lock but revoke access in case of connection loss?
here's how Kazoo recommends you implement the connection state listener:
def my_listener(state):
if state == KazooState.LOST:
# Register somewhere that the session was lost
elif state == KazooState.SUSPENDED:
# Handle being disconnected from Zookeeper
else:
# Handle being connected/reconnected to Zookeeper