re-use loggly connection

This commit is contained in:
Jack Robison 2019-10-04 09:40:41 -04:00
parent 8c0196301d
commit fc1b4658fc
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2

View file

@ -3,6 +3,7 @@ from aiohttp.client_exceptions import ClientError
import json import json
import logging.handlers import logging.handlers
import traceback import traceback
import aiohttp
from lbry import utils, __version__ from lbry import utils, __version__
@ -42,6 +43,8 @@ class HTTPSLogglyHandler(logging.Handler):
self.url = "https://logs-01.loggly.com/inputs/{token}/tag/{tag}".format( self.url = "https://logs-01.loggly.com/inputs/{token}/tag/{tag}".format(
token=utils.deobfuscate(loggly_token), tag='lbrynet-' + __version__ token=utils.deobfuscate(loggly_token), tag='lbrynet-' + __version__
) )
self._loop = asyncio.get_event_loop()
self._session = aiohttp.ClientSession()
def get_full_message(self, record): def get_full_message(self, record):
if record.exc_info: if record.exc_info:
@ -49,13 +52,17 @@ class HTTPSLogglyHandler(logging.Handler):
else: else:
return record.getMessage() return record.getMessage()
async def _emit(self, record): async def _emit(self, record, retry=True):
data = self.format(record).encode()
try: try:
async with utils.aiohttp_request('post', self.url, data=self.format(record).encode(), async with self._session.post(self.url, data=data,
cookies=self.cookies) as response: cookies=self.cookies) as response:
self.cookies.update(response.cookies) self.cookies.update(response.cookies)
except ClientError: except ClientError:
pass if self._loop.is_running() and retry:
await self._session.close()
self._session = aiohttp.ClientSession()
return await self._emit(record, retry=False)
def emit(self, record): def emit(self, record):
asyncio.ensure_future(self._emit(record)) asyncio.ensure_future(self._emit(record))