mirror of
https://github.com/sprockets/sprockets.mixins.correlation.git
synced 2024-11-23 19:29:55 +00:00
HandlerMixin: Make prepare async-safe.
This commit is contained in:
parent
3a6f57e5f6
commit
c38e80da17
1 changed files with 15 additions and 1 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue