micro-fan / aiozk

Asyncio client for zookeeper
MIT License
49 stars 20 forks source link

Connection issues handling #45

Closed decaz closed 4 years ago

decaz commented 4 years ago

Part of example client:

while True:
    children = await client.get_children(path, watch=True)
    ...
    await client.wait_for_events([WatchEvent.CHILDREN_CHANGED], path)
  1. Client 1 lost connection to the server
  2. Client 2 added new children to the path
  3. Client 1 repaired connection

In this case Client 1 doesn't get event so he won't continue the loop to the next iteration to update children list. Is there any proper way to handle such connection problems?

cybergrind commented 4 years ago

This should be handled in the watcher implementation https://github.com/micro-fan/aiozk/blob/master/aiozk/recipes/children_watcher.py But not all cases are covered in recipes tests, so it might not work.

If it isn't you can add test case for this case, it should be similar to https://github.com/micro-fan/aiozk/blob/master/aiozk/test/test_lock.py#L33

decaz commented 4 years ago

If I understand correct there should be some auxiliary task which handles connection losses and interacts with the main client loop. For instance:

while True:
    await client.session.state.wait_for(States.SUSPENDED, States.LOST)
    ...

Am I right?

cybergrind commented 4 years ago

@decaz yes, you can use it, probably is better to have it implemented as

... asyncio.wait([watch_task, connection_lost], return_when=asyncio.FIRST_COMPLETED)

and then process response according to the fired event.

But all this should be the part of Watcher and ideally, you don't need to implement it by hands every time

decaz commented 4 years ago

Ok, thanks!