Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions fns_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import asyncio
import logging
import time
from typing import Any, Callable, Coroutine

import websockets
Expand Down Expand Up @@ -36,6 +37,7 @@ def __init__(self, config: AppConfig) -> None:
self._on_reconnect: Callable[[], Coroutine] | None = None
self._msg_queue: list[str | bytes] = []
self._ready_event = asyncio.Event()
self._last_received_at: float = 0.0

def on_reconnect(self, handler: Callable[[], Coroutine]) -> None:
self._on_reconnect = handler
Expand Down Expand Up @@ -121,28 +123,48 @@ async def _connect(self) -> None:
)
log.info("Connecting to %s", url)

interval = self.config.client.heartbeat_interval
self.ws = await websockets.connect(
url,
max_size=128 * 1024 * 1024,
ping_interval=interval,
ping_timeout=interval,
ping_interval=None,
ping_timeout=None,
close_timeout=10,
)
self._last_received_at = time.monotonic()
log.info("WebSocket connected, sending auth")

auth_raw = f"{ACTION_AUTHORIZATION}{SEPARATOR}{self.config.server.token}"
await self._raw_send(auth_raw)

async def _listen(self) -> None:
assert self.ws is not None
async for raw in self.ws:
if isinstance(raw, bytes):
await self._handle_binary(raw)
else:
await self._handle_text(raw)
watchdog = asyncio.create_task(self._inactivity_watchdog())
try:
async for raw in self.ws:
if isinstance(raw, bytes):
await self._handle_binary(raw)
else:
await self._handle_text(raw)
finally:
watchdog.cancel()

async def _inactivity_watchdog(self) -> None:
"""Close the connection if no message is received for 2 × heartbeat_interval."""
interval = self.config.client.heartbeat_interval
deadline = interval * 2
while True:
await asyncio.sleep(interval)
idle = time.monotonic() - self._last_received_at
if idle >= deadline:
log.warning(
"No data received for %.0fs — closing for reconnect", idle
)
if self.ws:
await self.ws.close()
return

async def _handle_text(self, raw: str) -> None:
self._last_received_at = time.monotonic()
msg = decode_message(raw)
log.debug("← %s | %s", msg.action, str(msg.data)[:200])

Expand All @@ -160,6 +182,7 @@ async def _handle_text(self, raw: str) -> None:
log.debug("Unhandled action: %s", msg.action)

async def _handle_binary(self, raw: bytes) -> None:
self._last_received_at = time.monotonic()
if self._binary_handler and len(raw) > 42 and raw[:2] == b"00":
try:
sid, idx, data = parse_binary_chunk(raw[2:])
Expand Down
2 changes: 1 addition & 1 deletion fns_cli/sync_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ async def push(self) -> None:
async def _initial_sync(self) -> None:
if self.config.sync.sync_notes:
await self.note_sync.request_sync()
await self._wait_note_sync(timeout=60)
await self._wait_note_sync(timeout=300)

if self.config.sync.sync_files or self.config.sync.sync_config:
await self.file_sync.request_sync()
Expand Down
Loading