Skip to content

Commit dff993c

Browse files
Merge pull request #48 from blocknative/fix/reconnect-on-ping-timeout
0.2.9: Reconnect on TooSlow error
2 parents 9fb3d52 + a900bab commit dff993c

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

blocknative/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.8'
1+
__version__ = '0.2.9'

blocknative/stream.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import List, Mapping, Callable, Union
1010
import trio
1111
import logging
12+
from logging import INFO
1213
from trio_websocket import (
1314
open_websocket_url,
1415
ConnectionClosed,
@@ -24,8 +25,12 @@
2425
SubscriptionType,
2526
to_camel_case,
2627
)
28+
2729
from blocknative import __version__ as API_VERSION
2830

31+
FORMAT = "%(asctime)s [%(levelname)s]: %(message)s"
32+
logging.basicConfig(format=FORMAT, level=INFO)
33+
2934
PING_INTERVAL = 15
3035
PING_TIMEOUT = 10
3136
MESSAGE_SEND_INTERVAL = 0.021 # 21ms
@@ -174,7 +179,11 @@ def subscribe_txn(self, tx_hash: str, callback: Callback, status: str = "sent"):
174179
self._send_txn_watch_message(tx_hash, status)
175180

176181
def connect(self, base_url: str = BN_BASE_URL):
177-
"""Initializes the connection to the WebSocket server."""
182+
"""Initializes the connection to the WebSocket server.
183+
184+
Args:
185+
base_url: The websocket url to connect to. Useful for when using a proxy.
186+
"""
178187
try:
179188
return trio.run(self._connect, base_url)
180189
except KeyboardInterrupt:
@@ -267,6 +276,15 @@ async def _message_handler(self, message: dict):
267276
)
268277

269278
def unsubscribe(self, watched_address):
279+
"""Unsubscribe from the current stream.
280+
281+
Note:
282+
This function is passed as a parameter to the to the transaction callback that you provide.
283+
284+
Args:
285+
watched_address: The address to unsubscribe from.
286+
"""
287+
270288
# remove this subscription from the registry so that we don't execute the callback
271289
del self._subscription_registry[watched_address]
272290

@@ -330,7 +348,12 @@ async def _handle_connection(self, base_url: str):
330348
nursery.start_soon(self._heartbeat)
331349
nursery.start_soon(self._poll_messages)
332350
nursery.start_soon(self._message_dispatcher)
333-
except (ConnectionClosed, trio.MultiError) as error:
351+
except (ConnectionClosed, trio.MultiError, trio.TooSlowError) as error:
352+
if isinstance(error, trio.TooSlowError):
353+
logging.warn(
354+
f"Server failed to respond to ping within the given timeout of {PING_TIMEOUT} seconds."
355+
)
356+
logging.info("Attempting to reconnect...")
334357
# If server times the connection out or drops, reconnect
335358
await trio.sleep(0.5)
336359
await self._connect(base_url)

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Application Programming Interface
1818
global_filters = [{ "status": "pending" }]
1919
Stream(API_KEY, BLOCKCHAIN, network_id, global_filters)
2020
21+
.. autofunction:: blocknative.stream.Stream.connect
2122

2223
.. autofunction:: blocknative.stream.Stream.subscribe_address
2324

0 commit comments

Comments
 (0)