Skip to content

Commit a39d1fe

Browse files
committed
Update to use new websockets version
1 parent 30f2e23 commit a39d1fe

File tree

3 files changed

+6
-22
lines changed

3 files changed

+6
-22
lines changed

cryptofeed/connection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
from aiohttp.client_reqrep import ClientResponse
1818
import requests
19-
import websockets
19+
from websockets.asyncio.client import connect
20+
from websockets.protocol import State
2021
import aiohttp
2122
from aiohttp.typedefs import StrOrURL
2223
from yapic import json as json_parser
@@ -303,7 +304,7 @@ def __init__(self, address: str, conn_id: str, authentication=None, subscription
303304

304305
@property
305306
def is_open(self) -> bool:
306-
return self.conn and not self.conn.closed
307+
return self.conn and not self.conn.state == State.CLOSED
307308

308309
async def _open(self):
309310
if self.is_open:
@@ -315,7 +316,7 @@ async def _open(self):
315316
if self.authentication:
316317
self.address, self.ws_kwargs = await self.authentication(self.address, self.ws_kwargs)
317318

318-
self.conn = await websockets.connect(self.address, **self.ws_kwargs)
319+
self.conn = await connect(self.address, **self.ws_kwargs)
319320
self.sent = 0
320321
self.received = 0
321322
self.last_message = None
@@ -357,7 +358,7 @@ class WebsocketEndpoint:
357358
authentication: bool = None
358359

359360
def __post_init__(self):
360-
defaults = {'ping_interval': 10, 'ping_timeout': None, 'max_size': 2**23, 'max_queue': None, 'read_limit': 2**18}
361+
defaults = {'ping_interval': 10, 'ping_timeout': None, 'max_size': None, 'max_queue': None}
361362
if self.options:
362363
defaults.update(self.options)
363364
self.options = defaults

cryptofeed/connection_handler.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import zlib
1414

1515
from websockets import ConnectionClosed
16-
from websockets.exceptions import InvalidStatusCode
1716

1817
from cryptofeed.connection import AsyncConnection
1918
from cryptofeed.exceptions import ExhaustedRetries
@@ -77,22 +76,6 @@ async def _create_connection(self):
7776
await asyncio.sleep(delay)
7877
retries += 1
7978
delay *= 2
80-
except InvalidStatusCode as e:
81-
if self.exceptions:
82-
for ex in self.exceptions:
83-
if isinstance(e, ex):
84-
LOG.warning("%s: encountered exception %s, which is on the ignore list. Raising", self.conn.uuid, str(e))
85-
raise
86-
if e.status_code == 429:
87-
rand = random.uniform(1.0, 3.0)
88-
LOG.warning("%s: Rate Limited - waiting %d seconds to reconnect", self.conn.uuid, (rate_limited * 60 * rand))
89-
await asyncio.sleep(rate_limited * 60 * rand)
90-
rate_limited += 1
91-
else:
92-
LOG.warning("%s: encountered connection issue %s - reconnecting in %.1f seconds...", self.conn.uuid, str(e), delay, exc_info=True)
93-
await asyncio.sleep(delay)
94-
retries += 1
95-
delay *= 2
9679
except Exception as e:
9780
if self.exceptions:
9881
for ex in self.exceptions:

examples/demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def main():
106106
f.add_feed(HitBTC(channels=[TRADES], symbols=['BTC-USDT'], callbacks={TRADES: trade}))
107107
f.add_feed(Huobi(symbols=['BTC-USDT'], channels=[CANDLES, TRADES, L2_BOOK], callbacks={TRADES: trade, L2_BOOK: book, CANDLES: candle_callback}))
108108
f.add_feed(HuobiDM(subscription={L2_BOOK: HuobiDM.symbols()[:2], TRADES: HuobiDM.symbols()[:10]}, callbacks={TRADES: trade, L2_BOOK: book}))
109-
pairs = ['BTC-USD-PERP', 'ETH-USD-PERP', 'EOS-USD-PERP', 'BCH-USD-PERP', 'BSV-USD-PERP', 'LTC-USD-PERP']
109+
pairs = ['BTC-USD-PERP', 'ETH-USD-PERP', 'LTC-USD-PERP']
110110
f.add_feed(HuobiSwap(symbols=pairs, channels=[TRADES, L2_BOOK, FUNDING], callbacks={FUNDING: funding, TRADES: trade, L2_BOOK: book}))
111111
f.add_feed(KrakenFutures(symbols=KrakenFutures.symbols(), channels=[L2_BOOK, TICKER, TRADES, OPEN_INTEREST, FUNDING], callbacks={L2_BOOK: book, FUNDING: funding, OPEN_INTEREST: oi, TRADES: trade, TICKER: ticker}))
112112
f.add_feed(Kraken(config='config.yaml', checksum_validation=True, subscription={L2_BOOK: ['BTC-USD'], TRADES: ['BTC-USD'], CANDLES: ['BTC-USD'], TICKER: ['ETH-USD']}, callbacks={L2_BOOK: book, CANDLES: candle_callback, TRADES: trade, TICKER: ticker}))

0 commit comments

Comments
 (0)