client: Handle connection errors in the client.

This commit is contained in:
Damir Jelić 2019-04-17 13:31:37 +02:00
parent 444cf64fb5
commit 827549ab7c

View File

@ -2,6 +2,7 @@ import asyncio
from pprint import pformat from pprint import pformat
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from aiohttp.client_exceptions import ClientProxyConnectionError
from nio import (AsyncClient, ClientConfig, EncryptionError, from nio import (AsyncClient, ClientConfig, EncryptionError,
GroupEncryptionError, KeysQueryResponse, MegolmEvent, GroupEncryptionError, KeysQueryResponse, MegolmEvent,
RoomEncryptedEvent, SyncResponse) RoomEncryptedEvent, SyncResponse)
@ -58,14 +59,17 @@ class PanClient(AsyncClient):
logger.info(f"Starting sync loop for {self.user_id}") logger.info(f"Starting sync loop for {self.user_id}")
try: while True:
while True: try:
if not self.logged_in: if not self.logged_in:
# TODO login # TODO login
pass pass
# TODO use user lazy loading here # TODO use user lazy loading here
response = await self.sync(30000) response = await self.sync(30000)
if response.transport_response.status != 200:
await asyncio.sleep(5)
continue
if self.should_upload_keys: if self.should_upload_keys:
await self.keys_upload() await self.keys_upload()
@ -82,10 +86,21 @@ class PanClient(AsyncClient):
self.synced.set() self.synced.set()
self.synced.clear() self.synced.clear()
except asyncio.CancelledError: except asyncio.CancelledError:
logger.info("Stopping the sync loop") logger.info("Stopping the sync loop")
self.loop_running = False self._loop_stop()
self.loop_stopped.set() break
except (ClientProxyConnectionError, ConnectionRefusedError):
try:
await asyncio.sleep(5)
except asyncio.CancelledError:
self._loop_stop()
break
def _loop_stop(self):
self.loop_running = False
self.loop_stopped.set()
async def loop_stop(self): async def loop_stop(self):
"""Stop the client loop.""" """Stop the client loop."""