Commit 1e92fbe
authored
Refactor WebSocket client to use multiprocessing and improve queue handling (#127)
* Use worker process and MP queues for WS client
Replace the thread-based WebSocket implementation with a separate worker process and multiprocessing queues. Adds _ws_worker_process to manage connect/send/recv/reconnect logic and new MP queues for outbound/inbound/state/control; introduces an event thread to drain state and incoming queues and restart the worker if needed. Refactors connect/start/stop/send flows to orchestrate the worker process and cleans up queue draining on stop. Tests updated to expect multiprocessing queues and to exercise the new drain/dispatch behavior.
* Refactor WS worker to threaded sender/receiver
Split the websocket worker into dedicated sender and receiver threads and replace boolean flags with threading.Events (connected_event, stop_event). The main loop now manages connection lifecycle and stop requests, handling policy violations, connection errors, and clean shutdown. Added slow_send detection and more granular state_queue events (slow_send, send_error, receive_error, connection_error, policy_violation, stopped) and simplified send/receive error handling and cleanup.
* Remove unused thread attrs and tidy event thread
Remove obsolete thread attributes (receiver_thread, sender_thread, client_thread) from Client initialization and stop setting/clearing client_thread elsewhere. Add a brief docstring to _start_event_thread and tidy related logic to keep event thread management focused and clearer.
* Refactor WS client worker and add queue handling
Rename and document the worker function ( _ws_worker_process -> ws_worker_process ), add docstrings across worker and Client methods, and import queue.Full. Make the outbound message queue bounded (maxsize=10) and implement a drop-oldest strategy when the queue is full: on overflow drop the oldest message and retry, logging warnings if the new message still can't be queued. Adjust send_message to be a no-op when not connected/running and use non-blocking queue operations. Update process startup to use the renamed worker target and minor cleanup/comment removals. These changes prevent unbounded memory growth from a runaway outbound queue and improve robustness and observability of the WebSocket client.
* Update tests for event thread and queue timing
Adjust tests to reflect attribute rename from client_thread to _event_thread, add time import, and add a short sleep in the incoming-queue test to allow the multiprocessing.Queue feeder thread to flush the item to the pipe (fixes a flaky timing issue).1 parent eb0c024 commit 1e92fbe
2 files changed
Lines changed: 313 additions & 141 deletions
0 commit comments