switch to txrequests to marry async requests to twisted

This commit is contained in:
Alex Grintsvayg 2017-03-23 18:25:31 -04:00
parent c9c84784c9
commit a2a29fc02c
5 changed files with 15 additions and 23 deletions

View file

@ -16,7 +16,7 @@ at anytime.
### Changed
* Removed check_pending logic from Daemon
*
* Switched to txrequests so requests can use twisted event loop
*
### Fixed

View file

@ -3,32 +3,24 @@ import json
import logging
from requests import auth
from requests_futures import sessions
from txrequests import Session
from lbrynet import conf
from lbrynet.analytics import utils
log = logging.getLogger(__name__)
def log_response(fn):
def _log(future):
if future.cancelled():
log.warning('Request was unexpectedly cancelled')
elif future.exception():
exc, traceback = future.exception_info()
log.warning('Failed to send an analytics event', exc_info=(type(exc), exc, traceback))
# GRIN TURNED THIS OFF. Segment only has one response: {"success": true}
# else:
# response = future.result()
# log.debug('Response (%s): %s', response.status_code, response.content)
def _log_error(failure):
log.warning('Failed to send an analytics event. %s', failure.getTraceback())
@functools.wraps(fn)
def wrapper(*args, **kwargs):
future = fn(*args, **kwargs)
future.add_done_callback(_log)
return future
d = fn(*args, **kwargs)
d.addErrback(_log_error)
return d
return wrapper
@ -81,7 +73,7 @@ class Api(object):
def new_instance(cls, session=None):
"""Initialize an instance using values from the configuration"""
if not session:
session = sessions.FuturesSession()
session = Session()
return cls(
session,
conf.settings['ANALYTICS_ENDPOINT'],

View file

@ -6,7 +6,7 @@ import os
import sys
import traceback
from requests_futures.sessions import FuturesSession
from txrequests import Session
import twisted.python.log
from lbrynet import __version__ as lbrynet_version, analytics, build_type, conf
@ -29,7 +29,6 @@ _srcfile = os.path.normcase(_srcfile)
#####
session = FuturesSession()
TRACE = 5
@ -39,12 +38,13 @@ def bg_cb(sess, resp):
class HTTPSHandler(logging.Handler):
def __init__(self, url, fqdn=False, localname=None, facility=None):
def __init__(self, url, fqdn=False, localname=None, facility=None, session=None):
logging.Handler.__init__(self)
self.url = url
self.fqdn = fqdn
self.localname = localname
self.facility = facility
self.session = session if session is not None else Session()
def get_full_message(self, record):
if record.exc_info:
@ -55,7 +55,7 @@ class HTTPSHandler(logging.Handler):
def emit(self, record):
try:
payload = self.format(record)
session.post(self.url, data=payload, background_callback=bg_cb)
self.session.post(self.url, data=payload, background_callback=bg_cb)
except (KeyboardInterrupt, SystemExit):
raise
except:

View file

@ -18,7 +18,7 @@ pycrypto==2.6.1
pyyaml==3.12
qrcode==5.2.2
requests==2.9.1
requests_futures==0.9.7
txrequests==0.9.5
seccure==0.3.1.3
service_identity==16.0.0
six>=1.9.0

View file

@ -24,7 +24,7 @@ requires = [
'pycrypto',
'pyyaml',
'requests',
'requests_futures',
'txrequests',
'seccure',
'txJSON-RPC',
'zope.interface',