From c38e80da17892557d991717600642f354c97be9c Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Mon, 20 Jun 2016 15:54:35 -0400 Subject: [PATCH] HandlerMixin: Make prepare async-safe. --- sprockets/mixins/correlation/mixins.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/sprockets/mixins/correlation/mixins.py b/sprockets/mixins/correlation/mixins.py index 6d87db3..92ec499 100644 --- a/sprockets/mixins/correlation/mixins.py +++ b/sprockets/mixins/correlation/mixins.py @@ -1,5 +1,15 @@ 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): """ @@ -39,11 +49,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