From: Ihar Hrachyshka Date: Mon, 10 Aug 2015 06:57:41 +0000 (+0200) Subject: resources_rpc: fixed singleton behavior for ResourcesPullRpcApi X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=4ef2dcc106eb8014daf0f94e12db1030eb86aab6;p=openstack-build%2Fneutron-build.git resources_rpc: fixed singleton behavior for ResourcesPullRpcApi __init__ is called for any __call__ on a class, no matter whether it is a singleton. Meaning, client was reinitialized every time a caller instantiated the Rpc class. That's not a critical issue, but a minor performance hit. Change-Id: I24272ba44eb502c8552d3556c84214942944646c Partially-Implements: blueprint quantum-qos-api --- diff --git a/neutron/api/rpc/handlers/resources_rpc.py b/neutron/api/rpc/handlers/resources_rpc.py index c3c9afe04..55344a811 100755 --- a/neutron/api/rpc/handlers/resources_rpc.py +++ b/neutron/api/rpc/handlers/resources_rpc.py @@ -67,14 +67,12 @@ class ResourcesPullRpcApi(object): # make it a singleton if not hasattr(cls, '_instance'): cls._instance = super(ResourcesPullRpcApi, cls).__new__(cls) + target = oslo_messaging.Target( + topic=topics.PLUGIN, version='1.0', + namespace=constants.RPC_NAMESPACE_RESOURCES) + cls._instance.client = n_rpc.get_client(target) return cls._instance - def __init__(self): - target = oslo_messaging.Target( - topic=topics.PLUGIN, version='1.0', - namespace=constants.RPC_NAMESPACE_RESOURCES) - self.client = n_rpc.get_client(target) - @log_helpers.log_method_call def pull(self, context, resource_type, resource_id): _validate_resource_type(resource_type) diff --git a/neutron/tests/unit/api/rpc/handlers/test_resources_rpc.py b/neutron/tests/unit/api/rpc/handlers/test_resources_rpc.py index 4fd58afa2..64d67dacf 100755 --- a/neutron/tests/unit/api/rpc/handlers/test_resources_rpc.py +++ b/neutron/tests/unit/api/rpc/handlers/test_resources_rpc.py @@ -92,11 +92,11 @@ class ResourcesPullRpcApiTestCase(ResourcesRpcBaseTestCase): def setUp(self): super(ResourcesPullRpcApiTestCase, self).setUp() - mock.patch.object(resources_rpc.n_rpc, 'get_client').start() mock.patch.object(resources_rpc, '_validate_resource_type').start() mock.patch('neutron.api.rpc.callbacks.resources.get_resource_cls', return_value=FakeResource).start() self.rpc = resources_rpc.ResourcesPullRpcApi() + mock.patch.object(self.rpc, 'client').start() self.cctxt_mock = self.rpc.client.prepare.return_value def test_is_singleton(self):