예제 #1
0
    async def _connect(self, conn, subscribe, handler):
        """
        Connect to exchange and subscribe
        """
        retries = 0
        delay = 1  # conn.delay
        while retries <= self.retries or self.retries == -1:
            self.last_msg[conn.uuid] = None
            try:
                async with conn.connect() as connection:
                    asyncio.ensure_future(self._watch(connection))
                    # connection was successful, reset retry count and delay
                    retries = 0
                    delay = 1  # conn.delay
                    await subscribe(connection)
                    await self._handler(connection, handler)
            except (ConnectionClosed, ConnectionAbortedError,
                    ConnectionResetError, socket_error) as e:
                # LOG.warning("%s: encountered connection issue %s - reconnecting...", conn.uuid, str(e), exc_info=True)
                await asyncio.sleep(delay)
                retries += 1
                delay *= 2
            except Exception:
                # LOG.error("%s: encountered an exception, reconnecting", conn.uuid, exc_info=True)
                await asyncio.sleep(delay)
                retries += 1
                delay *= 2

        # LOG.error("%s: failed to reconnect after %d retries - exiting", conn.uuid, retries)
        raise ExhaustedRetries()
예제 #2
0
    async def _create_connection(self):
        await asyncio.sleep(self.start_delay)
        retries = 0
        rate_limited = 1
        delay = 1
        while (retries <= self.retries or self.retries == -1) and self.running:
            try:
                async with self.conn.connect() as connection:
                    await self.authenticate(connection)
                    await self.subscribe(connection)
                    # connection was successful, reset retry count and delay
                    retries = 0
                    rate_limited = 0
                    delay = 1
                    if self.timeout != -1:
                        loop = asyncio.get_running_loop()
                        loop.create_task(self._watcher())
                    await self._handler(connection, self.handler)
            except (ConnectionClosed, ConnectionAbortedError, ConnectionResetError, socket_error) as e:
                if self.exceptions:
                    for ex in self.exceptions:
                        if isinstance(e, ex):
                            LOG.warning("%s: encountered exception %s, which is on the ignore list. Raising", self.conn.uuid, str(e))
                            raise
                LOG.warning("%s: encountered connection issue %s - reconnecting in %.1f seconds...", self.conn.uuid, str(e), delay, exc_info=True)
                await asyncio.sleep(delay)
                retries += 1
                delay *= 2
            except InvalidStatusCode as e:
                if self.exceptions:
                    for ex in self.exceptions:
                        if isinstance(e, ex):
                            LOG.warning("%s: encountered exception %s, which is on the ignore list. Raising", self.conn.uuid, str(e))
                            raise
                if e.status_code == 429:
                    rand = random.uniform(1.0, 3.0)
                    LOG.warning("%s: Rate Limited - waiting %d seconds to reconnect", self.conn.uuid, (rate_limited * 60 * rand))
                    await asyncio.sleep(rate_limited * 60 * rand)
                    rate_limited += 1
                else:
                    LOG.warning("%s: encountered connection issue %s - reconnecting in %.1f seconds...", self.conn.uuid, str(e), delay, exc_info=True)
                    await asyncio.sleep(delay)
                    retries += 1
                    delay *= 2
            except Exception as e:
                if self.exceptions:
                    for ex in self.exceptions:
                        if isinstance(e, ex):
                            LOG.warning("%s: encountered exception %s, which is on the ignore list. Raising", self.conn.uuid, str(e))
                            raise
                LOG.error("%s: encountered an exception, reconnecting in %.1f seconds", self.conn.uuid, delay, exc_info=True)
                await asyncio.sleep(delay)
                retries += 1
                delay *= 2

        if not self.running:
            LOG.info('%s: terminate the connection handler because not running', self.conn.uuid)
        else:
            LOG.error('%s: failed to reconnect after %d retries - exiting', self.conn.uuid, retries)
            raise ExhaustedRetries()
