Merge pull request #5 from dave-shawley/async-prepare

Async prepare
This commit is contained in:
Ryan Mclean 2016-06-21 11:08:19 -04:00 committed by GitHub
commit 08bfdcc23f
7 changed files with 43 additions and 10 deletions

View file

@ -1,9 +1,14 @@
Version History
---------------
`1.0.2`_ (20-Jun-2016)
~~~~~~~~~~~~~~~~~~~~~~
- Add support for async prepare in superclasses of ``HandlerMixin``
`1.0.1`_ (31-Mar-2015)
~~~~~~~~~~~~~~~~~~~~~~
- Adds ``sprockets.mixins.correlation.HandlerMixin``
.. _`1.0.2`: https://github.com/sprockets/sprockets.mixins.correlation/compare/1.0.1...1.0.2
.. _`1.0.1`: https://github.com/sprockets/sprockets.mixins.correlation/compare/0.0.0...1.0.1

View file

@ -81,8 +81,8 @@ Relayed Correlation ID
.. |Downloads| image:: https://img.shields.io/pypi/dm/sprockets.mixins.correlation.svg
:target: https://pypi.python.org/pypi/sprockets.mixins.correlation
.. |License| image:: https://pypip.in/license/sprockets.mixins.correlation/badge.svg
:target: https://sprocketsmixinscorrelation.readthedocs.org/
:target: https://sprocketsmixinscorrelation.readthedocs.io/
.. |Documentation| image:: https://readthedocs.org/projects/sprocketsmixinscorrelation/badge
:target: https://sprocketsmixinscorrelation.readthedocs.org/
:target: https://sprocketsmixinscorrelation.readthedocs.io/
.. _Python Package Index: https://pypi.python.org/pypi/sprockets.mixins.correlation

View file

@ -10,7 +10,7 @@ templates_path = []
source_suffix = '.rst'
master_doc = 'index'
project = 'sprockets.mixins.correlation'
copyright = '2015, AWeber Communications'
copyright = '2015-2016, AWeber Communications'
version = '.'.join(__version__.split('.')[0:1])
release = __version__
if len(version_info) > 3:

View file

@ -0,0 +1 @@
tornado>=3.1,<4.4

View file

@ -1,5 +1,12 @@
try:
from .mixins import HandlerMixin
except ImportError as error:
version_info = (1, 0, 1)
class HandlerMixin(object):
def __init__(self, *args, **kwargs):
raise error
version_info = (1, 0, 2)
__version__ = '.'.join(str(v) for v in version_info[:3])

View file

@ -1,5 +1,16 @@
import uuid
import tornado.gen
import tornado.log
if tornado.version_info[0] >= 4:
from tornado.concurrent import is_future
else:
import tornado.concurrent
def is_future(maybe_future):
return isinstance(maybe_future, tornado.concurrent.Future)
class HandlerMixin(object):
"""
@ -39,11 +50,15 @@ class HandlerMixin(object):
self.__correlation_id = str(uuid.uuid4())
super(HandlerMixin, self).__init__(*args, **kwargs)
@tornado.gen.coroutine
def prepare(self):
# Here we want to copy an incoming Correlation-ID header if
# one exists. We also want to set it in the outgoing response
# which the property setter does for us.
super(HandlerMixin, self).prepare()
maybe_future = super(HandlerMixin, self).prepare()
if is_future(maybe_future):
yield maybe_future
correlation_id = self.get_request_header(self.__header_name, None)
if correlation_id is not None:
self.correlation_id = correlation_id
@ -98,11 +113,11 @@ def correlation_id_logger(handler):
is processing the client request.
"""
if handler.get_status() < 400:
log_method = log.access_log.info
log_method = tornado.log.access_log.info
elif handler.get_status() < 500:
log_method = log.access_log.warning
log_method = tornado.log.access_log.warning
else:
log_method = log.access_log.error
log_method = tornado.log.access_log.error
request_time = 1000.0 * handler.request.request_time()
correlation_id = getattr(handler, "correlation_id", None)
if correlation_id is None:

View file

@ -1,5 +1,5 @@
[tox]
envlist = py27,py33,py34,pypy,pypy3,tornado31,tornado32,tornado40
envlist = py27,py33,py34,pypy,pypy3,tornado31,tornado32,tornado40,tornado43
toxworkdir = {toxinidir}/build/tox
skip_missing_intepreters = true
@ -28,3 +28,8 @@ deps =
deps =
-rtest-requirements.txt
tornado>=4.0,<4.1
[testenv:tornado43]
deps =
-rtest-requirements.txt
tornado>=4.3,<4.4