Add ZeroConnectionsError in bfxapi.websocket.exceptions. Add log if connections = 0 in BfxWebsocketClient::start. Add raise ZeroConnectionError in BfxWebsocketClient::subscribe if connections equals 0.

This commit is contained in:
Davide Casale
2023-03-14 19:03:05 +01:00
parent 98c3e4f352
commit 249f19fe41
3 changed files with 27 additions and 14 deletions

View File

@@ -1,6 +1,5 @@
[MAIN]
py-version=3.8.0
ignore=examples
[MESSAGES CONTROL]
disable=

View File

@@ -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))

View File

@@ -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.
"""