forked from LBRYCommunity/lbry-sdk
Merge branch 'txrequests'
* txrequests: switch to txrequests to marry async requests to twisted
This commit is contained in:
commit
a0a122f0ef
5 changed files with 15 additions and 23 deletions
|
@ -16,7 +16,7 @@ at anytime.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
* Removed check_pending logic from Daemon
|
* Removed check_pending logic from Daemon
|
||||||
*
|
* Switched to txrequests so requests can use twisted event loop
|
||||||
*
|
*
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
|
@ -3,32 +3,24 @@ import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from requests import auth
|
from requests import auth
|
||||||
from requests_futures import sessions
|
from txrequests import Session
|
||||||
|
|
||||||
from lbrynet import conf
|
from lbrynet import conf
|
||||||
from lbrynet.analytics import utils
|
from lbrynet.analytics import utils
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def log_response(fn):
|
def log_response(fn):
|
||||||
def _log(future):
|
def _log_error(failure):
|
||||||
if future.cancelled():
|
log.warning('Failed to send an analytics event. %s', failure.getTraceback())
|
||||||
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)
|
|
||||||
|
|
||||||
@functools.wraps(fn)
|
@functools.wraps(fn)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
future = fn(*args, **kwargs)
|
d = fn(*args, **kwargs)
|
||||||
future.add_done_callback(_log)
|
d.addErrback(_log_error)
|
||||||
return future
|
return d
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,7 +73,7 @@ class Api(object):
|
||||||
def new_instance(cls, session=None):
|
def new_instance(cls, session=None):
|
||||||
"""Initialize an instance using values from the configuration"""
|
"""Initialize an instance using values from the configuration"""
|
||||||
if not session:
|
if not session:
|
||||||
session = sessions.FuturesSession()
|
session = Session()
|
||||||
return cls(
|
return cls(
|
||||||
session,
|
session,
|
||||||
conf.settings['ANALYTICS_ENDPOINT'],
|
conf.settings['ANALYTICS_ENDPOINT'],
|
||||||
|
|
|
@ -6,7 +6,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from requests_futures.sessions import FuturesSession
|
from txrequests import Session
|
||||||
import twisted.python.log
|
import twisted.python.log
|
||||||
|
|
||||||
from lbrynet import __version__ as lbrynet_version, analytics, build_type, conf
|
from lbrynet import __version__ as lbrynet_version, analytics, build_type, conf
|
||||||
|
@ -29,7 +29,6 @@ _srcfile = os.path.normcase(_srcfile)
|
||||||
#####
|
#####
|
||||||
|
|
||||||
|
|
||||||
session = FuturesSession()
|
|
||||||
TRACE = 5
|
TRACE = 5
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,12 +38,13 @@ def bg_cb(sess, resp):
|
||||||
|
|
||||||
|
|
||||||
class HTTPSHandler(logging.Handler):
|
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)
|
logging.Handler.__init__(self)
|
||||||
self.url = url
|
self.url = url
|
||||||
self.fqdn = fqdn
|
self.fqdn = fqdn
|
||||||
self.localname = localname
|
self.localname = localname
|
||||||
self.facility = facility
|
self.facility = facility
|
||||||
|
self.session = session if session is not None else Session()
|
||||||
|
|
||||||
def get_full_message(self, record):
|
def get_full_message(self, record):
|
||||||
if record.exc_info:
|
if record.exc_info:
|
||||||
|
@ -55,7 +55,7 @@ class HTTPSHandler(logging.Handler):
|
||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
try:
|
try:
|
||||||
payload = self.format(record)
|
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):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
raise
|
raise
|
||||||
except:
|
except:
|
||||||
|
|
|
@ -18,7 +18,7 @@ pycrypto==2.6.1
|
||||||
pyyaml==3.12
|
pyyaml==3.12
|
||||||
qrcode==5.2.2
|
qrcode==5.2.2
|
||||||
requests==2.9.1
|
requests==2.9.1
|
||||||
requests_futures==0.9.7
|
txrequests==0.9.5
|
||||||
seccure==0.3.1.3
|
seccure==0.3.1.3
|
||||||
service_identity==16.0.0
|
service_identity==16.0.0
|
||||||
six>=1.9.0
|
six>=1.9.0
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -24,7 +24,7 @@ requires = [
|
||||||
'pycrypto',
|
'pycrypto',
|
||||||
'pyyaml',
|
'pyyaml',
|
||||||
'requests',
|
'requests',
|
||||||
'requests_futures',
|
'txrequests',
|
||||||
'seccure',
|
'seccure',
|
||||||
'txJSON-RPC',
|
'txJSON-RPC',
|
||||||
'zope.interface',
|
'zope.interface',
|
||||||
|
|
Loading…
Reference in a new issue