예제 #3
0
    async def _connect(self, feed):
        """
        Connect to websocket feeds
        """
        retries = 0
        delay = 1
        while retries <= self.retries or self.retries == -1:
            self.last_msg[feed.uuid] = None
            try:
                # Coinbase frequently will not respond to pings within the ping interval, so
                # disable the interval in favor of the internal watcher, which will
                # close the connection and reconnect in the event that no message from the exchange
                # has been received (as opposed to a missing ping)
                async with websockets.connect(feed.address, ping_interval=30, ping_timeout=None,
                        max_size=2**23, max_queue=None, origin=feed.origin) as websocket:
                    asyncio.ensure_future(self._watch(feed.uuid, websocket))
                    # connection was successful, reset retry count and delay
                    retries = 0
                    delay = 1
                    await feed.subscribe(websocket)
                    await self._handler(websocket, feed.message_handler, feed.uuid)
            except (ConnectionClosed, ConnectionAbortedError, ConnectionResetError, socket_error) as e:
                LOG.warning("%s: encountered connection issue %s - reconnecting...", feed.id, str(e), exc_info=True)
                await asyncio.sleep(delay)
                retries += 1
                delay *= 2
            except Exception:
                LOG.error("%s: encountered an exception, reconnecting", feed.id, exc_info=True)
                await asyncio.sleep(delay)
                retries += 1
                delay *= 2

        LOG.error("%s: failed to reconnect after %d retries - exiting", feed.id, retries)
        raise ExhaustedRetries()
예제 #4
0
    async def _connect(self, conn, subscribe, handler):
        """
        Connect to exchange and subscribe
        """
        retries = 0
        rate_limited = 1
        delay = conn.delay
        while retries < self.retries or self.retries == -1:
            self.last_msg[conn.uuid] = None
            try:
                async with conn.connect() as connection:
                    asyncio.ensure_future(self._watch(connection))
                    # connection was successful, reset retry count and delay
                    retries = 0
                    rate_limited = 0
                    delay = conn.delay
                    await subscribe(connection)
                    await self._handler(connection, handler)
            except (ConnectionClosed, ConnectionAbortedError, ConnectionResetError, socket_error) as e:
                if self.exceptions:
                    for ex in self.exceptions:
                        if isinstance(e, ex):
                            LOG.warning("%s: encountered exception %s, which is on the ignore list. Raising", conn.uuid, str(e))
                            raise
                LOG.warning("%s: encountered connection issue %s - reconnecting in %.1f seconds...", conn.uuid, str(e), delay, exc_info=True)
                await asyncio.sleep(delay)
                retries += 1
                delay *= 2
            except InvalidStatusCode as e:
                if self.exceptions:
                    for ex in self.exceptions:
                        if isinstance(e, ex):
                            LOG.warning("%s: encountered exception %s, which is on the ignore list. Raising", conn.uuid, str(e))
                            raise
                if e.status_code == 429:
                    LOG.warning("%s: Rate Limited - waiting %d seconds to reconnect", conn.uuid, rate_limited * 60)
                    await asyncio.sleep(rate_limited * 60)
                    rate_limited += 1
                else:
                    LOG.warning("%s: encountered connection issue %s - reconnecting in %.1f seconds...", conn.uuid, str(e), delay, exc_info=True)
                    await asyncio.sleep(delay)
                    retries += 1
                    delay *= 2
            except Exception:
                if self.exceptions:
                    for ex in self.exceptions:
                        if isinstance(e, ex):
                            LOG.warning("%s: encountered exception %s, which is on the ignore list. Raising", conn.uuid, str(e))
                            raise
                LOG.error("%s: encountered an exception, reconnecting in %.1f seconds", conn.uuid, delay, exc_info=True)
                await asyncio.sleep(delay)
                retries += 1
                delay *= 2

        if self.retries == 0:
            LOG.info('%s: terminate the connection handler because self.retries=0', conn.uuid)
        else:
            LOG.error('%s: failed to reconnect after %d retries - exiting', conn.uuid, retries)
            raise ExhaustedRetries()
예제 #5
0
    async def _rest_connect(self, feed):
        """
        Connect to REST feed
        """
        retries = 0
        delay = 1
        while retries <= self.retries or self.retries == -1:
            await feed.subscribe()
            try:
                while True:
                    await feed.message_handler()
            except Exception:
                LOG.error("%s: encountered an exception, reconnecting", feed.id, exc_info=True)
                await asyncio.sleep(delay)
                retries += 1
                delay *= 2

        LOG.error("%s: failed to reconnect after %d retries - exiting", feed.id, retries)
        raise ExhaustedRetries()