Support overriding class for get_rpc_* helper functions

We currently do not support overriding the class being
instantiated in the RPC helper functions, this adds that
support so that projects that define their own classes
that inherit from oslo.messaging can use the helpers.

For example neutron utilizes code from neutron-lib that
has it's own RPCClient implementation that inherits from
oslo.messaging, in order for them to use for example
the get_rpc_client helper they need support to override
the class being returned. The alternative would be to
modify the internal _manual_load variable which seems
counter-productive to extending the API provided to
consumers.

Change-Id: Ie22f2ee47a4ca3f28a71272ee1ffdb88aaeb7758
This commit is contained in:
Tobias Urdin 2023-01-12 07:19:46 +00:00 committed by Tobias Urdin
parent 7505316902
commit 687dea2e65
4 changed files with 20 additions and 7 deletions

View File

@ -550,14 +550,16 @@ class RPCClient(_BaseCallContext):
return self.prepare(version=version).can_send_version()
def get_rpc_client(transport, target, **kwargs):
def get_rpc_client(transport, target, client_cls=RPCClient, **kwargs):
"""Construct an RPC client.
:param transport: the messaging transport
:type transport: Transport
:param target: the exchange, topic and server to listen on
:type target: Target
:param client_cls: The client class to instantiate
:type client_cls: class
:param **kwargs: The kwargs will be passed down to the
RPCClient constructor
client_cls constructor
"""
return RPCClient(transport, target, _manual_load=False, **kwargs)
return client_cls(transport, target, _manual_load=False, **kwargs)

View File

@ -200,7 +200,8 @@ class RPCServer(msg_server.MessageHandlingServer):
def get_rpc_server(transport, target, endpoints,
executor=None, serializer=None, access_policy=None):
executor=None, serializer=None, access_policy=None,
server_cls=RPCServer):
"""Construct an RPC server.
:param transport: the messaging transport
@ -217,10 +218,12 @@ def get_rpc_server(transport, target, endpoints,
:param access_policy: an optional access policy.
Defaults to DefaultRPCAccessPolicy
:type access_policy: RPCAccessPolicyBase
:param server_cls: The server class to instantiate
:type server_cls: class
"""
dispatcher = rpc_dispatcher.RPCDispatcher(endpoints, serializer,
access_policy)
return RPCServer(transport, target, dispatcher, executor)
return server_cls(transport, target, dispatcher, executor)
def expected_exceptions(*exceptions):

View File

@ -22,7 +22,8 @@ __all__ = [
def get_rpc_transport(conf, url=None,
allowed_remote_exmods=None):
allowed_remote_exmods=None,
transport_cls=msg_transport.RPCTransport):
"""A factory method for Transport objects for RPCs.
This method should be used to ensure the correct messaging functionality
@ -43,7 +44,9 @@ def get_rpc_transport(conf, url=None,
transport will deserialize remote exceptions
from
:type allowed_remote_exmods: list
:param transport_cls: the transport class to instantiate
:type transport_cls: class
"""
return msg_transport._get_transport(
conf, url, allowed_remote_exmods,
transport_cls=msg_transport.RPCTransport)
transport_cls=transport_cls)

View File

@ -0,0 +1,5 @@
---
features:
- |
The ``get_rpc_transport``, ``get_rpc_server`` and ``get_rpc_client`` helper
functions now have support for overriding the class that is instantiated.