mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-23 00:34:22 +01:00
Improve reconnections by not emitting againg once events.
This commit is contained in:
@@ -24,12 +24,13 @@ class BfxWebsocketBucket:
|
||||
|
||||
MAXIMUM_SUBSCRIPTIONS_AMOUNT = 25
|
||||
|
||||
def __init__(self, host, event_emitter):
|
||||
self.host, self.event_emitter, self.on_open_event = host, event_emitter, asyncio.locks.Event()
|
||||
|
||||
def __init__(self, host, event_emitter, events_per_subscription):
|
||||
self.host, self.event_emitter, self.events_per_subscription = host, event_emitter, events_per_subscription
|
||||
self.websocket, self.subscriptions, self.pendings = None, {}, []
|
||||
self.on_open_event = asyncio.locks.Event()
|
||||
|
||||
self.handler = PublicChannelsHandler(event_emitter=self.event_emitter)
|
||||
self.handler = PublicChannelsHandler(event_emitter=self.event_emitter, \
|
||||
events_per_subscription=self.events_per_subscription)
|
||||
|
||||
async def connect(self):
|
||||
async def _connection():
|
||||
@@ -43,12 +44,16 @@ class BfxWebsocketBucket:
|
||||
|
||||
if isinstance(message, dict):
|
||||
if message["event"] == "subscribed" and (chan_id := message["chanId"]):
|
||||
self.pendings = \
|
||||
[ pending for pending in self.pendings if pending["subId"] != message["subId"] ]
|
||||
self.pendings = [ pending \
|
||||
for pending in self.pendings if pending["subId"] != message["subId"] ]
|
||||
|
||||
self.subscriptions[chan_id] = message
|
||||
|
||||
self.event_emitter.emit("subscribed", message)
|
||||
sub_id = message["subId"]
|
||||
|
||||
if "subscribed" not in self.events_per_subscription.get(sub_id, []):
|
||||
self.events_per_subscription.setdefault(sub_id, []).append("subscribed")
|
||||
self.event_emitter.emit("subscribed", message)
|
||||
elif message["event"] == "unsubscribed" and (chan_id := message["chanId"]):
|
||||
if message["status"] == "OK":
|
||||
del self.subscriptions[chan_id]
|
||||
@@ -70,7 +75,7 @@ class BfxWebsocketBucket:
|
||||
await self.websocket.send(json.dumps(pending))
|
||||
|
||||
for _, subscription in self.subscriptions.items():
|
||||
await self.subscribe(**subscription)
|
||||
await self.subscribe(sub_id=subscription.pop("subId"), **subscription)
|
||||
|
||||
self.subscriptions.clear()
|
||||
|
||||
|
||||
@@ -55,19 +55,25 @@ class BfxWebsocketClient:
|
||||
|
||||
MAXIMUM_CONNECTIONS_AMOUNT = 20
|
||||
|
||||
ONCE_EVENTS = [ "open", "authenticated", "disconnection" ]
|
||||
ONCE_EVENTS = [
|
||||
"open", "authenticated", "disconnection",
|
||||
*AuthenticatedEventsHandler.ONCE_EVENTS
|
||||
]
|
||||
|
||||
EVENTS = [
|
||||
*ONCE_EVENTS, "subscribed", "wss-error",
|
||||
"subscribed", "wss-error",
|
||||
*ONCE_EVENTS,
|
||||
*PublicChannelsHandler.EVENTS,
|
||||
*AuthenticatedEventsHandler.EVENTS
|
||||
*AuthenticatedEventsHandler.ON_EVENTS
|
||||
]
|
||||
|
||||
def __init__(self, host, credentials, *, wss_timeout = 60 * 15, log_filename = None, log_level = "INFO"):
|
||||
self.websocket, self.buckets, self.authentication = None, [], False
|
||||
self.websocket, self.authentication, self.buckets = None, False, []
|
||||
|
||||
self.host, self.credentials, self.wss_timeout = host, credentials, wss_timeout
|
||||
|
||||
self.events_per_subscription = {}
|
||||
|
||||
self.event_emitter = AsyncIOEventEmitter()
|
||||
|
||||
self.handler = AuthenticatedEventsHandler(event_emitter=self.event_emitter)
|
||||
@@ -97,7 +103,7 @@ class BfxWebsocketClient:
|
||||
"block the client with <429 Too Many Requests>.")
|
||||
|
||||
for _ in range(connections):
|
||||
self.buckets += [BfxWebsocketBucket(self.host, self.event_emitter)]
|
||||
self.buckets += [BfxWebsocketBucket(self.host, self.event_emitter, self.events_per_subscription)]
|
||||
|
||||
await self.__connect()
|
||||
|
||||
@@ -105,10 +111,10 @@ class BfxWebsocketClient:
|
||||
async def __connect(self):
|
||||
Reconnection = namedtuple("Reconnection", ["status", "attempts", "timestamp"])
|
||||
reconnection = Reconnection(status=False, attempts=0, timestamp=None)
|
||||
delay = _Delay(backoff_factor=1.618)
|
||||
|
||||
timer, tasks, on_timeout_event = None, [], asyncio.locks.Event()
|
||||
|
||||
delay = None
|
||||
|
||||
def _on_wss_timeout():
|
||||
on_timeout_event.set()
|
||||
|
||||
@@ -125,7 +131,7 @@ class BfxWebsocketClient:
|
||||
|
||||
timer.cancel()
|
||||
|
||||
self.websocket, self.authentication = websocket, False
|
||||
self.websocket = websocket
|
||||
|
||||
coroutines = [ BfxWebsocketBucket.connect(bucket) for bucket in self.buckets ]
|
||||
|
||||
@@ -192,8 +198,10 @@ class BfxWebsocketClient:
|
||||
task.cancel()
|
||||
|
||||
reconnection = Reconnection(status=True, attempts=1, timestamp=datetime.now())
|
||||
|
||||
timer = asyncio.get_event_loop().call_later(self.wss_timeout, _on_wss_timeout)
|
||||
delay = _Delay(backoff_factor=1.618)
|
||||
|
||||
self.authentication = False
|
||||
elif isinstance(error, socket.gaierror) and reconnection.status:
|
||||
self.logger.warning(f"Reconnection attempt no.{reconnection.attempts} has failed. " \
|
||||
f"Next reconnection attempt in ~{round(delay.peek()):.1f} seconds. (at the moment " \
|
||||
|
||||
@@ -5,16 +5,24 @@ from .. serializers import _Notification
|
||||
from .. exceptions import HandlerNotFound
|
||||
|
||||
class AuthenticatedEventsHandler:
|
||||
__abbreviations = {
|
||||
"os": "order_snapshot", "on": "order_new", "ou": "order_update",
|
||||
"oc": "order_cancel", "ps": "position_snapshot", "pn": "position_new",
|
||||
"pu": "position_update", "pc": "position_close", "te": "trade_executed",
|
||||
"tu": "trade_execution_update", "fos": "funding_offer_snapshot", "fon": "funding_offer_new",
|
||||
"fou": "funding_offer_update", "foc": "funding_offer_cancel", "fcs": "funding_credit_snapshot",
|
||||
__once_abbreviations = {
|
||||
"os": "order_snapshot", "ps": "position_snapshot", "fos": "funding_offer_snapshot",
|
||||
"fcs": "funding_credit_snapshot", "fls": "funding_loan_snapshot", "ws": "wallet_snapshot"
|
||||
}
|
||||
|
||||
__on_abbreviations = {
|
||||
"on": "order_new", "ou": "order_update", "oc": "order_cancel",
|
||||
"pn": "position_new", "pu": "position_update", "pc": "position_close",
|
||||
"fon": "funding_offer_new", "fou": "funding_offer_update", "foc": "funding_offer_cancel",
|
||||
"fcn": "funding_credit_new", "fcu": "funding_credit_update", "fcc": "funding_credit_close",
|
||||
"fls": "funding_loan_snapshot", "fln": "funding_loan_new", "flu": "funding_loan_update",
|
||||
"flc": "funding_loan_close", "ws": "wallet_snapshot", "wu": "wallet_update",
|
||||
"bu": "balance_update",
|
||||
"fln": "funding_loan_new", "flu": "funding_loan_update", "flc": "funding_loan_close",
|
||||
"te": "trade_executed", "tu": "trade_execution_update", "wu": "wallet_update",
|
||||
"bu": "balance_update"
|
||||
}
|
||||
|
||||
__abbreviations = {
|
||||
**__once_abbreviations,
|
||||
**__on_abbreviations
|
||||
}
|
||||
|
||||
__serializers = {
|
||||
@@ -28,12 +36,15 @@ class AuthenticatedEventsHandler:
|
||||
("bu",): serializers.Balance
|
||||
}
|
||||
|
||||
EVENTS = [
|
||||
"notification",
|
||||
"on-req-notification", "ou-req-notification", "oc-req-notification",
|
||||
"oc_multi-notification",
|
||||
"fon-req-notification", "foc-req-notification",
|
||||
*list(__abbreviations.values())
|
||||
ONCE_EVENTS = [
|
||||
*list(__once_abbreviations.values())
|
||||
]
|
||||
|
||||
ON_EVENTS = [
|
||||
*list(__on_abbreviations.values()),
|
||||
"notification", "on-req-notification", "ou-req-notification",
|
||||
"oc-req-notification", "oc_multi-notification", "fon-req-notification",
|
||||
"foc-req-notification"
|
||||
]
|
||||
|
||||
def __init__(self, event_emitter):
|
||||
|
||||
@@ -3,15 +3,23 @@ from .. import serializers
|
||||
from .. exceptions import HandlerNotFound
|
||||
|
||||
class PublicChannelsHandler:
|
||||
EVENTS = [
|
||||
"t_ticker_update", "f_ticker_update", "t_trade_executed", "t_trade_execution_update", "f_trade_executed",
|
||||
"f_trade_execution_update", "t_trades_snapshot", "f_trades_snapshot", "t_book_snapshot", "f_book_snapshot",
|
||||
"t_raw_book_snapshot", "f_raw_book_snapshot", "t_book_update", "f_book_update", "t_raw_book_update",
|
||||
"f_raw_book_update", "candles_snapshot", "candles_update", "derivatives_status_update",
|
||||
ONCE_PER_SUBSCRIPTION_EVENTS = [
|
||||
"t_trades_snapshot", "f_trades_snapshot", "t_book_snapshot",
|
||||
"f_book_snapshot", "t_raw_book_snapshot", "f_raw_book_snapshot",
|
||||
"candles_snapshot"
|
||||
]
|
||||
|
||||
def __init__(self, event_emitter):
|
||||
self.event_emitter = event_emitter
|
||||
EVENTS = [
|
||||
*ONCE_PER_SUBSCRIPTION_EVENTS,
|
||||
"t_ticker_update", "f_ticker_update", "t_trade_executed",
|
||||
"t_trade_execution_update", "f_trade_executed", "f_trade_execution_update",
|
||||
"t_book_update", "f_book_update", "t_raw_book_update",
|
||||
"f_raw_book_update", "candles_update", "derivatives_status_update"
|
||||
]
|
||||
|
||||
def __init__(self, event_emitter, events_per_subscription):
|
||||
self.__event_emitter, self.__events_per_subscription = \
|
||||
event_emitter, events_per_subscription
|
||||
|
||||
self.__handlers = {
|
||||
"ticker": self.__ticker_channel_handler,
|
||||
@@ -31,16 +39,29 @@ class PublicChannelsHandler:
|
||||
|
||||
raise HandlerNotFound(f"No handler found for channel <{subscription['channel']}>.")
|
||||
|
||||
def __emit(self, event, sub, data):
|
||||
sub_id, should_emit_event = sub["subId"], True
|
||||
|
||||
if event in PublicChannelsHandler.ONCE_PER_SUBSCRIPTION_EVENTS:
|
||||
if sub_id not in self.__events_per_subscription:
|
||||
self.__events_per_subscription[sub_id] = [ event ]
|
||||
elif event not in self.__events_per_subscription[sub_id]:
|
||||
self.__events_per_subscription[sub_id] += [ event ]
|
||||
else: should_emit_event = False
|
||||
|
||||
if should_emit_event:
|
||||
return self.__event_emitter.emit(event, sub, data)
|
||||
|
||||
def __ticker_channel_handler(self, subscription, *stream):
|
||||
if subscription["symbol"].startswith("t"):
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
"t_ticker_update",
|
||||
subscription,
|
||||
serializers.TradingPairTicker.parse(*stream[0])
|
||||
)
|
||||
|
||||
if subscription["symbol"].startswith("f"):
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
"f_ticker_update",
|
||||
subscription,
|
||||
serializers.FundingCurrencyTicker.parse(*stream[0])
|
||||
@@ -49,28 +70,28 @@ class PublicChannelsHandler:
|
||||
def __trades_channel_handler(self, subscription, *stream):
|
||||
if (event := stream[0]) and event in [ "te", "tu", "fte", "ftu" ]:
|
||||
if subscription["symbol"].startswith("t"):
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
{ "te": "t_trade_executed", "tu": "t_trade_execution_update" }[event],
|
||||
subscription,
|
||||
serializers.TradingPairTrade.parse(*stream[1])
|
||||
)
|
||||
|
||||
if subscription["symbol"].startswith("f"):
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
{ "fte": "f_trade_executed", "ftu": "f_trade_execution_update" }[event],
|
||||
subscription,
|
||||
serializers.FundingCurrencyTrade.parse(*stream[1])
|
||||
)
|
||||
|
||||
if subscription["symbol"].startswith("t"):
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
"t_trades_snapshot",
|
||||
subscription,
|
||||
[ serializers.TradingPairTrade.parse(*substream) for substream in stream[0] ]
|
||||
)
|
||||
|
||||
if subscription["symbol"].startswith("f"):
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
"f_trades_snapshot",
|
||||
subscription,
|
||||
[ serializers.FundingCurrencyTrade.parse(*substream) for substream in stream[0] ]
|
||||
@@ -86,14 +107,14 @@ class PublicChannelsHandler:
|
||||
serializers.TradingPairBook, serializers.FundingCurrencyBook, False
|
||||
|
||||
if all(isinstance(substream, list) for substream in stream[0]):
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
event + "_" + (is_raw_book and "raw_book" or "book") + "_snapshot",
|
||||
subscription,
|
||||
[ { "t": _trading_pair_serializer, "f": _funding_currency_serializer }[event] \
|
||||
.parse(*substream) for substream in stream[0] ]
|
||||
)
|
||||
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
event + "_" + (is_raw_book and "raw_book" or "book") + "_update",
|
||||
subscription,
|
||||
{ "t": _trading_pair_serializer, "f": _funding_currency_serializer }[event].parse(*stream[0])
|
||||
@@ -101,13 +122,13 @@ class PublicChannelsHandler:
|
||||
|
||||
def __candles_channel_handler(self, subscription, *stream):
|
||||
if all(isinstance(substream, list) for substream in stream[0]):
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
"candles_snapshot",
|
||||
subscription,
|
||||
[ serializers.Candle.parse(*substream) for substream in stream[0] ]
|
||||
)
|
||||
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
"candles_update",
|
||||
subscription,
|
||||
serializers.Candle.parse(*stream[0])
|
||||
@@ -115,7 +136,7 @@ class PublicChannelsHandler:
|
||||
|
||||
def __status_channel_handler(self, subscription, *stream):
|
||||
if subscription["key"].startswith("deriv:"):
|
||||
return self.event_emitter.emit(
|
||||
return self.__emit(
|
||||
"derivatives_status_update",
|
||||
subscription,
|
||||
serializers.DerivativesStatus.parse(*stream[0])
|
||||
|
||||
Reference in New Issue
Block a user