diff --git a/lbry/lbry/extras/daemon/Components.py b/lbry/lbry/extras/daemon/Components.py index ee89dc7d3..f75260f3a 100644 --- a/lbry/lbry/extras/daemon/Components.py +++ b/lbry/lbry/extras/daemon/Components.py @@ -218,6 +218,7 @@ class HeadersComponent(Component): await self.headers.close() else: log.info("Header checkpoint verified.") + async def stop(self): pass @@ -239,14 +240,14 @@ class WalletComponent(Component): local_height = self.wallet_manager.ledger.headers.height remote_height = self.wallet_manager.ledger.network.remote_height best_hash = self.wallet_manager.get_best_blockhash() - server = self.wallet_manager.ledger.network.client.server return { 'connected_servers': [ { - 'host': server[0], - 'port': server[1], - 'latency': 0 # TODO: use real latency - } + 'host': session.server[0], + 'port': session.server[1], + 'latency': round(session.connection_latency, 2), + } for session in self.wallet_manager.ledger.network.session_pool.sessions + if session and session.available ], 'blocks': max(local_height, 0), 'blocks_behind': max(remote_height - local_height, 0), diff --git a/lbry/lbry/extras/daemon/Daemon.py b/lbry/lbry/extras/daemon/Daemon.py index 5c12ddebf..ea4619eb0 100644 --- a/lbry/lbry/extras/daemon/Daemon.py +++ b/lbry/lbry/extras/daemon/Daemon.py @@ -392,6 +392,10 @@ class Daemon(metaclass=JSONRPCServerType): async def update_connection_status(self): connected = await utils.async_check_connection() + if connected and not self._connection_status[1]: + log.info("detected internet connection is working") + elif not connected and self._connection_status[1]: + log.warning("detected internet connection was lost") self._connection_status = (self.component_manager.loop.time(), connected) async def get_connection_status(self) -> str: diff --git a/lbry/lbry/extras/daemon/exchange_rate_manager.py b/lbry/lbry/extras/daemon/exchange_rate_manager.py index f15d14478..bf784920d 100644 --- a/lbry/lbry/extras/daemon/exchange_rate_manager.py +++ b/lbry/lbry/extras/daemon/exchange_rate_manager.py @@ -3,7 +3,8 @@ import time import logging import json from decimal import Decimal - +from typing import Optional +from aiohttp.client_exceptions import ClientError from lbry.error import InvalidExchangeRateResponse, CurrencyConversionError from lbry.utils import aiohttp_request @@ -44,7 +45,7 @@ class MarketFeed: self.params = params self.fee = fee self.rate = None - self._task: asyncio.Task = None + self._task: Optional[asyncio.Task] = None self._online = True def rate_is_initialized(self): @@ -80,7 +81,7 @@ class MarketFeed: try: response = await asyncio.wait_for(self._make_request(), self.REQUESTS_TIMEOUT) self._save_price(self._subtract_fee(self._handle_response(response))) - except (asyncio.TimeoutError, InvalidExchangeRateResponse) as err: + except (asyncio.TimeoutError, InvalidExchangeRateResponse, ClientError) as err: self._on_error(err) await asyncio.sleep(self.EXCHANGE_RATE_UPDATE_RATE_SEC) diff --git a/lbry/lbry/extras/daemon/loggly_handler.py b/lbry/lbry/extras/daemon/loggly_handler.py index 121d5454a..91108bc73 100644 --- a/lbry/lbry/extras/daemon/loggly_handler.py +++ b/lbry/lbry/extras/daemon/loggly_handler.py @@ -1,4 +1,5 @@ import asyncio +from aiohttp.client_exceptions import ClientError import json import logging.handlers import traceback @@ -49,9 +50,12 @@ class HTTPSLogglyHandler(logging.Handler): return record.getMessage() async def _emit(self, record): - payload = self.format(record) - async with utils.aiohttp_request('post', self.url, data=payload.encode(), cookies=self.cookies) as response: - self.cookies.update(response.cookies) + try: + async with utils.aiohttp_request('post', self.url, data=self.format(record).encode(), + cookies=self.cookies) as response: + self.cookies.update(response.cookies) + except ClientError: + pass def emit(self, record): asyncio.ensure_future(self._emit(record)) diff --git a/lbry/lbry/utils.py b/lbry/lbry/utils.py index 3bd9f5c2e..a1a9ad6f1 100644 --- a/lbry/lbry/utils.py +++ b/lbry/lbry/utils.py @@ -81,28 +81,22 @@ def check_connection(server="lbry.com", port=80, timeout=5) -> bool: try: server = socket.gethostbyname(server) socket.create_connection((server, port), timeout).close() - log.debug('Connection successful') return True except (socket.gaierror, socket.herror) as ex: - log.warning("Failed to connect to %s:%s. Unable to resolve domain. Trying to bypass DNS", - server, port) + log.debug("Failed to connect to %s:%s. Unable to resolve domain. Trying to bypass DNS", + server, port) try: server = "8.8.8.8" port = 53 socket.create_connection((server, port), timeout).close() - log.debug('Connection successful') return True - except Exception: - log.error("Failed to connect to %s:%s. Maybe the internet connection is not working", - server, port) + except OSError: return False - except Exception: - log.error("Failed to connect to %s:%s. Maybe the internet connection is not working", - server, port) + except OSError: return False -async def async_check_connection(server="lbry.com", port=80, timeout=5) -> bool: +async def async_check_connection(server="lbry.com", port=80, timeout=1) -> bool: return await asyncio.get_event_loop().run_in_executor(None, check_connection, server, port, timeout)