✨ Opt-in auto-reconnect (backoff) for stream_measurements #49
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "feat/streaming-reconnect"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Slice 2/2 of the streaming work: adds opt-in auto-reconnect with exponential backoff to
ComwattClient.stream_measurements. Stacked on #48 (base branchfeat/streaming-websocket) — review/merge #48 first.New keyword-only params (default keeps slice-1 behavior exactly):
reconnect: bool = False— opt-in;False= single connection, generator stops on drop (unchanged).reconnect_backoff=1.0,reconnect_backoff_max=60.0— exponential (×2) delay with full jitter, capped.reconnect_max_attempts: int | None = None— max consecutive failed connects before giving up (None= unlimited); reset after any successful (re)connection.Behavior when
reconnect=True: on a dropped socket or a connection failure, sleep (jittered backoff) then re-open → re-CONNECT → re-SUBSCRIBE (siteUid topic) → re-SEND/app/streaming/start, and keep yielding transparently. Backoff + attempt counter reset after each successful session. An auth rejection is terminal: at most one_reauthenticate()retry (whenauto_reauth+ stored creds), elseComwattAuthError— never loops on bad credentials. The currentcwt_sessioncookie is re-read on every attempt (so a mid-stream re-auth is honored).Also hardens the connect path (applies to both modes): connection and STOMP-handshake
WebSocketExceptions are wrapped inComwattStreamingError(no raw library exceptions escape), and the socket is always closed on any connect/handshake failure.Design
Refactored the slice-1 monolith into
_connect_and_start(open + handshake + subscribe + start, returns a ready socket or raises a typed error, always closes on failure) and_consume_stream(the recv→yield loop). The public method is a thin dispatcher: single-shot whenreconnect=False, a backoff retry loop whenTrue. Sync only; no async/threads/callbacks; reconnection is transparent (no new event types).Test plan
filterwarnings=error).mypy comwatt_clientclean.reconnectis keyword-only, defaultFalse; the non-reconnect path is behaviorally identical to #48; no pre-existing test modified.sleepargs), give-up after N attempts, connection-error + STOMP-handshake-error wrapping (+ socket closed), auth refresh once on reconnect, auth-terminal (≤2 opens thenComwattAuthError), fresh cookie after reauth, backoff reset after a good session, DISCONNECT/close cleanup under reconnect +GeneratorExit.Notes
main.6e8ad04ab14daf185414WIP: ✨ Opt-in auto-reconnect (backoff) for stream_measurementsto ✨ Opt-in auto-reconnect (backoff) for stream_measurements