From 58109037df54cbb19c6bd2e1b9fbed8f6e2606ec Mon Sep 17 00:00:00 2001 From: mihaimarcu Date: Mon, 28 Sep 2026 14:21:23 +0300 Subject: [PATCH 1/2] shutdown change --- lighter/ws_client.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lighter/ws_client.py b/lighter/ws_client.py index 14abdf5..8f390fb 100644 --- a/lighter/ws_client.py +++ b/lighter/ws_client.py @@ -3,6 +3,8 @@ from websockets.client import connect as connect_async from lighter.configuration import Configuration from lighter.endpoint_profiles import join_url +import logging +logger = logging.getLogger(__name__) class WsClient: def __init__( @@ -13,6 +15,8 @@ def __init__( account_ids=[], on_order_book_update=print, on_account_update=print, + on_shutdown=None, + on_unhandled_message=None, ws_url=None, ): if ws_url is not None: @@ -35,6 +39,8 @@ def __init__( self.on_order_book_update = on_order_book_update self.on_account_update = on_account_update + self.on_shutdown = on_shutdown + self.on_unhandled_message = on_unhandled_message self.ws = None @@ -57,6 +63,8 @@ def on_message(self, ws, message): elif message_type == "ping": # Respond to ping with pong ws.send(json.dumps({"type": "pong"})) + elif message_type == "shutdown": + self.handle_shutdown(message) else: self.handle_unhandled_message(message) @@ -149,7 +157,17 @@ def handle_update_account(self, message): self.on_account_update(account_id, self.account_states[account_id]) def handle_unhandled_message(self, message): - raise Exception(f"Unhandled message: {message}") + if self.on_unhandled_message: + self.on_unhandled_message(message) + else: + logger.warning("Unhandled message type %r: %s", message.get("type"), message) + + def handle_shutdown(self, message): + close_in_ms = message.get("close_in_ms", 0) + if self.on_shutdown: + self.on_shutdown(close_in_ms) + else: + print(f"Server shutting down, connection closes in {close_in_ms}ms") def on_error(self, ws, error): raise Exception(f"Error: {error}") From 0b1619be928632cc3ee3eed0120c753ff9dd431d Mon Sep 17 00:00:00 2001 From: mihaimarcu Date: Mon, 28 Sep 2026 14:34:07 +0300 Subject: [PATCH 2/2] fix --- lighter/ws_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighter/ws_client.py b/lighter/ws_client.py index 8f390fb..0638875 100644 --- a/lighter/ws_client.py +++ b/lighter/ws_client.py @@ -15,9 +15,9 @@ def __init__( account_ids=[], on_order_book_update=print, on_account_update=print, + ws_url=None, on_shutdown=None, on_unhandled_message=None, - ws_url=None, ): if ws_url is not None: self.base_url = ws_url.rstrip("/")