Skip to content

Commit bda5198

Browse files
committed
Refine Betfair client reconnects
1 parent 55bae96 commit bda5198

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

nautilus_trader/adapters/betfair/sockets.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,11 @@ async def connect(self):
8585
self._client = await SocketClient.connect(
8686
config,
8787
None,
88+
# self.post_connection, # this method itself needs the `self._client` reference
8889
self.post_reconnection,
89-
None,
90-
# TODO - waiting for async handling
91-
# self.post_connection,
92-
# self.post_reconnection,
93-
# self.post_disconnection,
90+
self.post_disconnection,
9491
)
95-
self._log.debug("Running post connect")
96-
await self.post_connection()
92+
await self._post_connection()
9793

9894
self.is_connected = True
9995
self._log.info("Connected")
@@ -108,9 +104,6 @@ async def reconnect(self):
108104

109105
await self._client.reconnect()
110106

111-
self._log.debug("Running post connection")
112-
await self.post_connection()
113-
114107
self.is_connected = True
115108
self._log.info("Reconnected")
116109

@@ -124,13 +117,13 @@ async def disconnect(self):
124117

125118
await self._client.close()
126119

127-
self._log.debug("Running post disconnect")
128-
await self.post_disconnection()
129-
130120
self.is_connected = False
131121
self._log.info("Disconnected")
132122

133-
async def post_connection(self) -> None:
123+
async def _post_connection(self) -> None:
124+
pass
125+
126+
def post_connection(self) -> None:
134127
"""
135128
Actions to be performed post connection.
136129
"""
@@ -140,13 +133,12 @@ def post_reconnection(self) -> None:
140133
Actions to be performed post connection.
141134
"""
142135

143-
async def post_disconnection(self) -> None:
136+
def post_disconnection(self) -> None:
144137
"""
145138
Actions to be performed post disconnection.
146139
"""
147140

148141
async def send(self, message: bytes) -> None:
149-
self._log.debug(f"[SEND] {message.decode()}")
150142
if self._client is None:
151143
raise RuntimeError("Cannot send message: no client")
152144

@@ -201,8 +193,13 @@ def __init__(
201193
"partitionMatchedByStrategyRef": partition_matched_by_strategy_ref,
202194
}
203195

204-
async def post_connection(self):
205-
await super().post_connection()
196+
def post_connection(self):
197+
self._loop.create_task(self._post_connection())
198+
199+
def post_reconnection(self):
200+
self._loop.create_task(self._post_connection())
201+
202+
async def _post_connection(self):
206203
subscribe_msg = {
207204
"op": "orderSubscription",
208205
"id": self.unique_id,
@@ -213,10 +210,6 @@ async def post_connection(self):
213210
await self.send(msgspec.json.encode(self.auth_message()))
214211
await self.send(msgspec.json.encode(subscribe_msg))
215212

216-
def post_reconnection(self):
217-
super().post_reconnection()
218-
self._loop.create_task(self.post_connection())
219-
220213

221214
class BetfairMarketStreamClient(BetfairStreamClient):
222215
"""
@@ -319,10 +312,12 @@ async def send_subscription_message(
319312
}
320313
await self.send(msgspec.json.encode(message))
321314

322-
async def post_connection(self) -> None:
323-
await super().post_connection()
324-
await self.send(msgspec.json.encode(self.auth_message()))
315+
def post_connection(self) -> None:
316+
self._loop.create_task(self._post_connection())
325317

326318
def post_reconnection(self) -> None:
327319
super().post_reconnection()
328-
self._loop.create_task(self.post_connection())
320+
self._loop.create_task(self._post_connection())
321+
322+
async def _post_connection(self) -> None:
323+
await self.send(msgspec.json.encode(self.auth_message()))

0 commit comments

Comments
 (0)