@@ -25,7 +25,7 @@ class BinanceSocketType(str, Enum):
2525
2626class BinanceSocketManager :
2727 STREAM_URL = "wss://stream.binance.{}:9443/"
28- STREAM_TESTNET_URL = "wss://testnet.binance.vision/"
28+ STREAM_TESTNET_URL = "wss://stream. testnet.binance.vision/"
2929 FSTREAM_URL = "wss://fstream.binance.{}/"
3030 FSTREAM_TESTNET_URL = "wss://stream.binancefuture.com/"
3131 DSTREAM_URL = "wss://dstream.binance.{}/"
@@ -36,12 +36,19 @@ class BinanceSocketManager:
3636 WEBSOCKET_DEPTH_10 = "10"
3737 WEBSOCKET_DEPTH_20 = "20"
3838
39- def __init__ (self , client : AsyncClient , user_timeout = KEEPALIVE_TIMEOUT ):
39+ def __init__ (
40+ self ,
41+ client : AsyncClient ,
42+ user_timeout = KEEPALIVE_TIMEOUT ,
43+ max_queue_size : int = 100 ,
44+ ):
4045 """Initialise the BinanceSocketManager
4146
4247 :param client: Binance API client
4348 :type client: binance.AsyncClient
44-
49+ :param user_timeout: Timeout for user socket in seconds
50+ :param max_queue_size: Max size of the websocket queue, defaults to 100
51+ :type max_queue_size: int
4552 """
4653 self .STREAM_URL = self .STREAM_URL .format (client .tld )
4754 self .FSTREAM_URL = self .FSTREAM_URL .format (client .tld )
@@ -52,8 +59,8 @@ def __init__(self, client: AsyncClient, user_timeout=KEEPALIVE_TIMEOUT):
5259 self ._loop = get_loop ()
5360 self ._client = client
5461 self ._user_timeout = user_timeout
55-
5662 self .testnet = self ._client .testnet
63+ self ._max_queue_size = max_queue_size
5764 self .ws_kwargs = {}
5865
5966 def _get_stream_url (self , stream_url : Optional [str ] = None ):
@@ -84,6 +91,7 @@ def _get_socket(
8491 exit_coro = lambda p : self ._exit_socket (f"{ socket_type } _{ p } " ),
8592 is_binary = is_binary ,
8693 https_proxy = self ._client .https_proxy ,
94+ max_queue_size = self ._max_queue_size ,
8795 ** self .ws_kwargs ,
8896 )
8997
@@ -1202,6 +1210,7 @@ def __init__(
12021210 session_params : Optional [Dict [str , Any ]] = None ,
12031211 https_proxy : Optional [str ] = None ,
12041212 loop : Optional [asyncio .AbstractEventLoop ] = None ,
1213+ max_queue_size : int = 100 ,
12051214 ):
12061215 super ().__init__ (
12071216 api_key ,
@@ -1214,10 +1223,14 @@ def __init__(
12141223 loop ,
12151224 )
12161225 self ._bsm : Optional [BinanceSocketManager ] = None
1226+ self ._max_queue_size = max_queue_size
12171227
12181228 async def _before_socket_listener_start (self ):
12191229 assert self ._client
1220- self ._bsm = BinanceSocketManager (client = self ._client )
1230+ self ._bsm = BinanceSocketManager (
1231+ client = self ._client ,
1232+ max_queue_size = self ._max_queue_size
1233+ )
12211234
12221235 def _start_async_socket (
12231236 self ,
@@ -1226,7 +1239,10 @@ def _start_async_socket(
12261239 params : Dict [str , Any ],
12271240 path : Optional [str ] = None ,
12281241 ) -> str :
1242+ start_time = time .time ()
12291243 while not self ._bsm :
1244+ if time .time () - start_time > 5 :
1245+ raise RuntimeError ("Binance Socket Manager failed to initialize after 5 seconds" )
12301246 time .sleep (0.1 )
12311247 socket = getattr (self ._bsm , socket_name )(** params )
12321248 socket_path : str = path or socket ._path # noqa
0 commit comments