Просмотр исходного кода

Merge pull request #167 from aznashwan/rpc-client-instantiations

Optimize RPC client instantiations in conductor.
Nashwan Azhari 5 лет назад
Родитель
Сommit
a65ba2d1ae
1 измененных файлов с 17 добавлено и 5 удалено
  1. 17 5
      coriolis/conductor/rpc/server.py

+ 17 - 5
coriolis/conductor/rpc/server.py

@@ -196,23 +196,35 @@ def minion_pool_synchronized(func):
 class ConductorServerEndpoint(object):
 class ConductorServerEndpoint(object):
     def __init__(self):
     def __init__(self):
         self._licensing_client = licensing_client.LicensingClient.from_env()
         self._licensing_client = licensing_client.LicensingClient.from_env()
+        self._scheduler_client_instance = None
+        self._worker_client_instance = None
+        self._replica_cron_client_instance = None
 
 
     # NOTE(aznashwan): it is unsafe to fork processes with pre-instantiated
     # NOTE(aznashwan): it is unsafe to fork processes with pre-instantiated
     # oslo_messaging clients as the underlying eventlet thread queues will
     # oslo_messaging clients as the underlying eventlet thread queues will
     # be invalidated. Considering this class both serves from a "main
     # be invalidated. Considering this class both serves from a "main
     # process" as well as forking child processes, it is safest to
     # process" as well as forking child processes, it is safest to
-    # re-instantiate the clients every time:
+    # instantiate the clients only when needed:
     @property
     @property
-    def _rpc_worker_client(self):
-        return rpc_worker_client.WorkerClient()
+    def _worker_client(self):
+        if not getattr(self, '_worker_client_instance'):
+            self._worker_client_instance = (
+                rpc_worker_client.WorkerClient())
+        return self._worker_client_instance
 
 
     @property
     @property
     def _scheduler_client(self):
     def _scheduler_client(self):
-        return rpc_scheduler_client.SchedulerClient()
+        if not getattr(self, '_scheduler_client_instance'):
+            self._scheduler_client_instance = (
+                rpc_scheduler_client.SchedulerClient())
+        return self._scheduler_client_instance
 
 
     @property
     @property
     def _replica_cron_client(self):
     def _replica_cron_client(self):
-        return rpc_cron_client.ReplicaCronClient()
+        if not getattr(self, '_replica_cron_client_instance'):
+            self._replica_cron_client_instance = (
+                rpc_cron_client.ReplicaCronClient())
+        return self._replica_cron_client_instance
 
 
     def get_all_diagnostics(self, ctxt):
     def get_all_diagnostics(self, ctxt):
         diagnostics = [
         diagnostics = [