From 249f19fe417762d1dbdeee4ff322985b797397df Mon Sep 17 00:00:00 2001 From: Davide Casale Date: Tue, 14 Mar 2023 19:03:05 +0100 Subject: [PATCH] Add ZeroConnectionsError in bfxapi.websocket.exceptions. Add log if connections = 0 in BfxWebsocketClient::start. Add raise ZeroConnectionError in BfxWebsocketClient::subscribe if connections equals 0. --- .pylintrc | 1 - .../websocket/client/bfx_websocket_client.py | 12 ++++++-- bfxapi/websocket/exceptions.py | 28 +++++++++++-------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/.pylintrc b/.pylintrc index e3196fc..996e616 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,6 +1,5 @@ [MAIN] py-version=3.8.0 -ignore=examples [MESSAGES CONTROL] disable= diff --git a/bfxapi/websocket/client/bfx_websocket_client.py b/bfxapi/websocket/client/bfx_websocket_client.py index 3f893eb..a88d3cd 100644 --- a/bfxapi/websocket/client/bfx_websocket_client.py +++ b/bfxapi/websocket/client/bfx_websocket_client.py @@ -13,7 +13,7 @@ from .bfx_websocket_bucket import _HEARTBEAT, F, _require_websocket_connection, from .bfx_websocket_inputs import BfxWebsocketInputs from ..handlers import PublicChannelsHandler, AuthenticatedChannelsHandler from ..exceptions import WebsocketAuthenticationRequired, InvalidAuthenticationCredentials, EventNotSupported, \ - OutdatedClientVersion + ZeroConnectionsError, OutdatedClientVersion from ...utils.json_encoder import JSONEncoder @@ -85,6 +85,10 @@ class BfxWebsocketClient: return asyncio.run(self.start(connections)) async def start(self, connections = 5): + if connections == 0: + self.logger.info("With connections set to 0 it will not be possible to subscribe to any public channel. " \ + "Attempting a subscription will cause a ZeroConnectionsError to be thrown.") + if connections > BfxWebsocketClient.MAXIMUM_CONNECTIONS_AMOUNT: self.logger.warning(f"It is not safe to use more than {BfxWebsocketClient.MAXIMUM_CONNECTIONS_AMOUNT} " \ f"buckets from the same connection ({connections} in use), the server could momentarily " \ @@ -121,7 +125,8 @@ class BfxWebsocketClient: self.websocket, self.authentication = websocket, False - if await asyncio.gather(*[on_open_event.wait() for on_open_event in self.on_open_events]): + if len(self.on_open_events) == 0 or \ + (await asyncio.gather(*[on_open_event.wait() for on_open_event in self.on_open_events])): self.event_emitter.emit("open") if self.credentials: @@ -202,6 +207,9 @@ class BfxWebsocketClient: await self.websocket.send(json.dumps(data)) async def subscribe(self, channel, **kwargs): + if len(self.buckets) == 0: + raise ZeroConnectionsError("Unable to subscribe: the number of connections must be greater than 0.") + counters = [ len(bucket.pendings) + len(bucket.subscriptions) for bucket in self.buckets ] index = counters.index(min(counters)) diff --git a/bfxapi/websocket/exceptions.py b/bfxapi/websocket/exceptions.py index 49049fd..e1ff53e 100644 --- a/bfxapi/websocket/exceptions.py +++ b/bfxapi/websocket/exceptions.py @@ -5,9 +5,11 @@ __all__ = [ "ConnectionNotOpen", "TooManySubscriptions", + "ZeroConnectionsError", "WebsocketAuthenticationRequired", "InvalidAuthenticationCredentials", "EventNotSupported", + "HandlerNotFound", "OutdatedClientVersion" ] @@ -26,29 +28,33 @@ class TooManySubscriptions(BfxWebsocketException): This error indicates a subscription attempt after reaching the limit of simultaneous connections. """ +class ZeroConnectionsError(BfxWebsocketException): + """ + This error indicates an attempt to subscribe to a public channel while the number of connections is 0. + """ + class WebsocketAuthenticationRequired(BfxWebsocketException): """ This error indicates an attempt to access a protected resource without logging in first. """ +class InvalidAuthenticationCredentials(BfxWebsocketException): + """ + This error indicates that the user has provided incorrect credentials (API-KEY and API-SECRET) for authentication. + """ + class EventNotSupported(BfxWebsocketException): """ This error indicates a failed attempt to subscribe to an event not supported by the BfxWebsocketClient. """ -class OutdatedClientVersion(BfxWebsocketException): - """ - This error indicates a mismatch between the client version and the server WSS version. - """ - - -class InvalidAuthenticationCredentials(BfxWebsocketException): - """ - This error indicates that the user has provided incorrect credentials (API-KEY and API-SECRET) for authentication. - """ - class HandlerNotFound(BfxWebsocketException): """ This error indicates that a handler was not found for an incoming message. """ + +class OutdatedClientVersion(BfxWebsocketException): + """ + This error indicates a mismatch between the client version and the server WSS version. + """