From e62d99c3cc36d798177cd62a9183c0fbc76e77d0 Mon Sep 17 00:00:00 2001 From: miruka Date: Mon, 20 Jul 2020 16:03:46 -0400 Subject: [PATCH] Stricter requirements to consider a client healthy A healthy client must not only have a sync task running, but also have finished its initial sync and have been succesful in its last sync attempt. The previous weak requirement caused this issue: Have two clients, one on a failing server, and try to fetch a media, thumbnail or profile: since these functions don't need auth, any healthy client is picked to do the request, but the previous requirement made it possible to pick the failing client. --- src/backend/backend.py | 3 ++- src/backend/matrix_client.py | 19 +++++++++++++------ src/icons/thin/image-rotate-left.svg | 3 +++ src/icons/thin/image-rotate-right.svg | 3 +++ 4 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 src/icons/thin/image-rotate-left.svg create mode 100644 src/icons/thin/image-rotate-right.svg diff --git a/src/backend/backend.py b/src/backend/backend.py index 6eccdd24..cf3c63b3 100644 --- a/src/backend/backend.py +++ b/src/backend/backend.py @@ -258,7 +258,8 @@ class Backend: while True: for client in self.clients.values(): - if client.syncing: + if client.healthy: + print( client, client.first_sync_done) return client if failures and failures % 300 == 0: diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 24d5cd2e..9fda547b 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -189,8 +189,9 @@ class MatrixClient(nio.AsyncClient): self.upload_tasks: Dict[UUID, asyncio.Task] = {} self.send_message_tasks: Dict[UUID, asyncio.Task] = {} - self.first_sync_done: asyncio.Event = asyncio.Event() - self.first_sync_date: Optional[datetime] = None + self.first_sync_done: asyncio.Event = asyncio.Event() + self.first_sync_date: Optional[datetime] = None + self.last_sync_error: Optional[Exception] = None self.past_tokens: Dict[str, str] = {} # {room_id: token} self.fully_loaded_rooms: Set[str] = set() # {room_id} @@ -324,13 +325,15 @@ class MatrixClient(nio.AsyncClient): await self.close() @property - def syncing(self) -> bool: - """Return whether this client is currently syncing with the server.""" + def healthy(self) -> bool: + """Return whether we're syncing and last sync was successful.""" - if not self.sync_task: + task = self.sync_task + + if not task or not self.first_sync_date or self.last_sync_error: return False - return not self.sync_task.done() + return not task.done() async def _start(self) -> None: @@ -395,6 +398,8 @@ class MatrixClient(nio.AsyncClient): await self.sync_task break # task cancelled except Exception as err: + self.last_sync_error = err + trace = traceback.format_exc().rstrip() if isinstance(err, MatrixError) and err.http_code >= 500: @@ -405,6 +410,8 @@ class MatrixClient(nio.AsyncClient): ) else: LoopException(str(err), err, trace) + else: + self.last_sync_error = None await asyncio.sleep(5) diff --git a/src/icons/thin/image-rotate-left.svg b/src/icons/thin/image-rotate-left.svg new file mode 100644 index 00000000..634eee2e --- /dev/null +++ b/src/icons/thin/image-rotate-left.svg @@ -0,0 +1,3 @@ + + + diff --git a/src/icons/thin/image-rotate-right.svg b/src/icons/thin/image-rotate-right.svg new file mode 100644 index 00000000..07fa9321 --- /dev/null +++ b/src/icons/thin/image-rotate-right.svg @@ -0,0 +1,3 @@ + + +