Merge pull request #2411 from lbryio/fix-offline-connection-errors

Fix misc errors when going offline/online
This commit is contained in:
Jack Robison 2019-08-22 13:48:02 -04:00 committed by GitHub
commit ff4b2f2dec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 22 deletions

View file

@ -218,6 +218,7 @@ class HeadersComponent(Component):
await self.headers.close() await self.headers.close()
else: else:
log.info("Header checkpoint verified.") log.info("Header checkpoint verified.")
async def stop(self): async def stop(self):
pass pass
@ -239,14 +240,14 @@ class WalletComponent(Component):
local_height = self.wallet_manager.ledger.headers.height local_height = self.wallet_manager.ledger.headers.height
remote_height = self.wallet_manager.ledger.network.remote_height remote_height = self.wallet_manager.ledger.network.remote_height
best_hash = self.wallet_manager.get_best_blockhash() best_hash = self.wallet_manager.get_best_blockhash()
server = self.wallet_manager.ledger.network.client.server
return { return {
'connected_servers': [ 'connected_servers': [
{ {
'host': server[0], 'host': session.server[0],
'port': server[1], 'port': session.server[1],
'latency': 0 # TODO: use real latency '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': max(local_height, 0),
'blocks_behind': max(remote_height - local_height, 0), 'blocks_behind': max(remote_height - local_height, 0),

View file

@ -392,6 +392,10 @@ class Daemon(metaclass=JSONRPCServerType):
async def update_connection_status(self): async def update_connection_status(self):
connected = await utils.async_check_connection() 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) self._connection_status = (self.component_manager.loop.time(), connected)
async def get_connection_status(self) -> str: async def get_connection_status(self) -> str:

View file

@ -3,7 +3,8 @@ import time
import logging import logging
import json import json
from decimal import Decimal from decimal import Decimal
from typing import Optional
from aiohttp.client_exceptions import ClientError
from lbry.error import InvalidExchangeRateResponse, CurrencyConversionError from lbry.error import InvalidExchangeRateResponse, CurrencyConversionError
from lbry.utils import aiohttp_request from lbry.utils import aiohttp_request
@ -44,7 +45,7 @@ class MarketFeed:
self.params = params self.params = params
self.fee = fee self.fee = fee
self.rate = None self.rate = None
self._task: asyncio.Task = None self._task: Optional[asyncio.Task] = None
self._online = True self._online = True
def rate_is_initialized(self): def rate_is_initialized(self):
@ -80,7 +81,7 @@ class MarketFeed:
try: try:
response = await asyncio.wait_for(self._make_request(), self.REQUESTS_TIMEOUT) response = await asyncio.wait_for(self._make_request(), self.REQUESTS_TIMEOUT)
self._save_price(self._subtract_fee(self._handle_response(response))) 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) self._on_error(err)
await asyncio.sleep(self.EXCHANGE_RATE_UPDATE_RATE_SEC) await asyncio.sleep(self.EXCHANGE_RATE_UPDATE_RATE_SEC)

View file

@ -1,4 +1,5 @@
import asyncio import asyncio
from aiohttp.client_exceptions import ClientError
import json import json
import logging.handlers import logging.handlers
import traceback import traceback
@ -49,9 +50,12 @@ class HTTPSLogglyHandler(logging.Handler):
return record.getMessage() return record.getMessage()
async def _emit(self, record): async def _emit(self, record):
payload = self.format(record) try:
async with utils.aiohttp_request('post', self.url, data=payload.encode(), cookies=self.cookies) as response: async with utils.aiohttp_request('post', self.url, data=self.format(record).encode(),
cookies=self.cookies) as response:
self.cookies.update(response.cookies) self.cookies.update(response.cookies)
except ClientError:
pass
def emit(self, record): def emit(self, record):
asyncio.ensure_future(self._emit(record)) asyncio.ensure_future(self._emit(record))

View file

@ -81,28 +81,22 @@ def check_connection(server="lbry.com", port=80, timeout=5) -> bool:
try: try:
server = socket.gethostbyname(server) server = socket.gethostbyname(server)
socket.create_connection((server, port), timeout).close() socket.create_connection((server, port), timeout).close()
log.debug('Connection successful')
return True return True
except (socket.gaierror, socket.herror) as ex: except (socket.gaierror, socket.herror) as ex:
log.warning("Failed to connect to %s:%s. Unable to resolve domain. Trying to bypass DNS", log.debug("Failed to connect to %s:%s. Unable to resolve domain. Trying to bypass DNS",
server, port) server, port)
try: try:
server = "8.8.8.8" server = "8.8.8.8"
port = 53 port = 53
socket.create_connection((server, port), timeout).close() socket.create_connection((server, port), timeout).close()
log.debug('Connection successful')
return True return True
except Exception: except OSError:
log.error("Failed to connect to %s:%s. Maybe the internet connection is not working",
server, port)
return False return False
except Exception: except OSError:
log.error("Failed to connect to %s:%s. Maybe the internet connection is not working",
server, port)
return False 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) return await asyncio.get_event_loop().run_in_executor(None, check_connection, server, port, timeout)