]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
resources_rpc: fixed singleton behavior for ResourcesPullRpcApi
authorIhar Hrachyshka <ihrachys@redhat.com>
Mon, 10 Aug 2015 06:57:41 +0000 (08:57 +0200)
committerJakub Libosvar <libosvar@redhat.com>
Mon, 10 Aug 2015 09:20:18 +0000 (09:20 +0000)
__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

neutron/api/rpc/handlers/resources_rpc.py
neutron/tests/unit/api/rpc/handlers/test_resources_rpc.py

index c3c9afe0454729deb8927cbd280d912aaf08d382..55344a8110427772bde2773546d1f7cb1c9f5667 100755 (executable)
@@ -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)
index 4fd58afa2656841e2e4ca533dead3531d796872c..64d67dacff0b806f2976cf8dbae875491252db22 100755 (executable)
@@ -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):