forked from LBRYCommunity/lbry-sdk
Merge pull request #2411 from lbryio/fix-offline-connection-errors
Fix misc errors when going offline/online
This commit is contained in:
commit
ff4b2f2dec
5 changed files with 26 additions and 22 deletions
|
@ -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),
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue