|
@@ -28,6 +28,7 @@ LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
ALLOWED_EXMODS = [
|
|
ALLOWED_EXMODS = [
|
|
|
coriolis.exception.__name__]
|
|
coriolis.exception.__name__]
|
|
|
|
|
+_TRANSPORT = None
|
|
|
|
|
|
|
|
|
|
|
|
|
class RequestContextSerializer(messaging.Serializer):
|
|
class RequestContextSerializer(messaging.Serializer):
|
|
@@ -65,6 +66,13 @@ def get_server(target, endpoints, serializer=None):
|
|
|
serializer=serializer)
|
|
serializer=serializer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+def init():
|
|
|
|
|
+ global _TRANSPORT
|
|
|
|
|
+ if _TRANSPORT is None:
|
|
|
|
|
+ _TRANSPORT = _get_transport()
|
|
|
|
|
+ return _TRANSPORT
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
class BaseRPCClient(object):
|
|
class BaseRPCClient(object):
|
|
|
""" Wrapper for 'oslo_messaging.RPCClient' which automatically
|
|
""" Wrapper for 'oslo_messaging.RPCClient' which automatically
|
|
|
instantiates and cleans up transports for each call.
|
|
instantiates and cleans up transports for each call.
|
|
@@ -76,43 +84,42 @@ class BaseRPCClient(object):
|
|
|
if self._timeout is None:
|
|
if self._timeout is None:
|
|
|
self._timeout = CONF.default_messaging_timeout
|
|
self._timeout = CONF.default_messaging_timeout
|
|
|
self._serializer = RequestContextSerializer(serializer)
|
|
self._serializer = RequestContextSerializer(serializer)
|
|
|
|
|
+ self._transport_conn = None
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
def __repr__(self):
|
|
|
return "<RPCClient(target=%s, timeout=%s)>" % (
|
|
return "<RPCClient(target=%s, timeout=%s)>" % (
|
|
|
self._target, self._timeout)
|
|
self._target, self._timeout)
|
|
|
|
|
|
|
|
- @contextlib.contextmanager
|
|
|
|
|
- def _rpc_messaging_client(self):
|
|
|
|
|
- transport = None
|
|
|
|
|
- try:
|
|
|
|
|
- transport = _get_transport()
|
|
|
|
|
- yield messaging.RPCClient(
|
|
|
|
|
- transport, self._target, serializer=self._serializer,
|
|
|
|
|
|
|
+ @property
|
|
|
|
|
+ def _transport(self):
|
|
|
|
|
+ global _TRANSPORT
|
|
|
|
|
+ if _TRANSPORT is None:
|
|
|
|
|
+ if self._transport_conn is None:
|
|
|
|
|
+ self._transport_conn = _get_transport()
|
|
|
|
|
+ return self._transport_conn
|
|
|
|
|
+ else:
|
|
|
|
|
+ return _TRANSPORT
|
|
|
|
|
+
|
|
|
|
|
+ def _rpc_client(self):
|
|
|
|
|
+ return messaging.RPCClient(
|
|
|
|
|
+ self._transport, self._target,
|
|
|
|
|
+ serializer=self._serializer,
|
|
|
timeout=self._timeout)
|
|
timeout=self._timeout)
|
|
|
- finally:
|
|
|
|
|
- if transport:
|
|
|
|
|
- try:
|
|
|
|
|
- transport.cleanup()
|
|
|
|
|
- except (Exception, KeyboardInterrupt):
|
|
|
|
|
- LOG.warn(
|
|
|
|
|
- "Exception occurred while cleaning up transport for "
|
|
|
|
|
- "RPC client instance '%s'. Error was: %s",
|
|
|
|
|
- repr(self), utils.get_exception_details())
|
|
|
|
|
|
|
|
|
|
def _call(self, ctxt, method, **kwargs):
|
|
def _call(self, ctxt, method, **kwargs):
|
|
|
- with self._rpc_messaging_client() as client:
|
|
|
|
|
- return client.call(ctxt, method, **kwargs)
|
|
|
|
|
|
|
+ client = self._rpc_client()
|
|
|
|
|
+ return client.call(ctxt, method, **kwargs)
|
|
|
|
|
|
|
|
def _call_on_host(self, host, ctxt, method, **kwargs):
|
|
def _call_on_host(self, host, ctxt, method, **kwargs):
|
|
|
- with self._rpc_messaging_client() as client:
|
|
|
|
|
- cctxt = client.prepare(server=host)
|
|
|
|
|
- return cctxt.call(ctxt, method, **kwargs)
|
|
|
|
|
|
|
+ client = self._rpc_client()
|
|
|
|
|
+ cctxt = client.prepare(server=host)
|
|
|
|
|
+ return cctxt.call(ctxt, method, **kwargs)
|
|
|
|
|
|
|
|
def _cast(self, ctxt, method, **kwargs):
|
|
def _cast(self, ctxt, method, **kwargs):
|
|
|
- with self._rpc_messaging_client() as client:
|
|
|
|
|
- client.cast(ctxt, method, **kwargs)
|
|
|
|
|
|
|
+ client = self._rpc_client()
|
|
|
|
|
+ client.cast(ctxt, method, **kwargs)
|
|
|
|
|
|
|
|
def _cast_for_host(self, host, ctxt, method, **kwargs):
|
|
def _cast_for_host(self, host, ctxt, method, **kwargs):
|
|
|
- with self._rpc_messaging_client() as client:
|
|
|
|
|
- cctxt = client.prepare(server=host)
|
|
|
|
|
- cctxt.cast(ctxt, method, **kwargs)
|
|
|
|
|
|
|
+ client = self._rpc_client()
|
|
|
|
|
+ cctxt = client.prepare(server=host)
|
|
|
|
|
+ cctxt.cast(ctxt, method, **kwargs)
|