HandlerMixin: Make prepare async-safe.

This commit is contained in:
Dave Shawley 2016-06-20 15:54:35 -04:00
parent 3a6f57e5f6
commit c38e80da17

View file

@ -1,5 +1,15 @@
import uuid import uuid
import tornado.gen
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): class HandlerMixin(object):
""" """
@ -39,11 +49,15 @@ class HandlerMixin(object):
self.__correlation_id = str(uuid.uuid4()) self.__correlation_id = str(uuid.uuid4())
super(HandlerMixin, self).__init__(*args, **kwargs) super(HandlerMixin, self).__init__(*args, **kwargs)
@tornado.gen.coroutine
def prepare(self): def prepare(self):
# Here we want to copy an incoming Correlation-ID header if # Here we want to copy an incoming Correlation-ID header if
# one exists. We also want to set it in the outgoing response # one exists. We also want to set it in the outgoing response
# which the property setter does for us. # 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) correlation_id = self.get_request_header(self.__header_name, None)
if correlation_id is not None: if correlation_id is not None:
self.correlation_id = correlation_id self.correlation_id = correlation_id