From 7d8936cc802ce28a476e30a6b94ab23881580b48 Mon Sep 17 00:00:00 2001 From: Eoghan Glynn Date: Thu, 19 Jul 2012 11:42:15 +0100 Subject: Static FaultWrapper status_to_type map. Avoid maintaining needless copies of this dict, by instantiating lazily once and only once. This approach will bring master into sync with the corresponding patch proposed to stable/essex: https://review.openstack.org/9446 Change-Id: I8a7cd5fc4fe0effd436e91a6c481df7b0d5a8b01 --- nova/api/openstack/__init__.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/nova/api/openstack/__init__.py b/nova/api/openstack/__init__.py index f49c721ff..c1ce7ee94 100644 --- a/nova/api/openstack/__init__.py +++ b/nova/api/openstack/__init__.py @@ -37,18 +37,21 @@ LOG = logging.getLogger(__name__) class FaultWrapper(base_wsgi.Middleware): """Calls down the middleware stack, making exceptions into faults.""" - def __init__(self, application): - self.status_to_type = {} - for clazz in utils.walk_class_hierarchy(webob.exc.HTTPError): - self.status_to_type[clazz.code] = clazz - super(FaultWrapper, self).__init__(application) + _status_to_type = {} + + @staticmethod + def status_to_type(status): + if not FaultWrapper._status_to_type: + for clazz in utils.walk_class_hierarchy(webob.exc.HTTPError): + FaultWrapper._status_to_type[clazz.code] = clazz + return FaultWrapper._status_to_type.get( + status, webob.exc.HTTPInternalServerError)() def _error(self, inner, req, headers=None, status=500, safe=False): LOG.exception(_("Caught error: %s"), unicode(inner)) msg_dict = dict(url=req.url, status=status) LOG.info(_("%(url)s returned with HTTP %(status)d") % msg_dict) - outer = self.status_to_type.get(status, - webob.exc.HTTPInternalServerError)() + outer = self.status_to_type(status) if headers: outer.headers = headers # NOTE(johannes): We leave the explanation empty here on -- cgit